【问题标题】:How to encrypt a byte array with Crypto++如何使用 Crypto++ 加密字节数组
【发布时间】:2013-11-17 19:36:51
【问题描述】:

如何使用 Crypto++ 的 RSA 实现加密字节数组?我已经找到了一个字符串的例子。但我找不到一个很好的例子,如何对字节数组做同样的事情。

这是我的第一次尝试:

//dataSize:     Size of data that is going to be send
//dataToSend    Bytes to send to the user
//seedPool      is an AutoSeededRandomPool

CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(publicKey);

int size = 64000; 
byte * cipher = new byte(size);

CryptoPP::ArraySink* test = new CryptoPP::ArraySink(cipher, size);
CryptoPP::ArraySource as((byte*)dataToSend, dataSize, true, new CryptoPP::PK_EncryptorFilter(seedPool, encryptor, test));

int newDataSize = test->TotalPutLength();
unsigned int bytesSend = ::send(socketLink, (char *)(cipher), (int)newDataSize, 0);

delete[] cipher;

这不起作用。 TotalPutLength 将始终返回 0,但密码中有数据。

什么是实现此功能的安全方法?我不想受到缓冲区溢出或任何其他攻击的影响。

【问题讨论】:

    标签: c++ arrays encryption crypto++


    【解决方案1】:

    字节 * 密码 = 新字节(大小);

    我认为应该是这样的:

    byte * cipher = new byte[size];
    

    否则,我认为您会将一个字节初始化为 6400(被截断为 0x00)。


    CryptoPP::ArraySink * test = new CryptoPP::ArraySink(cipher, size);

    这有点不同。如果您愿意,您可以远离内存管理器:

     CryptoPP::ArraySink test(cipher, size);
    

    int newDataSize = test->TotalPutLength();

    我从未使用过TotalPutLength,也没有看到它记录在BufferedTransformation 或Sink 上。所以我对它的回报没有任何建议。

    TotalPutLength 可以使用。 ArraySink 可能会返回错误值如果接收器已满。如果数组是固定的并且对于所有数据来说太小,就会发生这种情况。我们在 Crypto++ 5.6.3 或 5.6.4 中清除了这个问题。

    如果你想计算处理的字节数(即使接收器无法存储它们的字节),那么你也可以使用MeterFilter:

    byte data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
    
    string encoded;
    MeterFilter meter( new StringSink( encoded ) );
    
    ArraySource( data, sizeof( data ), true,
        new HexEncoder(
            new Redirector( meter ),
            true /*UCase*/, 2 /*Group*/,
            " " /*Separator*/
        )
    );
    
    cout << "processed " << meter.GetTotalBytes() << " bytes" << endl;
    cout << encoded << endl;
    

    输出:

    Processed 23 bytes
    00 01 02 03 04 05 06 07
    

    如何使用 Cryptopp RSA 实现加密字节数组

    现在我们正在讨论 ;) 从 Crypto++ wiki 上的 RSA Encryption 试试这个。

    ////////////////////////////////////////////////
    // Generate keys
    AutoSeededRandomPool rng;
    
    InvertibleRSAFunction params;
    params.GenerateRandomWithKeySize( rng, 1536 );
    
    RSA::PrivateKey privateKey( params );
    RSA::PublicKey publicKey( params );
    
    string plain="RSA Encryption", cipher, recovered;
    
    ////////////////////////////////////////////////
    // Encryption
    RSAES_OAEP_SHA_Encryptor e( publicKey );
    
    StringSource ss1( plain, true,
        new PK_EncryptorFilter( rng, e,
            new StringSink( cipher )
        ) // PK_EncryptorFilter
     ); // StringSource
    
    ////////////////////////////////////////////////
    // Decryption
    RSAES_OAEP_SHA_Decryptor d( privateKey );
    
    StringSource ss2( cipher, true,
        new PK_DecryptorFilter( rng, d,
            new StringSink( recovered )
        ) // PK_DecryptorFilter
     ); // StringSource
    
    assert( plain == recovered );
    

    【讨论】:

      猜你喜欢
      • 2017-08-06
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      相关资源
      最近更新 更多