其他分享
首页 > 其他分享> > CS DES任意长度密钥加密

CS DES任意长度密钥加密

作者:互联网

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;
        }

Console

标签:des,string,Cryptography,DES,System,密钥,CS,Security,GetBytes
来源: https://www.cnblogs.com/honk/p/14600625.html