首页 文章

解密导致Rijndael和256块大小的乱码[重复]

提问于
浏览
1

这个问题在这里已有答案:

所以我有一些用旧代码加密的旧邮件:

'the ecryption algorithm with specific settings
        Dim myRijndael As New RijndaelManaged
        myRijndael.Padding = PaddingMode.Zeros
        myRijndael.Mode = CipherMode.CBC
        myRijndael.KeySize = 256
        myRijndael.BlockSize = 256

        'declared byte arrays for the message string and the key and IV for the encryption algorithm
        Dim encrypted() As Byte
        Dim toEncrypt() As Byte
        Dim key() As Byte
        Dim IV() As Byte

        'populating the arryas with the needed bytes for the encryption algorithm
        key = System.Text.Encoding.ASCII.GetBytes(prm_key)
        IV = System.Text.Encoding.ASCII.GetBytes(prm_iv)

        'the actual instance of the ecryption algorithm
        Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)

        'streams for the encrypted byte array
        Dim msEncrypt As New MemoryStream()
        Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

        toEncrypt = System.Text.Encoding.ASCII.GetBytes(text_to_encrypt)

        csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)

        'make sure we have all the blocks
        csEncrypt.FlushFinalBlock()

        'turn encrypted stream into byte array
        encrypted = msEncrypt.ToArray()
        'return a base 64 string so we can upload it to the server
        Return (Convert.ToBase64String(encrypted))

我试图用Crypto解密它

这是我提出的代码:

std::string coded = "pCyWPA5Enc3F0NAkowrt206brSfMrOgKMTXI1pKhCUY=";
//std::string coded = "F9uvtbK3Ue67Gbe9si5yvDn8a50bYnTovjfWali+Xjo=";

std::string coded2;
std::string ciphertext;
std::string decryptedtext;


CryptoPP::StringSource sss(coded, true,
   new CryptoPP::Base64Decoder(
       new CryptoPP::StringSink(ciphertext)
   ) // Base64Decoder
); // StringSource
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ),CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING);
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();

//
// Dump Decrypted Text
//
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;

但我得到的回报都是胡言乱语 . 填充模式设置,键正确 . 我没有想法 .

注释掉的“编码”字符串实际上已被解密,但它是用c和crypto加密的 . 文字完全相同 . 那么为什么Base64加密蜇不同?

我想可能因为VB代码加密了ASCII字节,这可能是问题所在 . 但我不知道如何使用加密使用ASCII编码 .

有人可以在这里指出问题吗?

1 回答

  • 1

    AES使用128位块大小 . VB代码使用具有256位块大小的Rijndael .

相关问题