public static byte[] decrypt(BigInteger privKey, byte[] cipher, byte[] macData) throws IOException, InvalidCipherTextException { byte[] plaintext; ByteArrayInputStream is = new ByteArrayInputStream(cipher); byte[] ephemBytes = new byte[2 * ((CURVE.getCurve().getFieldSize() + 7) / 8) + 1]; is.read(ephemBytes); ECPoint ephem = CURVE.getCurve().decodePoint(ephemBytes); byte[] IV = new byte[KEY_SIZE / 8]; is.read(IV); byte[] cipherBody = new byte[is.available()]; is.read(cipherBody); plaintext = decrypt(ephem, privKey, IV, cipherBody, macData); return plaintext; }
public static byte[] encrypt(ECPoint toPub, byte[] plaintext) { return encrypt(toPub, plaintext, null); }
@Test // encrypt decrypt round trip public void test2() { BigInteger privKey = new BigInteger("5e173f6ac3c669587538e7727cf19b782a4f2fda07c1eaa662c593e5e85e3051", 16); byte[] payload = Hex.decode("1122334455"); ECKey ecKey = ECKey.fromPrivate(privKey); ECPoint pubKeyPoint = ecKey.getPubKeyPoint(); byte[] cipher = new byte[0]; try { cipher = ECIESCoder.encrypt(pubKeyPoint, payload); } catch (Throwable e) { log.error(e.getMessage()); } log.debug(Hex.toHexString(cipher)); byte[] decrypted_payload = new byte[0]; try { decrypted_payload = ECIESCoder.decrypt(privKey, cipher); } catch (Throwable e) { log.error(e.getMessage()); } log.debug(Hex.toHexString(decrypted_payload)); }
public static byte[] encrypt(ECPoint toPub, byte[] plaintext, byte[] macData) { ECKeyPairGenerator eGen = new ECKeyPairGenerator(); SecureRandom random = new SecureRandom(); KeyGenerationParameters gParam = new ECKeyGenerationParameters(CURVE, random); eGen.init(gParam); byte[] IV = new byte[KEY_SIZE / 8]; new SecureRandom().nextBytes(IV); AsymmetricCipherKeyPair ephemPair = eGen.generateKeyPair(); BigInteger prv = ((ECPrivateKeyParameters) ephemPair.getPrivate()).getD(); ECPoint pub = ((ECPublicKeyParameters) ephemPair.getPublic()).getQ(); EthereumIESEngine iesEngine = makeIESEngine(true, toPub, prv, IV); ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(CURVE, random); ECKeyPairGenerator generator = new ECKeyPairGenerator(); generator.init(keygenParams); ECKeyPairGenerator gen = new ECKeyPairGenerator(); gen.init(new ECKeyGenerationParameters(ECKey.CURVE, random)); byte[] cipher; try { cipher = iesEngine.processBlock(plaintext, 0, plaintext.length, macData); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(pub.getEncoded(false)); bos.write(IV); bos.write(cipher); return bos.toByteArray(); } catch (InvalidCipherTextException | IOException e) { throw new RuntimeException(e); } }
@Test // decrypt cpp data public void test1() { BigInteger privKey = new BigInteger("5e173f6ac3c669587538e7727cf19b782a4f2fda07c1eaa662c593e5e85e3051", 16); byte[] cipher = Hex.decode("049934a7b2d7f9af8fd9db941d9da281ac9381b5740e1f64f7092f3588d4f87f5ce55191a6653e5e80c1c5dd538169aa123e70dc6ffc5af1827e546c0e958e42dad355bcc1fcb9cdf2cf47ff524d2ad98cbf275e661bf4cf00960e74b5956b799771334f426df007350b46049adb21a6e78ab1408d5e6ccde6fb5e69f0f4c92bb9c725c02f99fa72b9cdc8dd53cff089e0e73317f61cc5abf6152513cb7d833f09d2851603919bf0fbe44d79a09245c6e8338eb502083dc84b846f2fee1cc310d2cc8b1b9334728f97220bb799376233e113"); byte[] payload = new byte[0]; try { payload = ECIESCoder.decrypt(privKey, cipher); } catch (Throwable e) { log.error(e.getMessage()); } Assert.assertEquals("802b052f8b066640bba94a4fc39d63815c377fced6fcb84d27f791c9921ddf3e9bf0108e298f490812847109cbd778fae393e80323fd643209841a3b7f110397f37ec61d84cea03dcc5e8385db93248584e8af4b4d1c832d8c7453c0089687a700", Hex.toHexString(payload)); }