使用rijndeal算法解密会产生填充错误 [英] decryption with rijndeal algorithm gives padding error

查看:107
本文介绍了使用rijndeal算法解密会产生填充错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨......

我正在使用rijndeal来加密和解密一些数据!但是它给了我这个错误:

hi...
i am using rijndeal to encrypt and decrypt some data! but it gives me this error :

Padding is invalid and cannot be removed.



i搜索很多但没什么帮我解决这个错误!这是我的加密/解密代码:




i searched much but nothing help me to solve this error! tis is my encrypt/decrypt codes:

public string Encrypt(string text)
    {

        mainRM = new System.Security.Cryptography.RijndaelManaged();
        mainRM.BlockSize = 256;
        mainRM.KeySize = 256;
        memorystream = new System.IO.MemoryStream();
        ICryptoTransform icrypt = mainRM.CreateEncryptor(key, iv);
        CryptoStream cryptstream = new CryptoStream(memorystream, icrypt, CryptoStreamMode.Write);
        cryptstream.FlushFinalBlock();
        System.IO.StreamWriter sw = new System.IO.StreamWriter(cryptstream);
        sw.Write(text);
        return Convert.ToBase64String(memorystream.ToArray());
    }

    public string Decrypt(string CryptedText)
    {
        string custinfo;
        System.IO.StreamReader streamreader;
        mainRM = new RijndaelManaged();
        mainRM.BlockSize = 256;
        mainRM.KeySize = 256;
        memorystream = new System.IO.MemoryStream(Convert.FromBase64String(CryptedText));
        ICryptoTransform icrypt = mainRM.CreateDecryptor(key, iv);
        memorystream.Position = 0;
        CryptoStream cryptstream = new CryptoStream(memorystream, icrypt, CryptoStreamMode.Read);
        cryptstream.FlushFinalBlock();
        streamreader = new System.IO.StreamReader(cryptstream);
        custinfo = streamreader.ReadToEnd();
        return custinfo;
    }





有人可以帮我吗?



can anyone help me?

推荐答案

Rijndael / AES是一个块密码。它以128位(16个字符)块加密数据。加密填充用于确保消息的最后一个块始终是正确的大小。

您需要明确设置加密和解密的填充。

例如:

Rijndael/AES is a block cypher. It encrypts data in 128 bit (16 character) blocks. Cryptographic padding is used to make sure that last block of the message is always the correct size.
You need to explicitly set the padding for both encryption and decryption.
For example:
mainRM.Padding = PaddingMode.None;





这里'我几个月前在一个项目中使用过的一些代码。它会完成这项工作:





Here''s some code I used in a project a few months ago. It''ll do the job:

public static string Encrypt(string toEncrypt, string securityKey)
{
    var key = securityKey;
    var keyArray = Encoding.UTF8.GetBytes(key);

    var tdes = new TripleDESCryptoServiceProvider
    {
        Key = keyArray,
        Mode = CipherMode.ECB,
        Padding = PaddingMode.PKCS7
    };

    var cTransform = tdes.CreateEncryptor();
    var toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
    var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    tdes.Clear();
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

public static string Decrypt(string cipherString, string securityKey)
{
    var key = securityKey;
    var keyArray = Encoding.UTF8.GetBytes(key);

    var tdes = new TripleDESCryptoServiceProvider
    {
        Key = keyArray,
        Mode = CipherMode.ECB,
        Padding = PaddingMode.PKCS7
    };

    var cTransform = tdes.CreateDecryptor();
    var toEncryptArray = Convert.FromBase64String(cipherString);
    var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    tdes.Clear();
    return Encoding.UTF8.GetString(resultArray);
}


这篇关于使用rijndeal算法解密会产生填充错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆