Reputation: 11
I have a 202 byte key and that is used to decrypt a binary file.
StringSource keyStr( key, Z3_KEY_LENGTH, true );
AutoSeededRandomPool rng;
ECIES<ECP>::Decryptor ellipticalEnc( keyStr );
unsigned char *tmpBuffer( new unsigned char[ src.Size() ] );
DecodingResult dr = ellipticalEnc.Decrypt( rng, src.Data(), src.Size(), tmpBuffer );
I tried to use jsafejce for this:
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory factory = KeyFactory.getInstance("EC", "JsafeJCE");
PrivateKey privateKey = factory.generatePrivate(privKeySpec);
Cipher eciesDecrypter = Cipher.getInstance("ECIES/SHA1/HMACSHA1", "JsafeJCE");
and
Cipher eciesDecrypter = Cipher.getInstance("ECIESwithXOR/SHA1/HMACSHA1", "JsafeJCE");
But with the first I get a block error, must be divided by 16, and with the second I get a mac check error.
Does anyone have any suggestions?
Upvotes: 1
Views: 718
Reputation: 102205
Well, I don't really know what you are trying to do in your code. I'll try and answer some of the questions.
Convert ECIES ECP CryptoPP to JAVA
To get the point out of Crypto++, its about as hard as:
// Assuming your key was DER Encoded
byte key[Z3_KEY_LENGTH] = ...;
ECIES<ECP>::Decryptor decryptor;
decryptor.BERDecodePublicKey(ArraySource(key, sizeof(key)).Ref(), false, sizeof(key));
const ECPPoint& point = decryptor.GetPublicElement();
const Integer& x = point.x;
const Integer& y = point.y;
If your key was not DER Encoded, refer to Keys and Formats from the Crypto++ wiki. You also have the wiki page on Elliptic Curve Integrated Encryption Scheme.
Java 7 provides and ECPoint class, and it takes an X and Y coordinate.
> ECIES<ECP>::Decryptor ellipticalEnc( keyStr );
> unsigned char *tmpBuffer( new unsigned char[ src.Size() ] );
> DecodingResult dr = ellipticalEnc.Decrypt( rng, src.Data(), src.Size(), tmpBuffer );
This does not look quite right, but you have not showed enough code.
size_t maxLength = decryptor.MaxPlaintextLength( src.Size() );
unsigned char *tmpBuffer = new unsigned char[ maxLength ];
DecodingResult dr = ellipticalEnc.Decrypt( rng, src.Data(), src.Size(), tmpBuffer );
if( !result.isValidCoding )
throw runtime_error("failed to decrypt cipher text");
unsigned char *buffer = new unsigned char[ result.messageLength ];
std::cpy(tmpBuffer, buffer, result.messageLength);
Upvotes: 1
Reputation: 1
Have you tried adding some empty bytes to the end of your key so that it is 208 bytes long? That might fix your block size error.
Upvotes: 0