DES\RSA加密解密简单用法



    <summary> 
    /// DES加密解密算法。 
    /// </summary> 
    public static class DESEncrypt
    {
        /**/
        /// <summary> 
        /// 对称加密解密的密钥 
        /// </summary> 
        public static string EncryptKey = "12345678";//只能8位


        /**/
        /// <summary> 
        /// DES加密 
        /// </summary> 
        /// <param name="encryptString"></param> 
        /// <returns></returns> 
        public static string Encrypt(string encryptString)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(EncryptKey);
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }


        /**/
        /// <summary> 
        /// DES解密 
        /// </summary> 
        /// <param name="decryptString"></param> 
        /// <returns></returns> 
        public static string Decrypt(string decryptString)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(EncryptKey);
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
    }


    /// <summary>
    /// RSA加密解密算法
    /// </summary>
    public static class RSAEncrypt
    {
        /**/
        /// <summary> 
        /// 对称加密解密的密钥 
        /// </summary> 
        public static string EncryptKey = "123465654";
        public static CspParameters param;


        /**/
        /// <summary> 
        /// DES加密 
        /// </summary> 
        /// <param name="encryptString"></param> 
        /// <returns></returns> 
        public static string Encrypt(string encryptString)
        {
            param = new CspParameters();
            param.KeyContainerName = EncryptKey;//密匙容器的名称,保持加密解密一致才能解密成功
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindata = Encoding.Default.GetBytes(encryptString);//将要加密的字符串转换为字节数组
                byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据转换为新的加密字节数组
                return Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串
            }
        }


        /**/
        /// <summary> 
        /// 解密 
        /// </summary> 
        /// <param name="decryptString"></param> 
        /// <returns></returns> 
        public static string Decrypt(string decryptString)
        {
            param = new CspParameters();
            param.KeyContainerName = EncryptKey;
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdata = Convert.FromBase64String(decryptString);
                byte[] decryptdata = rsa.Decrypt(encryptdata, false);
                return Encoding.Default.GetString(decryptdata);
            }
        }
    }
OpenSSL是一个广泛使用的开放源代码工具包,提供加密和解密功能。它支持DESRSA等加密算法,可以用来加密和解密数据。DES是一种对称加密算法,使用相同的密钥进行加密和解密。RSA是一种非对称加密算法,使用公钥进行加密,私钥进行解密。 使用OpenSSL进行DES加密和解密,首先需要生成一个DES密钥,然后使用该密钥进行加密和解密操作。可以使用以下命令生成DES密钥: ``` openssl rand -out des.key 8 ``` 生成的密钥保存在des.key文件中。然后使用该密钥进行加密和解密: ``` openssl enc -des -in plaintext.txt -out encrypted.des -kfile des.key openssl enc -d -des -in encrypted.des -out decrypted.txt -kfile des.key ``` 上述命令分别用指定的DES密钥对明文文件plaintext.txt进行加密,然后将加密结果保存到encrypted.des文件中。解密操作则相反,使用相同的DES密钥对加密后的文件进行解密,得到明文文件decrypted.txt。 而要使用RSA算法进行加密和解密,首先需要生成RSA密钥对(公钥和私钥),然后使用公钥进行加密,私钥进行解密。可以使用以下命令生成RSA密钥对: ``` openssl genrsa -out private.pem 1024 openssl rsa -in private.pem -pubout -out public.pem ``` 生成的私钥保存在private.pem文件中,公钥保存在public.pem文件中。然后使用公钥进行加密,私钥进行解密: ``` openssl rsautl -encrypt -in plaintext.txt -out encrypted.rsa -inkey public.pem openssl rsautl -decrypt -in encrypted.rsa -out decrypted.txt -inkey private.pem ``` 上述命令分别使用指定的公钥对明文文件plaintext.txt进行加密,然后将加密结果保存到encrypted.rsa文件中。解密操作则相反,使用相同的私钥对加密后的文件进行解密,得到明文文件decrypted.txt。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值