如何使用Crypto ++加密字节数组 [英] How to encrypt a byte array with Crypto++

查看:595
本文介绍了如何使用Crypto ++加密字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I encrypt a byte array with Crypto++'s RSA implementation? I already found an example for strings. But I can't find a good example how to do the same for a byte array.

这是我的第一个尝试:

//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,但是存在加密的数据。

This doesn't work. TotalPutLength will always return 0 but there is data put in cipher.

什么是安全的方法来实现?我不想对缓冲区溢出或任何其他攻击感到脆弱。

What is a safe way to implement this? I don't want to be vulnerable for buffer overflows or any other attack.

推荐答案


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

byte * cipher = new byte(size);

我相信这应该是:

byte * cipher = new byte[size];

否则,我想你会将一个字节初始化为6400(被截断为0x00) p>




Otherwise, I think you get one byte initialized to 6400 (which is truncated to 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();

我从未使用过 TotalPutLength ,我没有看到它在 BufferedTransformation Sink 中记录。所以我真的没有任何关于它返回的建议。

I've never used TotalPutLength, and I did not see it documented on a BufferedTransformation or Sink. So I don't really have any advice on what its returning.

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

TotalPutLength is OK to use. An ArraySink could return the wrong value if the sink was full. 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.

如果要计算处理的字节数(即使接收器不能存储它们的字节),那么您还可以使用 MeterFilter

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:

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实现加密字节数组

How can you encrypt a byte array with Cryptopp RSA implementation

现在我们正在说话;)从Crypto ++ wiki上 RSA Encryption p>

Now we're talking ;) Try this from the Crypto++ wiki on 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 );

这篇关于如何使用Crypto ++加密字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆