码迷,mamicode.com
首页 > Web开发 > 详细

.Net AES、RSA 加密解密

时间:2020-01-08 19:07:49      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:image   key   mic   util   sys   form   世界   init   none   

     

           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

.Net AES、RSA 加密解密

标签:image   key   mic   util   sys   form   世界   init   none   

原文地址:https://www.cnblogs.com/BabyRui/p/12168171.html

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