C#encrypt和Crypto ++解密

时间:2016-08-11 13:44:22

标签: c# c++ encryption aes crypto++

#include <iostream>
#include <iomanip>

#include"modes.h"   
#include "aes.h"
#include "filters.h"

int main(int argc, char* argv[]) {

    //Key and IV setup
    //AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-   
    //bit). This key is secretly exchanged between two parties before communication   
    //begins. DEFAULT_KEYLENGTH= 16 bytes
    byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ] = {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};
    byte iv[ CryptoPP::AES::BLOCKSIZE ]={0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};

    //Now is the time for all good men to come to the aide...";
    //std::string plaintext="{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}";
    std::string ciphertext;
    std::string decryptedtext;

    //
    // Dump Plain Text
    //
    std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
    std::cout << plaintext;
    std::cout << std::endl << std::endl;

    //
    // Create Cipher Text
    //
    CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );

    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
    stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() + 1 );
    stfEncryptor.MessageEnd();

    //
    // Dump Cipher Text
    //
    std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

    for( int i = 0; i < ciphertext.size(); i++ ) {

        std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
    }

    std::cout << std::endl << std::endl;

    //
    // Decrypt
    //
    CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
    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;

    return 0;
}

请在Getting different output when using AES encryption in c# and online tool检查修订后的C#代码。

理想情况下,我想使用c ++(crypto ++)解密c#的加密输出。 但是在用c ++测试加密和解密时,我在加密本身的输出并不像c#那样。

以下是我的示例代码。 我的输出是:

Plain Text (51 bytes)
{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}
Cipher Text (64 bytes)
0x36 0xe7 0x78 0xf8 0xb8 0x97 0x15 0x6c 0xc3 0x73 0xea 0xa8 0x1b 0x12 0x71 0xc2 0xa0 0x5a 0xf2 0x40 0x60 0x89 0x6b 0x8 0x70 0x90 0xc9 0xb6 0x75 0x57 0xf7 0x22 0x73 0x3d 0x15 0xab 0xb1 0xd5 0xe5 0x73 0x85 0xe 0xa5 0x7e 0xa9 0xd2 0x7c 0xf2 0x87 0x83 0xac 0x79 0xc3 0x13 0xb7 0x89 0xfc 0x63 0x68 0x75 0x99 0x87 0x37 0xab
Decrypted Text:
{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}

0 个答案:

没有答案