zeros_Padding结果不同的输出

时间:2015-05-13 06:07:49

标签: c# qt qt5 crypto++

为什么会出错? 它不是由crypto ++支持的pkcs7吗? 我想知道结果的价值就像做什么。 Iv值等于所谓的良好交付。

else

下面是qt5代码。 省略细节。

QT代码

// c# code
private byte[] _iv;
private readonly string key = "7794b12op901252bfcea66d6f0521212";
public string decrypt(string Input)
{
    string str = "";
    RijndaelManaged managed = new RijndaelManaged();
    managed.KeySize = 128;
    managed.BlockSize = 128;
    managed.Mode = CipherMode.CBC;
    managed.Padding = PaddingMode.Zeros;
    managed.Key = Encoding.UTF8.GetBytes(this.key);
    managed.IV = this._iv;
    try
    {
        ICryptoTransform transform = managed.CreateDecryptor();
        byte[] bytes = null;
        using (MemoryStream stream = new MemoryStream())
        {
            using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write))
            {
                byte[] buffer = Convert.FromBase64String(Input);
                stream2.Write(buffer, 0, buffer.Length);
            }
            bytes = stream.ToArray();
        }
        str = Encoding.ASCII.GetString(bytes);
    }
    catch (Exception)
    {
    }
    return str;
}
public string encrypt(string Input)
{
    RijndaelManaged managed = new RijndaelManaged();
    managed.KeySize = 128;
    managed.BlockSize = 128;
    managed.Mode = CipherMode.CBC;
    managed.Padding = PaddingMode.Zeros;
    managed.Key = Encoding.ASCII.GetBytes(this.key);
    managed.GenerateIV();
    this._iv = managed.IV;
    ICryptoTransform transform = managed.CreateEncryptor(managed.Key, managed.IV);
    byte[] inArray = null;
    using (MemoryStream stream = new MemoryStream())
    {
        using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write))
        {
            byte[] bytes = Encoding.UTF8.GetBytes(Input);
            stream2.Write(bytes, 0, bytes.Length);
        }
        inArray = stream.ToArray();
    }
    return Convert.ToBase64String(inArray);
}

2 个答案:

答案 0 :(得分:1)

我真的不明白你的问题是什么,我不能真正评论,所以我想这里:

ICryptoTransform transform = managed.CreateEncryptor(managed.Key, managed.IV);
ICryptoTransform transform = managed.CreateDecryptor();

两者都需要键和IV,或者至少需要相同....

然后你使用了Rijndael然后AES。您也可以在C#中使用AES。

答案 1 :(得分:0)

有几件事情跳出来......在C#代码中,你这样做:

private readonly string key = "7794b12op901252bfcea66d6f0521212";
...
managed.Key = Encoding.UTF8.GetBytes(this.key);

在Crypto ++代码中,您可以这样做:

QString aeskey = "7794b12op901252bfcea66d6f0521212";
...
string key = aeskey.toStdString();

你需要HexDecode Crypto ++中的字符串。

此外,GenerateIV Base64在Qt方面编码:

AutoSeededRandomPool rnd;
byte iv3[AES::BLOCKSIZE];
rnd.GenerateBlock(iv3, AES::BLOCKSIZE);
QByteArray out((char*)iv3, AES::BLOCKSIZE);
_iv = out.toBase64();

但C#使用byte[](可能 Base64编码):

private byte[] _iv;