C# PKCS7加密解密

//加密字符串
    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);
    }
相關文章
相關標籤/搜索