我有一个202字节的密钥,用于解密二进制文件。
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 );
我试图为此使用jsafejce:
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory factory = KeyFactory.getInstance("EC", "JsafeJCE");
PrivateKey privateKey = factory.generatePrivate(privKeySpec);
Cipher eciesDecrypter = Cipher.getInstance("ECIES/SHA1/HMACSHA1", "JsafeJCE");
和
Cipher eciesDecrypter = Cipher.getInstance("ECIESwithXOR/SHA1/HMACSHA1", "JsafeJCE");
但是第一次出现块错误,必须除以16,第二次出现mac检查错误。
有没有人有什么建议?
最佳答案
好吧,我真的不知道您在代码中要做什么。我将尝试回答一些问题。
转换ECIES ECP CryptoPP到JAVA
为了弄清楚Crypto ++的意义,它的难度大约是:
// 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;
如果您的密钥未经DER编码,请参阅Crypto ++ Wiki中的Keys and Formats。您还可以在Elliptic Curve Integrated Encryption Scheme上找到Wiki页面。
Java 7提供了ECPoint class和,它采用X和Y坐标。
> ECIES<ECP>::Decryptor ellipticalEnc( keyStr );
> unsigned char *tmpBuffer( new unsigned char[ src.Size() ] );
> DecodingResult dr = ellipticalEnc.Decrypt( rng, src.Data(), src.Size(), tmpBuffer );
这看起来不太正确,但是您没有显示足够的代码。
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);