首页 > Windows开发 > 详细

C# PKCS7加密解密

时间:2019-06-12 21:54:53      阅读:232      评论:0      收藏:0      [点我收藏+]
//加密字符串
    public string Encryption(string toE)
    {
        //加密和解密必须采用相同的key,具体自己填写,但是必须为32位
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678123456781234567812345678");
        RijndaelManaged rijndaelManaged = new RijndaelManaged();
        rijndaelManaged.Key = keyArray;
        rijndaelManaged.Mode = CipherMode.ECB;
        rijndaelManaged.Padding = PaddingMode.PKCS7;
        ICryptoTransform icryptoTransform = rijndaelManaged.CreateEncryptor();

        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toE);
        byte[] resultArray = icryptoTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return Convert.ToBase64String( resultArray, 0, resultArray.Length);
    }

    //解密字符串
    public string Decryption(string toD)
    { 
        //加密和解密必须采用相同的key,具体值自己填,但是必须为32位
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678123456781234567812345678");
        RijndaelManaged rijndaelManaged = new RijndaelManaged();
        rijndaelManaged.Key = keyArray;
        rijndaelManaged.Mode = CipherMode.ECB;
        rijndaelManaged.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = rijndaelManaged.CreateDecryptor();

        byte[] toEncryptArray = Convert.FromBase64String(toD);
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

        return UTF8Encoding.UTF8.GetString(resultArray);
    }

 

C# PKCS7加密解密

原文:https://www.cnblogs.com/Fooo/p/11012812.html

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