日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/19 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 数字信封_GitHub - zhopen/eos-crypto-java: EOS 公钥加密,私钥解密。基于ECC+AES 实现的双向验证加解密。数字信封的 加解密。... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

/**

*

* sender

*

* EOS8g1u3ktAGHs4QsVp9aeaWNebFLtprQHwpaSjegx6iEuoTNhjXU

* 5KTZYCDdcfNrmEpcf97SJBCtToZjYHjHm8tqTWvzUbsUJgkxcfk

*

* receiver 平臺公私鑰對

*

* EOS7ez2gagfoXw9XdW3kRx3EsCoWvupGR6u6ZJhFPEe9Q12V8JgUL

* 5JUrqxYcssR9LLVtWDeQcc9HCX4FEqBG7d9GW6t7mvmB1rUuZr9

*

* receiver 省側(cè)公私鑰對

* EOS5WMHqw6jDDBPBm7JXTHemAwtSo2tp93pRysJMRhiT1zUYb24vL

* 5HrcVeuHHNwHsivrMoJ9XvU6EM7Q2wQ2ECiy8GeoiuamhNiSuZq

*/

// 1. 調(diào)用錢包獲取 發(fā)送方私鑰

String senderPrivateKey = "5KTZYCDdcfNrmEpcf97SJBCtToZjYHjHm8tqTWvzUbsUJgkxcfk";

EosPrivateKey senderECPrivateKey = new EosPrivateKey(senderPrivateKey);

// EosPublicKey senderECPublicKey = new EosPublicKey(senderPublicKey);

// 2. 根據(jù)私鑰 生成公鑰。 或者直接根據(jù)公鑰 調(diào)用錢包獲取私鑰。 都可以,看具體業(yè)務需求

EosPublicKey senderECPublicKey = senderECPrivateKey.getPublicKey();

String senderPublicKey = senderECPublicKey.toString();

/**

* 調(diào)用錢包獲取 接收方私鑰 獲取公私鑰方式 根據(jù)業(yè)務需求確定。

* 1. 可以根據(jù)公鑰,從錢包里獲取私鑰

* 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);

// 待加密 數(shù)據(jù)

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

System.out.println("加密前原始數(shù)據(jù): " + new String(params,"utf8"));

// 發(fā)起方使用對稱密鑰,對原始數(shù)據(jù)進行加密

byte[] encryptedData = null;

try {

encryptedData = CryptUtil.aesEncryptWithNOIV(nonce,params);

} catch (InvalidCipherTextException e) {

e.printStackTrace();

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

}

System.out.println("加密后數(shù)據(jù): " + HexUtils.toHex(encryptedData));

System.out.println("加密前對稱密鑰: " + HexUtils.toHex(nonce));

// 發(fā)起方使用 接收方公鑰,對對稱密鑰進行加密

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));

// 將對稱密鑰加密后的數(shù)據(jù),密文組裝后,進行網(wǎng)絡傳輸。

// 組裝 demo

/**

* 4 byte | encryptedKey | 4 byte | encryptedData

* 對稱密鑰加密后的數(shù)據(jù)長度 | ECC 加密后的對稱秘鑰 | 密文數(shù)據(jù)長度 | 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(" 將數(shù)字信封和密文組裝后的報文 16進制格式:" + hexencode);

System.out.println("發(fā)送方數(shù)據(jù)加密完成,可以將數(shù)據(jù)發(fā)送出去 ");

/**

***************************************************** 以下為接收方 代碼 *************************************

*/

// 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);

// 解密后數(shù)據(jù)

System.out.println("解密后數(shù)據(jù) : "+new String(plainText, "utf8"));

} catch (InvalidCipherTextException e) {

e.printStackTrace();

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

}

總結(jié)

以上是生活随笔為你收集整理的java 数字信封_GitHub - zhopen/eos-crypto-java: EOS 公钥加密,私钥解密。基于ECC+AES 实现的双向验证加解密。数字信封的 加解密。...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。