java 数字信封_GitHub - yanjunli/eos-crypto-java: EOS 公钥加密,私钥解密。基于ECC+AES 实现的双向验证加解密。数字信封的 加解密。...

/**

*

* sender

*

* EOS8g1u3ktAGHs4QsVp9aeaWNebFLtprQHwpaSjegx6iEuoTNhjXU

* 5KTZYCDdcfNrmEpcf97SJBCtToZjYHjHm8tqTWvzUbsUJgkxcfk

*

* receiver 平台公私钥对

*

* EOS7ez2gagfoXw9XdW3kRx3EsCoWvupGR6u6ZJhFPEe9Q12V8JgUL

* 5JUrqxYcssR9LLVtWDeQcc9HCX4FEqBG7d9GW6t7mvmB1rUuZr9

*

* receiver 省侧公私钥对

* EOS5WMHqw6jDDBPBm7JXTHemAwtSo2tp93pRysJMRhiT1zUYb24vL

* 5HrcVeuHHNwHsivrMoJ9XvU6EM7Q2wQ2ECiy8GeoiuamhNiSuZq

*/

// 1. 调用钱包获取 发送方私钥

String senderPrivateKey = "5KTZYCDdcfNrmEpcf97SJBCtToZjYHjHm8tqTWvzUbsUJgkxcfk";

EosPrivateKey senderECPrivateKey = new EosPrivateKey(senderPrivateKey);

// EosPublicKey senderECPublicKey = new EosPublicKey(senderPublicKey);

// 2. 根据私钥 生成公钥。 或者直接根据公钥 调用钱包获取私钥。 都可以,看具体业务需求

EosPublicKey senderECPublicKey = senderECPrivateKey.getPublicKey();

String senderPublicKey = senderECPublicKey.toString();

/**

* 调用钱包获取 接收方私钥 获取公私钥方式 根据业务需求确定。

* 1. 可以根据公钥,从钱包里获取私钥

* 2. 也可以直接从钱包里取出私钥,反向生成公钥

*/

String receiverPrivateKey = "5JUrqxYcssR9LLVtWDeQcc9HCX4FEqBG7d9GW6t7mvmB1rUuZr9";

EosPrivateKey receiverECPrivateKey = new EosPrivateKey(receiverPrivateKey);

EosPublicKey receiverECPublicKey = receiverECPrivateKey.getPublicKey();

/**

* 生成对称密钥

*/

byte[] nonce = new byte[16];

MTRandom random=new MTRandom();

random.nextBytes(nonce);

// 待加密 数据

byte[] params = "{\"age\": 1,\"汉字\":\"为初始化向量,可以使用固定值,\",\"12345\":\"24qqwazzxdtttdxkaskjewuizckczxnlsdosasda4!!!@#$$%^&&*(()(^#\"}".getBytes("utf8");

System.out.println("加密前原始数据: " + new String(params,"utf8"));

// 发起方使用对称密钥,对原始数据进行加密

byte[] encryptedData = null;

try {

encryptedData = CryptUtil.aesEncryptWithNOIV(nonce,params);

} catch (InvalidCipherTextException e) {

e.printStackTrace();

System.out.println(" do something!!!!");

}

System.out.println("加密后数据: " + HexUtils.toHex(encryptedData));

System.out.println("加密前对称密钥: " + HexUtils.toHex(nonce));

// 发起方使用 接收方公钥,对对称密钥进行加密

byte[] encryptedKey = null;

try {

encryptedKey = ECCUtil.publicEncrypt(nonce,receiverECPublicKey.getECPublicKey());

} catch (Exception e) {

e.printStackTrace();

System.out.println(" do something!!!!");

}

System.out.println("加密后对称密钥: " + HexUtils.toHex(encryptedKey));

// 将对称密钥加密后的数据,密文组装后,进行网络传输。

// 组装 demo

/**

* 4 byte | encryptedKey | 4 byte | encryptedData

* 对称密钥加密后的数据长度 | ECC 加密后的对称秘钥 | 密文数据长度 | AES 加密后的密文

*/

ByteBuffer bytebuffer = ByteBuffer.allocate( 4 + encryptedKey.length + 4 +encryptedData.length);

bytebuffer.putInt(encryptedKey.length);

bytebuffer.put(encryptedKey);

bytebuffer.putInt(encryptedData.length);

bytebuffer.put(encryptedData);

// String base58encode = Base58.encode(bytebuffer.array());

// System.out.println("base58 编码后的: " + base58encode);

// 进行 16 进制编码

String hexencode = HexUtils.toHex(bytebuffer.array());

System.out.println(" 将数字信封和密文组装后的报文 16进制格式:" + hexencode);

System.out.println("发送方数据加密完成,可以将数据发送出去 ");

/**

***************************************************** 以下为接收方 代码 *************************************

*/

// byte[] base58decode = Base58.decode(hexencode);

byte[] hexdecode = HexUtils.toBytes(hexencode);

ByteBuffer receiveBuffer = ByteBuffer.wrap(hexdecode);

// 获取到对称秘钥长度

int receivedEncryptedKeyLength = receiveBuffer.getInt();

// 加密后的对称密钥key

byte[] receivedEncryptKey = new byte[receivedEncryptedKeyLength];

receiveBuffer.get(receivedEncryptKey,0,receivedEncryptedKeyLength);

System.out.println(" 接收到的 加密后的对称密钥 :" + HexUtils.toHex(receivedEncryptKey));

// 获取到的 密文的长度

int contextLength = receiveBuffer.getInt();

// 密文

byte[] receivedEncryptContext = new byte[contextLength];

receiveBuffer.get(receivedEncryptContext,0,contextLength);

System.out.println(" 接收到的 密文:" + HexUtils.toHex(receivedEncryptContext));

// 使用接收方私钥,解密对称密钥

byte[] receiveddecryptKey = null;

try {

receiveddecryptKey = ECCUtil.privateDecrypt(receivedEncryptKey,receiverECPrivateKey.getECPrivateKey());

} catch (Exception e) {

e.printStackTrace();

System.out.println(" do something!!!!");

}

System.out.println(" 解密后的对称密钥 :" + HexUtils.toHex(receiveddecryptKey));

// 使用对称密钥,对密文进行解密

try {

byte[] plainText = CryptUtil.aesDecryptWithNOIV(receiveddecryptKey,receivedEncryptContext);

// 解密后数据

System.out.println("解密后数据 : "+new String(plainText, "utf8"));

} catch (InvalidCipherTextException e) {

e.printStackTrace();

System.out.println(" do something!!!!");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值