简体   繁体   English

如何使用Crypto ++加密字节数组

[英]How to encrypt a byte array with Crypto++

How can I encrypt a byte array with Crypto++'s RSA implementation? 如何使用Crypto ++的RSA实现加密字节数组? I already found an example for strings. 我已经找到了一个字符串示例。 But I can't find a good example how to do the same for a byte array. 但我找不到一个很好的例子,如何对字节数组做同样的事情。

This is my first attempt: 这是我的第一次尝试:

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

This doesn't work. 这不起作用。 TotalPutLength will always return 0 but there is data put in cipher. TotalPutLength将始终返回0但有数据以密码形式输入。

What is a safe way to implement this? 实现这个的安全方法是什么? I don't want to be vulnerable for buffer overflows or any other attack. 我不希望因缓冲区溢出或任何其他攻击而受到攻击。

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

I believe this should be: 我相信这应该是:

byte * cipher = new byte[size];

Otherwise, I think you get one byte initialized to 6400 (which is truncated to 0x00). 否则,我认为你将一个字节初始化为6400(被截断为0x00)。


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

This is kind of different. 这有点不同。 You can stay out of the memory manager if you'd like: 如果您愿意,可以不要使用内存管理器:

 CryptoPP::ArraySink test(cipher, size);

int newDataSize = test->TotalPutLength(); int newDataSize = test-> TotalPutLength();

I've never used TotalPutLength , and I did not see it documented on a BufferedTransformation or Sink . 我从未使用过 TotalPutLength ,我没有在 BufferedTransformationSink上看到它。 So I don't really have any advice on what its returning. 所以我对它的回归没有任何建议。

TotalPutLength is OK to use. TotalPutLength可以使用。 An ArraySink could return the wrong value if the sink was full. 如果接收器已满, ArraySink可能返回错误的值。 It would happen if the array was fixed and too small for all the data. 如果阵列是固定的并且对于所有数据而言太小,则会发生这种情况。 We cleared that issue at Crypto++ 5.6.3 or 5.6.4. 我们在Crypto ++ 5.6.3或5.6.4中清除了该问题。

If you want to count the number of bytes processed (even if the sink cannot store they bytes), then you can also use a MeterFilter : 如果要计算处理的字节数(即使接收器不能存储它们的字节),那么您也可以使用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;

Output: 输出:

Processed 23 bytes
00 01 02 03 04 05 06 07

How can you encrypt a byte array with Cryptopp RSA implementation 如何使用Cryptopp RSA实现加密字节数组

Now we're talking ;) Try this from the Crypto++ wiki on RSA Encryption . 现在我们正在讨论;)在RSA加密的Crypto ++ wiki上尝试这个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM