Ejemplo n.º 1
0
 public void EncryptAndDecryptTest()
 {
     DesHelper des = new DesHelper();
     Assert.AreEqual(des.Decrypt(des.Encrypt("admin")), "admin");
     des = new DesHelper(true);
     Assert.AreEqual(des.Decrypt(des.Encrypt("admin")), "admin");
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 对文件内容进行DES解密,能自动识别并处理是否为分段加密
        /// </summary>
        /// <param name="sourceFile">待加密的文件名</param>
        /// <param name="targetFile">保存加密文件名</param>
        /// <param name="key">密钥字符串,长度为8或者24</param>
        public static void DecryptFile(string sourceFile, string targetFile, string key)
        {
            sourceFile.CheckFileExists("sourceFile");
            targetFile.CheckNotNullOrEmpty("targetFile");
            key.CheckNotNullOrEmpty("key");

            byte[]    keyBytes = Encoding.UTF8.GetBytes(key);
            DesHelper des      = new DesHelper(keyBytes);

            des.DecryptFile(sourceFile, targetFile);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 分段加密文件
        /// </summary>
        /// <param name="sourceFile">待加密的文件名</param>
        /// <param name="targetFile">保存加密文件名</param>
        /// <param name="sectionLength">分段大小(字节)</param>
        /// <param name="key">密钥字符串,长度为8或者24</param>
        public static void EncryptFile(string sourceFile, string targetFile, int sectionLength, string key)
        {
            sourceFile.CheckFileExists("sourceFile");
            targetFile.CheckNotNullOrEmpty("targetFile");
            key.CheckNotNullOrEmpty("key");
            sectionLength.CheckGreaterThan("sectionLength", 0);

            byte[]    keyBytes = Encoding.UTF8.GetBytes(key);
            DesHelper des      = new DesHelper(keyBytes);

            des.EncryptFile(sourceFile, targetFile, sectionLength);
        }
Ejemplo n.º 4
0
        public void DecryptTest()
        {
            string key = "12345678";
            string actual = "TMR29YtnGPI=";
            DesHelper des = new DesHelper(Encoding.UTF8.GetBytes(key));
            Assert.AreEqual(des.Decrypt(actual), "admin");
            Assert.AreEqual(DesHelper.Decrypt(actual, key), "admin");

            key = "!@#$%^&*QWERTYUI12345678";
            actual = "Qp4r67VJ8Z0=";
            des = new DesHelper(Encoding.UTF8.GetBytes(key));
            Assert.AreEqual(des.Decrypt(actual), "admin");
            Assert.AreEqual(DesHelper.Decrypt(actual, key), "admin");
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 加密要发送的数据,包含签名,DES加密,RSA加密DES密钥等步骤
        /// </summary>
        /// <param name="data">要加密的正文明文数据</param>
        /// <returns>已加密待发送的密文</returns>
        public string EncryptData(string data)
        {
            data.CheckNotNull("data");

            //获取正文摘要
            string signData = RsaHelper.SignData(data, _hashType, _ownPrivateKey);
            data = new[] { data, signData }.ExpandAndToString(Separator);
            //使用DES加密 正文+摘要
            DesHelper des = new DesHelper();
            data = des.Encrypt(data);
            //RSA加密DES密钥
            string enDesKey = Convert.ToBase64String(RsaHelper.Encrypt(des.Key, _facePublicKey));
            return new[] { enDesKey, data }.ExpandAndToString(Separator);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 加密要发送的数据,包含签名,DES加密,RSA加密DES密钥等步骤
        /// </summary>
        /// <param name="data">要加密的正文明文数据</param>
        /// <returns>已加密待发送的密文</returns>
        public string EncryptData(string data)
        {
            data.CheckNotNull("data");

            //获取正文摘要
            string signData = RsaHelper.SignData(data, _hashType, _ownPrivateKey);

            data = new[] { data, signData }.ExpandAndToString(Separator);
            //使用DES加密 正文+摘要
            DesHelper des = new DesHelper();

            data = des.Encrypt(data);
            //RSA加密DES密钥
            string enDesKey = Convert.ToBase64String(RsaHelper.Encrypt(des.Key, _facePublicKey));

            return(new[] { enDesKey, data }.ExpandAndToString(Separator));
        }
Ejemplo n.º 7
0
        public void EncryptTest()
        {
            string key = "12345678";
            string actual = "TMR29YtnGPI=";
            DesHelper des = new DesHelper(Encoding.UTF8.GetBytes(key));
            Assert.AreEqual(des.Encrypt("admin"), actual);
            Assert.AreEqual(DesHelper.Encrypt("admin", key), actual);

            //弱密钥
            key = "123456781234567812345678";
            des = new DesHelper(Encoding.UTF8.GetBytes(key));
            ExceptionAssert.IsException<CryptographicException>(() => des.Encrypt("admin"));

            key = "!@#$%^&*QWERTYUI12345678";
            actual = "Qp4r67VJ8Z0=";
            des = new DesHelper(Encoding.UTF8.GetBytes(key));
            Assert.AreEqual(des.Encrypt("admin"), actual);
            Assert.AreEqual(DesHelper.Encrypt("admin", key), actual);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 解密接收到的加密数据并验证完整性,如果验证通过返回明文
        /// </summary>
        /// <param name="data">接收到的加密数据</param>
        /// <returns>解密并验证成功后,返回明文</returns>
        public string DecryptAndVerifyData(string data)
        {
            data.CheckNotNullOrEmpty("data");

            string[] separators = { Separator };
            //0为DES密钥密文,1为 正文+摘要 的密文
            string[] datas = data.Split(separators, StringSplitOptions.None);
            //用接收端私钥RSA解密获取DES密钥
            byte[] desKey = RsaHelper.Decrypt(Convert.FromBase64String(datas[0]), _ownPrivateKey);
            //DES解密获取 正文+摘要 的明文
            data = new DesHelper(desKey).Decrypt(datas[1]);
            //0为正文明文,1为摘要
            datas = data.Split(separators, StringSplitOptions.None);
            data  = datas[0];
            if (RsaHelper.VerifyData(data, datas[1], _hashType, _facePublicKey))
            {
                return(data);
            }
            throw new CryptographicException("加密数据在进行解密时校验失败");
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 解密接收到的加密数据并验证完整性,如果验证通过返回明文
        /// </summary>
        /// <param name="data">接收到的加密数据</param>
        /// <returns>解密并验证成功后,返回明文</returns>
        public string DecryptAndVerifyData(string data)
        {
            data.CheckNotNullOrEmpty("data");

            string[] separators = { Separator };
            //0为DES密钥密文,1为 正文+摘要 的密文
            string[] datas = data.Split(separators, StringSplitOptions.None);
            //用接收端私钥RSA解密获取DES密钥
            byte[] desKey = RsaHelper.Decrypt(Convert.FromBase64String(datas[0]), _ownPrivateKey);
            //DES解密获取 正文+摘要 的明文
            data = new DesHelper(desKey).Decrypt(datas[1]);
            //0为正文明文,1为摘要
            datas = data.Split(separators, StringSplitOptions.None);
            data = datas[0];
            if (RsaHelper.VerifyData(data, datas[1], _hashType, _facePublicKey))
            {
                return data;
            }
            throw new CryptographicException("加密数据在进行解密时校验失败");
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 对文件内容进行DES解密,能自动识别并处理是否为分段加密
        /// </summary>
        /// <param name="sourceFile">待加密的文件名</param>
        /// <param name="targetFile">保存加密文件名</param>
        /// <param name="key">密钥字符串,长度为8或者24</param>
        public static void DecryptFile(string sourceFile, string targetFile, string key)
        {
            sourceFile.CheckFileExists("sourceFile");
            targetFile.CheckNotNullOrEmpty("targetFile");
            key.CheckNotNullOrEmpty("key");

            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            DesHelper des = new DesHelper(keyBytes);
            des.DecryptFile(sourceFile, targetFile);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 分段加密文件
        /// </summary>
        /// <param name="sourceFile">待加密的文件名</param>
        /// <param name="targetFile">保存加密文件名</param>
        /// <param name="sectionLength">分段大小(字节)</param>
        /// <param name="key">密钥字符串,长度为8或者24</param>
        public static void EncryptFile(string sourceFile, string targetFile, int sectionLength, string key)
        {
            sourceFile.CheckFileExists("sourceFile");
            targetFile.CheckNotNullOrEmpty("targetFile");
            key.CheckNotNullOrEmpty("key");
            sectionLength.CheckGreaterThan("sectionLength", 0);

            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            DesHelper des = new DesHelper(keyBytes);
            des.EncryptFile(sourceFile, targetFile, sectionLength);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// 解密字符串,输入BASE64编码字符串
 /// </summary>
 /// <param name="source">要解密的BASE64编码字符串</param>
 /// <param name="key">密钥字符串,长度为8或者24</param>
 /// <returns>解密的明文字符串</returns>
 public static string Decrypt(string source, string key)
 {
     source.CheckNotNullOrEmpty("source");
     key.CheckNotNullOrEmpty("key");
     byte[] keyBytes = Encoding.UTF8.GetBytes(key);
     DesHelper des = new DesHelper(keyBytes);
     return des.Decrypt(source);
 }
Ejemplo n.º 13
0
 /// <summary>
 /// 解密字节数组
 /// </summary>
 /// <param name="source">要解密的字节数组</param>
 /// <param name="key">密钥字节数组,长度为8或者24</param>
 /// <returns>解密后的字节数组</returns>
 public static byte[] Decrypt(byte[] source, byte[] key)
 {
     DesHelper des = new DesHelper(key);
     return des.Decrypt(source);
 }
Ejemplo n.º 14
0
        /// <summary>
        /// 解密字节数组
        /// </summary>
        /// <param name="source">要解密的字节数组</param>
        /// <param name="key">密钥字节数组,长度为8或者24</param>
        /// <returns>解密后的字节数组</returns>
        public static byte[] Decrypt(byte[] source, byte[] key)
        {
            DesHelper des = new DesHelper(key);

            return(des.Decrypt(source));
        }