What is the preferred method for encrypting(RSA) and then
immediately writing out to a socket? I inherited some code
that I couldn't get to work:

AutoSeededRandomPool randPool; // should we use a self seeded one?
StringSource source (pKeyPair->getPublicKey(), pKeyPair->getPublicKeySize(), true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor encryptor (source);
_encryptorFilter = new PK_EncryptorFilter (randPool, encryptor );


_encryptorFilter->Put ((const byte*)plaintext, plaintextlen);
if (_encryptorFilter->AnyRetrievable()) {
unsigned int ready = (unsigned int)_encryptorFilter->MaxRetrievable();
unsigned char *eBuf = new unsigned char[ready];
_encryptorFilter->Get(eBuf, ready);
if (_pWriter->writeBytes (eBuf, ready)) {//write the bytes out to the socket
return -1;
}
return 0;
}


So I rewrote it along the lines of the sample code RSAEncryptString like so:


AutoSeededRandomPool randPool; // should we use a self seeded one?
StringSource source (pKeyPair->getPublicKey(), pKeyPair->getPublicKeySize(), true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor encryptor (source);


int ciphertextlen = encryptor.CiphertextLength (plaintextlen);
char *ciphertext = new char[ciphertextlen];
encryptor.Encrypt (randPool, (byte *)plaintext, plaintextlen, (byte *)ciphertext);


    if (_pWriter->writeBytes (ciphertext, ciphertextlen)) {
        return -1;
    }

which works fine. Am I missing something? Which is the most efficient/fastest way to accomplish this?

Many Thanks in advance for any advice...

Tom

Thomas B. Cowin

Research Associate
Institute for Human and Machine Cognition
University of West Florida
40 S. Alcaniz St.
Pensacola Florida 32501

[EMAIL PROTECTED]
Phone (850)202-4426
Cell (850)341-3667
Fax (850)202-4440




Reply via email to