教学服务系统

 找回密码
 立即注册
搜索
查看: 595|回复: 3

信息计算2019-1易称清小二班

[复制链接]

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
发表于 2022-5-12 22:17:14 | 显示全部楼层 |阅读模式
1、创建文本A.txt B.txt C.txt;
2、引用类TripleDES_;
3、导入加密文件的路径inFileName1,加密后输出路径outFileName1,解密路径inFileName2,解密后输出路径outFileName2;
5、调用TripleDES_里的方法Encrypt加密,调用TripleDES_里的方法Decrypt解密。


回复

使用道具 举报

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-5-12 22:21:23 | 显示全部楼层
   /**//// <summary>
    类
/// 三重DES
    /// </summary>
    public class TripleDES_
    {
        private TripleDES mydes;
        public string Key;
        public string IV;
        /**//// <summary>
        /// 对称加密类的构造函数
        /// </summary>
        public TripleDES_(string key)
        {
            mydes = new TripleDESCryptoServiceProvider();
            Key = key;
            IV = "#$^%&&*Yisifhsfjsljfslhgosdshf26382837sdfjskhf97(*&(*";
        }
        /**//// <summary>
        /// 对称加密类的构造函数
        /// </summary>
        public TripleDES_(string key, string iv)
        {
            mydes = new TripleDESCryptoServiceProvider();
            Key = key;
            IV = iv;
        }
        /**//// <summary>
        /// 获得密钥
        /// </summary>
        /// <returns>密钥</returns>
        private byte[] GetLegalKey()
        {
            string sTemp = Key;
            mydes.GenerateKey();
            byte[] bytTemp = mydes.Key;
            int KeyLength = bytTemp.Length;
            if (sTemp.Length > KeyLength)
                sTemp = sTemp.Substring(0, KeyLength);
            else if (sTemp.Length < KeyLength)
                sTemp = sTemp.PadRight(KeyLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /**//// <summary>
        /// 获得初始向量IV
        /// </summary>
        /// <returns>初试向量IV</returns>
        private byte[] GetLegalIV()
        {
            string sTemp = IV;
            mydes.GenerateIV();
            byte[] bytTemp = mydes.IV;
            int IVLength = bytTemp.Length;
            if (sTemp.Length > IVLength)
                sTemp = sTemp.Substring(0, IVLength);
            else if (sTemp.Length < IVLength)
                sTemp = sTemp.PadRight(IVLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /**//// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="Source">待加密的串</param>
        /// <returns>经过加密的串</returns>
        public string Encrypt(string Source)
        {
            try
            {
                byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
                MemoryStream ms = new MemoryStream();
                mydes.Key = GetLegalKey();
                mydes.IV = GetLegalIV();
                ICryptoTransform encrypto = mydes.CreateEncryptor();
                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
                cs.Write(bytIn, 0, bytIn.Length);
                cs.FlushFinalBlock();
                ms.Close();
                byte[] bytOut = ms.ToArray();
                return Convert.ToBase64String(bytOut);
            }
            catch (Exception ex)
            {
                throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
            }
        }
        /**//// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="Source">待解密的串</param>
        /// <returns>经过解密的串</returns>
        public string Decrypt(string Source)
        {
            try
            {
                byte[] bytIn = Convert.FromBase64String(Source);
                MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
                mydes.Key = GetLegalKey();
                mydes.IV = GetLegalIV();
                ICryptoTransform encrypto = mydes.CreateDecryptor();
                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs);
                return sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
            }
        }
        /**//// <summary>
        /// 加密方法byte[] to byte[]
        /// </summary>
        /// <param name="Source">待加密的byte数组</param>
        /// <returns>经过加密的byte数组</returns>
        public byte[] Encrypt(byte[] Source)
        {
            try
            {
                byte[] bytIn = Source;
                MemoryStream ms = new MemoryStream();
                mydes.Key = GetLegalKey();
                mydes.IV = GetLegalIV();
                ICryptoTransform encrypto = mydes.CreateEncryptor();
                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
                cs.Write(bytIn, 0, bytIn.Length);
                cs.FlushFinalBlock();
                ms.Close();
                byte[] bytOut = ms.ToArray();
                return bytOut;
            }
            catch (Exception ex)
            {
                throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
            }
        }
        /**//// <summary>
        /// 解密方法byte[] to byte[]
        /// </summary>
        /// <param name="Source">待解密的byte数组</param>
        /// <returns>经过解密的byte数组</returns>
        public byte[] Decrypt(byte[] Source)
        {
            try
            {
                byte[] bytIn = Source;
                MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
                mydes.Key = GetLegalKey();
                mydes.IV = GetLegalIV();
                ICryptoTransform encrypto = mydes.CreateDecryptor();
                CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs);
                return UTF8Encoding.UTF8.GetBytes(sr.ReadToEnd());
            }
            catch (Exception ex)
            {
                throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
            }
        }

        /**//// <summary>
        /// 加密方法File to File
        /// </summary>
        /// <param name="inFileName">待加密文件的路径</param>
        /// <param name="outFileName">待加密后文件的输出路径</param>

        public void Encrypt(string inFileName, string outFileName)
        {
            try
            {

                FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
                FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                fout.SetLength(0);

                mydes.Key = GetLegalKey();
                mydes.IV = GetLegalIV();

                byte[] bin = new byte[100];
                long rdlen = 0;
                long totlen = fin.Length;
                int len;

                ICryptoTransform encrypto = mydes.CreateEncryptor();
                CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
                while (rdlen < totlen)
                {
                    len = fin.Read(bin, 0, 100);
                    cs.Write(bin, 0, len);
                    rdlen = rdlen + len;
                }
                cs.Close();
                fout.Close();
                fin.Close();

            }
            catch (Exception ex)
            {
                throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
            }
        }
        /**//// <summary>
        /// 解密方法File to File
        /// </summary>
        /// <param name="inFileName">待解密文件的路径</param>
        /// <param name="outFileName">待解密后文件的输出路径</param>
        public void Decrypt(string inFileName, string outFileName)
        {
            try
            {
                FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
                FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
                fout.SetLength(0);

                byte[] bin = new byte[100];
                long rdlen = 0;
                long totlen = fin.Length;
                int len;
                mydes.Key = GetLegalKey();
                mydes.IV = GetLegalIV();
                ICryptoTransform encrypto = mydes.CreateDecryptor();
                CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
                while (rdlen < totlen)
                {
                    len = fin.Read(bin, 0, 100);
                    cs.Write(bin, 0, len);
                    rdlen = rdlen + len;
                }
                cs.Close();
                fout.Close();
                fin.Close();

            }
            catch (Exception ex)
            {
                throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
            }
        }

    }

回复

使用道具 举报

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-5-12 22:21:59 | 显示全部楼层
本帖最后由 19信计易称清201 于 2022-5-12 22:32 编辑

namespace TriDES
{
    class Program
    {
        static void Main(string[] args)
        {
            string key = "yayayayayaya";
            TripleDES_ tripleDES_ = new TripleDES_(key);
            Console.WriteLine("需要加密的文件:");
            string inFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
            Console.WriteLine("加密后的文件:");
            string miFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
            Console.WriteLine("解密后的文件:");
            string outFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
            tripleDES_.Encrypt(inFile,miFile);
            tripleDES_.Decrypt(miFile, outFile);
        }
    }
}
回复

使用道具 举报

9

主题

24

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-5-12 22:40:02 | 显示全部楼层
本帖最后由 19信计易称清201 于 2022-5-12 22:47 编辑

图片1,2,3,4,

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 07:55 , Processed in 0.015757 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表