Zeros_Padding Result: Result

Why is he wrong? Is it not pkcs7 supported by crypto ++? I would like to know the value of the result as something to be done. The Iv is equal to the estimated delivered.

// 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);
}

      

Below is the qt5 code. Omit details.

QT code

    QString aeskey = "7794b12op901252bfcea66d6f0521212";
    QString _iv;
void Cipher::GenerateIV()
{
    AutoSeededRandomPool rnd;
    byte iv3[AES::BLOCKSIZE];
    rnd.GenerateBlock(iv3, AES::BLOCKSIZE);
    QByteArray out((char*)iv3, AES::BLOCKSIZE);
    _iv = out.toBase64();
}
QString Cipher::AESencrypt(QString Qstr_in)
{
    string str_in = Qstr_in.toStdString();
    string key = aeskey.toStdString();
    GenerateIV();
    string iv = _iv.toStdString();
    string str_out;
    CBC_Mode<AES>::Encryption encryption;
    encryption.SetKeyWithIV((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
    StringSource encryptor(str_in, true,
                 new StreamTransformationFilter(encryption,
                 new Base64Encoder(
                 new StringSink(str_out)
//                ,StreamTransformationFilter::PKCS_PADDING
                ,StreamTransformationFilter::ZEROS_PADDING
           )
        )
    );

    return QString::fromStdString(str_out);
}
QString Cipher::AESdecrypt(QString Qstr_in)
{
    string str_in = Qstr_in.toStdString();
    string key = aeskey.toStdString();
    string iv = _iv.toStdString();
    string str_out;
    CBC_Mode<AES>::Decryption decryption;
    decryption.SetKeyWithIV((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
    StringSource decryptor(str_in, true,
                 new Base64Decoder(
                 new StreamTransformationFilter(decryption,
                 new StringSink(str_out)
//                ,StreamTransformationFilter::PKCS_PADDING
                ,StreamTransformationFilter::DEFAULT_PADDING
            )
        )
    );
    return QString::fromStdString(str_out);
}

      

+3


source to share


2 answers


I don't understand what your question is, and I cannot comment like this here on what I think:

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

      



Both require a key and an IV, or at least must be the same ....

Then you used Rijndael once, then AES. You can use AES in C # too.

+1


source


A few things jump out ... In C # code, you do this:

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

      

In Crypto ++ code, you do this:

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

      

You need a HexDecode string in Crypto ++.




Also, GenerateIV

Base64 encodes things on the Qt side:

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

      

But C # uses byte[]

(presumably not Base64 encoded):

private byte[] _iv;

      

0


source







All Articles