明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

C# PKCS7加密解密

Posted on 2019-06-12 21:24  且行且思  阅读(3043)  评论(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);
    }