Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zeros_Padding result different output

Tags:

c#

qt

qt5

crypto++

Why it does get wrong results? It not pkcs7 supported by the crypto ++? I would like to know the value of the result to be like what to do. Iv value is equal to the supposed well-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 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);
}
like image 884
Brian Avatar asked Apr 12 '26 03:04

Brian


1 Answers

I don't understand really what your question is and I can't really comment so here what I think:

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

Both need key and IV, or at least need to be the same....

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

like image 76
SamY Avatar answered Apr 14 '26 19:04

SamY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!