.Net AES、RSA 加密解密

     

           2019就这样过完了,好像还有很多事情没来得及做,然后就过去了,整的还有很多遗憾,把握住2020的每一天时间吧,望你们的2020年不要浑浑噩噩,要忙忙碌碌、基础、学习、创新、记录、总结、生活都要前进向前看。过程重要结果更重要,不希望N年之后想想之前怎么不努力,薪水怎么不加,这些都是有原因的, 机遇都是给准备着的人的,总感觉这句话有问题,我时刻都准备着呢,机遇咋就是不给我,证明我还是不够努力不够上进呗。加油吧 小姑娘你的人生还很长,驾照还没考,更好的美食还没吃,薪水还没加,钱包还没满,世界各地去的还少,闺蜜还没嫁    、花呗还没还、房子还没买、车还没买,这么多还没做完的事情,你有什么

 颜色相同的代表一对公私钥,本图涉及到四套证书,浅绿浅粉块的是自己的两套证书,浅黄浅蓝第三方证书。

RSA加密

    /// <summary>
        /// RSA 公钥加密
        /// </summary>
        /// <param name="xmlPublicteKey">公钥</param>
        /// <param name="encryptKey">随机生成的Key</param>
        /// <returns></returns>
        public static string RSAEncryptByPublicteKey( string encryptKey)
        {
            RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider();
            publicRsa.FromXmlString(xmlPublicteKey);
            RSAParameters rp = publicRsa.ExportParameters(false);

            //转换密钥
            AsymmetricKeyParameter pbk = DotNetUtilities.GetRsaPublicKey(rp);
            IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/NoPadding");// 参数与Java中加密解密的参数一致     //RSA/ECB/PKCS1Padding
            //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥
            c.Init(true, pbk); //keyPair.Private

            byte[] DataToEncrypt = Encoding.UTF8.GetBytes(encryptKey);
            byte[] outBytes = c.DoFinal(DataToEncrypt);//加密
            string strBase64 = Convert.ToBase64String(outBytes);

            return strBase64;
        }
View Code

RSA解密

 /// <summary>
        /// 私钥解密
        /// </summary>
        /// <param name="xmlPrivateKey"></param>
        /// <param name="encryptKey"></param>
        /// <returns></returns> 
        public static string RSAEncryptByPrivateKey( string encryptKey)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(xmlPrivateKey);
            RSAParameters rp = rsa.ExportParameters(true);

            //转换密钥
            AsymmetricCipherKeyPair pbk = DotNetUtilities.GetRsaKeyPair(rp);
            IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/NoPadding");// 参数与Java中加密解密的参数一致     
            //RSA/ECB/PKCS1Padding
            //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥
            c.Init(false, pbk.Private); //keyPair.Private

            byte[] PlainTextBArray = Convert.FromBase64String(encryptKey);
            byte[] outBytes = c.DoFinal(PlainTextBArray);//加密
            string outresp = System.Text.UTF8Encoding.UTF8.GetString(outBytes);

            return outresp;
        }
View Code

AES加密

   /// <summary>
        /// AES加密 
        /// </summary>
        /// <param name="text">加密字符</param>
        /// <param name="password">加密的密码</param>
        /// <returns></returns>
        public static string AESEncrypt(string text, string password)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.ECB;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 128;
            rijndaelCipher.BlockSize = 128;
            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
            byte[] keyBytes = new byte[16];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length) len = keyBytes.Length;
            System.Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            byte[] ivBytes = System.Text.Encoding.GetEncoding("UTF-8").GetBytes("0102030405060708");
            rijndaelCipher.IV = new byte[16];
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
            byte[] plainText = Encoding.UTF8.GetBytes(text);
            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
            return Convert.ToBase64String(cipherBytes);

        }
View Code

AES解密

   /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="text"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string AESDecrypt(string text, string password)
        {

            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.ECB;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 128;
            rijndaelCipher.BlockSize = 128;
            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
            byte[] keyBytes = new byte[16];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length) len = keyBytes.Length;
            System.Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            byte[] ivBytes = System.Text.Encoding.GetEncoding("UTF-8").GetBytes("0102030405060708");
            rijndaelCipher.IV = new byte[16];
            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
            //byte[] plainText = Encoding.UTF8.GetBytes(text);
            byte[] plainText = Convert.FromBase64String(text);
            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
            return Encoding.UTF8.GetString(cipherBytes);

        }
View Code

猜你喜欢

转载自www.cnblogs.com/BabyRui/p/12168171.html