/* * Copyright (c) [2016] [ <ether.camp> ] * This file is part of the ethereumJ library. * * The ethereumJ library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * The ethereumJ library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>. */ package org.ethereum.crypto; import org.junit.Assert; import org.junit.Test; import org.spongycastle.math.ec.ECPoint; import org.spongycastle.util.encoders.Hex; import java.math.BigInteger; public class ECIESCoderTest { @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) {e.printStackTrace();} Assert.assertEquals("802b052f8b066640bba94a4fc39d63815c377fced6fcb84d27f791c9921ddf3e9bf0108e298f490812847109cbd778fae393e80323fd643209841a3b7f110397f37ec61d84cea03dcc5e8385db93248584e8af4b4d1c832d8c7453c0089687a700", Hex.toHexString(payload)); } @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) {e.printStackTrace();} System.out.println(Hex.toHexString(cipher)); byte[] decrypted_payload = new byte[0]; try { decrypted_payload = ECIESCoder.decrypt(privKey, cipher); } catch (Throwable e) {e.printStackTrace();} System.out.println(Hex.toHexString(decrypted_payload)); } }