首页 > 其他 > 详细

CS DES任意长度密钥加密

时间:2021-03-31 14:00:21      阅读:13      评论:0      收藏:0      [点我收藏+]

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任意长度密钥加密

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!