Java 11 – ChaCha20加密示例
在本文中,我们将向您展示如何使用RFC 7539中定义的ChaCha20流密码对消息进行加密和解密。
PS ChaCha20流密码可从Java 11获得,请参阅JEP 329 。
注意
您可能对此ChaCha20-Poly1305加密示例感兴趣
1.如何运作?
1.1 ChaCha20加密和解密的输入:
- 256位密钥(32字节)
- 96位随机数(12个字节)
- 32位初始计数(4个字节)
1.2 ChaCha20加密。
(plain text) + (secrect key | nonce | initial count) -> `ChaCha20` -> ciphertext (encrypted text).
1.2 ChaCha20解密。
ciphertext + (secrect key | nonce | initial count) -> `ChaCha20` -> plain text.
1.3 ChaCha20
加密使用密钥和IV(初始化值,随机数+初始计数)将纯文本加密为等长的密文。
1.4每次加密的随机数和秘密密钥必须唯一。 随机数和初始计数可以公开知道,但是密钥必须是私有的并对其保密。
ChaCha20 Java实现
下载JDK源代码,并找到用于ChaCha20算法实现的此类ChaCha20Cipher
。
package com.sun.crypto.provider;/*** Implementation of the ChaCha20 cipher, as described in RFC 7539.** @since 11*/
abstract class ChaCha20Cipher extends CipherSpi {//...
}
2. ChaCha20加密和解密。
2.1一个使用ChaCha20算法加密和解密消息的Java示例。
package com.mkyong.java11.jep329.chacha20;import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.ChaCha20ParameterSpec;/*
The inputs to ChaCha20 encryption, specified by RFC 7539, are:- A 256-bit secret key (32 bytes)- A 96-bit nonce (12 bytes)- A 32-bit initial count (4 bytes)
*/
public class ChaCha20 {private static final String ENCRYPT_ALGO = "ChaCha20";public byte[] encrypt(byte[] pText, SecretKey key, byte[] nonce, int counter) throws Exception {Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);ChaCha20ParameterSpec param = new ChaCha20ParameterSpec(nonce, counter);cipher.init(Cipher.ENCRYPT_MODE, key, param);byte[] encryptedText = cipher.doFinal(pText);return encryptedText;}public byte[] decrypt(byte[] cText, SecretKey key, byte[] nonce, int counter) throws Exception {Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);ChaCha20ParameterSpec param = new ChaCha20ParameterSpec(nonce, counter);cipher.init(Cipher.DECRYPT_MODE, key, param);byte[] decryptedText = cipher.doFinal(cText);return decryptedText;}}
2.1测试。
package com.mkyong.java11.jep329.chacha20;import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;public class TestChaCha20 {public static void main(String[] args) throws Exception {String input = "Java & ChaCha20 encryption example.";ChaCha20 cipher = new ChaCha20();SecretKey key = getKey(); // 256-bit secret key (32 bytes)byte[] nonce = getNonce(); // 96-bit nonce (12 bytes)int counter = 1; // 32-bit initial count (8 bytes)System.out.println("Input : " + input);System.out.println("Input (hex): " + convertBytesToHex(input.getBytes()));System.out.println("\n---Encryption---");byte[] cText = cipher.encrypt(input.getBytes(), key, nonce, counter); // encryptSystem.out.println("Key (hex): " + convertBytesToHex(key.getEncoded()));System.out.println("Nonce (hex): " + convertBytesToHex(nonce));System.out.println("Counter : " + counter);System.out.println("Encrypted (hex): " + convertBytesToHex(cText));System.out.println("\n---Decryption---");byte[] pText = cipher.decrypt(cText, key, nonce, counter); // decryptSystem.out.println("Key (hex): " + convertBytesToHex(key.getEncoded()));System.out.println("Nonce (hex): " + convertBytesToHex(nonce));System.out.println("Counter : " + counter);System.out.println("Decrypted (hex): " + convertBytesToHex(pText));System.out.println("Decrypted : " + new String(pText));}// A 256-bit secret key (32 bytes)private static SecretKey getKey() throws NoSuchAlgorithmException {KeyGenerator keyGen = KeyGenerator.getInstance("ChaCha20");keyGen.init(256, SecureRandom.getInstanceStrong());return keyGen.generateKey();}// 96-bit nonce (12 bytes)private static byte[] getNonce() {byte[] newNonce = new byte[12];new SecureRandom().nextBytes(newNonce);return newNonce;}private static String convertBytesToHex(byte[] bytes) {StringBuilder result = new StringBuilder();for (byte temp : bytes) {result.append(String.format("%02x", temp));}return result.toString();}}
输出量
Input : Java & ChaCha20 encryption example.
Input (hex): 4a617661202620436861436861323020656e6372797074696f6e206578616d706c652e---Encryption---
Key (hex): ee416df8b5154a4ac48f3930fcfa53ef7f677c8fd7cd093f1328eedfd831db1a
Nonce (hex): 9806308f4d1732d2d39beaba
Counter : 1
Encrypted (hex): 2149db2c32bf82f9e8dc0a709d8c15d5a22eb79d5f692e04f070d46cc7e264631f85e0---Decryption---
Key (hex): ee416df8b5154a4ac48f3930fcfa53ef7f677c8fd7cd093f1328eedfd831db1a
Nonce (hex): 9806308f4d1732d2d39beaba
Counter : 1
Decrypted (hex): 4a617661202620436861436861323020656e6372797074696f6e206578616d706c652e
Decrypted : Java & ChaCha20 encryption example.
key + nonce + counter
必须相同,才能进行加密和解密,不同的单个位将产生不同的结果。
3. ChaCha20…版本2
3.1在此示例中,我们使用System.arraycopy
将现时数和初始化计数器附加到密文(加密消息)的末尾,因此我们只需要用于解密的密钥即可。
package com.mkyong.java11.jep329.chacha20v2;import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.ChaCha20ParameterSpec;
import java.nio.ByteBuffer;public class ChaCha20v2 {private static final String ENCRYPT_ALGO = "ChaCha20";private static final int LEN_NONCE = 12;private static final int LEN_COUNTER = 4;public byte[] encrypt(byte[] pText, SecretKey key, byte[] nonce, int counter) throws Exception {Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);ChaCha20ParameterSpec param = new ChaCha20ParameterSpec(nonce, counter);cipher.init(Cipher.ENCRYPT_MODE, key, param);byte[] encryptedText = cipher.doFinal(pText);// append nonce + countbyte[] output = new byte[encryptedText.length + LEN_NONCE + LEN_COUNTER];System.arraycopy(encryptedText, 0, output, 0, encryptedText.length);System.arraycopy(nonce, 0, output, encryptedText.length, LEN_NONCE);// convert int to byte[]byte[] counterByteArray = ByteBuffer.allocate(4).putInt(counter).array();System.arraycopy(counterByteArray, 0, output, encryptedText.length + LEN_NONCE, LEN_COUNTER);return output;}public byte[] decrypt(byte[] cText, SecretKey key) throws Exception {Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);// get only the encrypted textbyte[] encryptedText = new byte[cText.length - (LEN_NONCE + LEN_COUNTER)];System.arraycopy(cText, 0, encryptedText, 0, cText.length - (LEN_NONCE + LEN_COUNTER));// get noncebyte[] nonce = new byte[12];System.arraycopy(cText, encryptedText.length, nonce, 0, LEN_NONCE);// get initial counterbyte[] counter = new byte[4];System.arraycopy(cText, encryptedText.length + LEN_NONCE, counter, 0, LEN_COUNTER);/// convert byte array to intint ic = ByteBuffer.wrap(counter).getInt();ChaCha20ParameterSpec param = new ChaCha20ParameterSpec(nonce, ic);cipher.init(Cipher.DECRYPT_MODE, key, param);byte[] decryptedText = cipher.doFinal(encryptedText);return decryptedText;}}
3.2对于解密,我们只需要提供秘密密钥,因为随机数和初始计数器位于加密文本或密文的末尾。
package com.mkyong.java11.jep329.chacha20v2;import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;public class TestChaCha20v2 {public static void main(String[] args) throws Exception {String input = "Java & ChaCha20 encryption example.";ChaCha20v2 cipher = new ChaCha20v2();SecretKey key = getKey(); // 256-bit secret key (32 bytes)byte[] nonce = getNonce(); // 96-bit nonce (12 bytes)int counter = 1; // 32-bit initial count (8 bytes)System.out.println("Input : " + input);System.out.println("Input (hex): " + convertBytesToHex(input.getBytes()));System.out.println("\n---Encryption---");byte[] cText = cipher.encrypt(input.getBytes(), key, nonce, counter); // encryptSystem.out.println("Key (hex): " + convertBytesToHex(key.getEncoded()));System.out.println("Nonce (hex): " + convertBytesToHex(nonce));System.out.println("Counter : " + counter);System.out.println("Encrypted (hex): " + convertBytesToHex(cText));System.out.println("\n---Decryption---");byte[] pText = cipher.decrypt(cText, key); // decryptSystem.out.println("Key (hex): " + convertBytesToHex(key.getEncoded()));System.out.println("Decrypted (hex): " + convertBytesToHex(pText));System.out.println("Decrypted : " + new String(pText));}private static String convertBytesToHex(byte[] bytes) {StringBuilder result = new StringBuilder();for (byte temp : bytes) {result.append(String.format("%02x", temp));}return result.toString();}// A 256-bit secret key (32 bytes)private static SecretKey getKey() throws NoSuchAlgorithmException {KeyGenerator keyGen = KeyGenerator.getInstance("ChaCha20");keyGen.init(256, SecureRandom.getInstanceStrong());return keyGen.generateKey();}// 96-bit nonce (12 bytes)private static byte[] getNonce() {byte[] newNonce = new byte[12];new SecureRandom().nextBytes(newNonce);return newNonce;}}
输出量
Input : Java & ChaCha20 encryption example.
Input (hex): 4a617661202620436861436861323020656e6372797074696f6e206578616d706c652e---Encryption---
Key (hex): f95fd5b41783595e41f4cbcd8dc26a782599184e97ccd768ac531aae729781d3
Nonce (hex): 84133f2261ef44796e3669dc
Counter : 1
Encrypted (hex): 7738807b409f3349dbefbeae988482e0e5959c35ee8f8ee8987357db459e10d7fb8c7e84133f2261ef44796e3669dc00000001---Decryption---
Key (hex): f95fd5b41783595e41f4cbcd8dc26a782599184e97ccd768ac531aae729781d3
Decrypted (hex): 4a617661202620436861436861323020656e6372797074696f6e206578616d706c652e
Decrypted : Java & ChaCha20 encryption example.
查看加密的文本; 它结合了加密的消息,随机数(12个字节)和初始计数器(4个字节)。
Encrypted (hex): 7738807b409f3349dbefbeae988482e0e5959c35ee8f8ee8987357db459e10d7fb8c7e84133f2261ef44796e3669dc00000001## split view
Encrypted (hex): 7738807b409f3349dbefbeae988482e0e5959c35ee8f8ee8987357db459e10d7fb8c7e | 84133f2261ef44796e3669dc | 00000001
注意
如果发现任何错误,请在下面发表评论,谢谢。
下载源代码
$ git clone https://github.com/mkyong/core-java.git
$ cd java-11
$ cd / src / main / java / com / mkyong / java11 / jep329
参考文献
- ChaCha20
- RFC 7539
- Cloudflare –需要两个到ChaCha(Poly)
- ChaCha20计数器实际上通过迭代增加吗?
- Java:ChaCha20带Poly1305作为MAC,用于通用文件加密
翻译自: https://mkyong.com/java/java-11-chacha20-stream-cipher-examples/
http://www.taodudu.cc/news/show-5084955.html
相关文章:
- Generative Causal Explanations for Graph Neural Networks
- 【论文笔记】GeDi:Generative Discriminator Guided Sequence Generation
- 生成式AI(Generative AI)将重新定义生产力
- 【论文解析】Deep Generative Models on 3D Representations: A Survey
- Generative Modeling by Estimating Gradients of the Data Distribution阅读笔记
- Generative Model - 李宏毅笔记
- C语言:求最小公倍数
- 用C语言求最小公倍数
- C语言求最小公倍数和最大公约数的两种方法
- C语言 求最小公倍数
- [c语言]最小公倍数和最大公约数(详细步骤求解)
- C语言:求最小公倍数和最大公约数
- C语言求最小公倍数的三种方法
- C语言题解——最小公倍数的三种求法(含最大公约数)
- Unity-动作系统-案例学习(1)人物移动和转向
- flash与动画 人物动作动画
- 四川省有多少个市,有多少个县;
- WiFi 频段资源
- 蓝牙与wifi区别
- Wi-Fi 频段信道划分和常见国家信道列表
- Cellular/Wifi/Bluetooth频率
- Kali Linux环境使用aircrack破译WiFi密码
- 2.4Gwifi频道划分
- 怎么更改wifi频段_【wifi信号频率】wifi频率怎么设置 wifi2.4g和5g哪个更好
- WIFI频段
- 8.WLAN频段介绍_频段与信道
- 矩阵0空间,0度,核子空间
- 对相空间的理解
- 相空间重构、时间序列重构、状态空间重构、phase space reconstruction、state space reconstruction
- 矩阵的各种空间
Java 11 – ChaCha20加密示例相关推荐
- Java 11 – ChaCha20-Poly1305加密示例
本文向您展示如何使用RFC 7539中定义的ChaCha20-Poly1305算法对消息进行加密和解密. PS ChaCha20-Poly1305加密算法可从Java 11获得. 1.常见问题 一些常 ...
- java实现md5加密示例
/*** 实现MD5加密**/ public class MD5 {/*** 获取加密后的字符串* @param input* @return*/public static String string ...
- chacha20加密c语言算法,Java 11的新加密算法ChaCha20-Poly1305
ChaCha20-Poly1305 介绍 Java 11新增加了加密算法ChaCha20-Poly1305. ChaCha20-Poly1305相关的名词需要解释一下: ChaCha20是一种流式对称 ...
- 11 种加密 哈希算法的原理及其 Java 实现
11 种加密 & 哈希算法的原理及其 Java 实现 一.目的 二.运行环境 三.基本原理及步骤 (I)各种加密算法的原理: ① DES 数据加密标准(Data Encryption Stan ...
- 在intellij上运行java_如何解决无法在IntelliJ中运行java 11示例程序?
public class First { public static void main(String[] args) { System.out.println("Hello Java 11 ...
- java url地址加密_Java实现url加密处理的方法示例
本文实例讲述了Java实现url加密处理的方法.分享给大家供大家参考,具体如下: package test; import java.security.Key; import java.securit ...
- java11 是长期支持_这里有你不得不了解的Java 11版本特性说明
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- 4.6 W 字总结!Java 11—Java 17特性详解
作者 | 民工哥技术之路 来源 | https://mp.weixin.qq.com/s/SVleHYFQeePNT7q67UoL4Q Java 11 特性详解 基于嵌套的访问控制 与 Java 语言 ...
- Java 11 JDK
根据Oracle新出台的每6个月发布一次Java SE的节奏,Java 11将于2018年9月发布,迄今为止Oracle已经宣布了好几个新功能. 此外,Java 11还将删除一些功能,其中包括删除CO ...
最新文章
- Java---实现运行任意目录下class中加了@MyTest的空参方法(实现图形界面)
- mysql数据库压缩备份_MySQL数据库之mysql数据库备份命令分享(mysql压缩数据库备份)...
- 价值50万年薪的Java面试题
- php 实现树状组织图插件,使用jstree插件实现树形结构
- Linux Shell常用技巧(四) awk
- 爱上Foobar2000抛弃winamp一周年纪念日
- Db4o 新建、查询、更新、删除操作
- 系统性的学会 Pandas, 看这一篇就够了!
- TMC8670 – 集成EtherCAT通讯和FOC伺服运动控制芯片适用2/3相永磁同步电机
- 登录时用户名或密码错误弹窗提醒重新登录
- JavaScript代码模拟鼠标自动点击事件
- 天梯赛+01训练总结
- 怎样将PDF转成JPG?PDF转换图片其实很简单
- 汇编语言中的[...]和(...)
- ae计算机配置要求,CG馒头分享AE cs6对电脑所需配置需求
- 三星手机投屏到Windows10电脑上教程(利用自带连接软件)
- java家庭饮食营养管理计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
- c语言 条件判断函数,用Excel函数实现多个区间的条件判断
- [CSP-S2020] 儒略日 解题报告
- sql sum的使用
热门文章
- PPS优化安装版和暴风影音优化安装版
- 判断是否构成三角形,并用海伦公式计算三角形的面积
- Calico node ‘10.248.xx.79‘ is already using the IPv4 address 10.248.xx.113
- 17年寒假阅读页面索引
- 【CodingNoBorder - 09】无际软工队 - 求职岛:ALPHA 阶段项目展示
- 计算机打竖字体,告诉你Photoshop怎么输入竖排的文字
- 【Google Chart Tools: Infographics】谷歌图表工具:信息图表
- CRS-2728: A resource type with the name 'ora.daemon.type' is already register
- doxygen 命令_Doxygen
- 安徽省抽检安防监控摄像机产品质量合格率75%