码迷,mamicode.com
首页 > 其他好文 > 详细

CS DES任意长度密钥加密

时间:2021-04-01 12:53:38      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:lock   tor   transform   encoding   pre   byte   hash   base   har   

CS DES任意长度密钥加密

        private static string Encrypt2(string str, string sKey)
        {
            string s = "";
            using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider())
            {
                using (System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 = new System.Security.Cryptography.Rfc2898DeriveBytes(sKey, new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(sKey))))
                {
                    des.Key = rfc2898.GetBytes(des.KeySize / 8);
                    des.IV = rfc2898.GetBytes(des.BlockSize / 8);
                }
                byte[] input = Encoding.Default.GetBytes(str);
                s = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(input, 0, input.Length));
            }
            return s;
        }
        private string DecryptByDES(string encrypted, string key, string iv)
        {
            string s = "";
            using (System.Security.Cryptography.DESCryptoServiceProvider des =
                new System.Security.Cryptography.DESCryptoServiceProvider())
            {
                using (System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
                    new System.Security.Cryptography.Rfc2898DeriveBytes(key,
                        new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(
                            Encoding.Default.GetBytes(key))))
                {
                    des.Key = rfc2898.GetBytes(des.KeySize / 8);
                    des.IV = rfc2898.GetBytes(des.BlockSize / 8);
                }
                byte[] input = Convert.FromBase64String(encrypted);
                s = Encoding.Default.GetString(des.CreateDecryptor().TransformFinalBlock(input, 0, input.Length));
            }
            return s;
        }

技术图片

CS DES任意长度密钥加密

标签:lock   tor   transform   encoding   pre   byte   hash   base   har   

原文地址:https://www.cnblogs.com/honk/p/14600625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!