- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 ChaCha20/Poly1305 对来自任何来源的任何数据进行通用加密和解密(与我们使用 I/O 流的方式相同,例如 CipherInputStream)。
This question已经询问是否可以使用 Bouncy CaSTLe 的 ChaCha20Poly1305 类来处理数据,但似乎只支持 TLS 事务。
所以现在我只剩下纯 ChaCha20 (ChaCha20Engine)。我想知道编写自己的加密然后 MAC 方案是否是个好主意,以便我得到我需要的东西。
tl;dr 可以编写我自己的 ChaCha20/Poly1305 加密然后 MAC 操作模式吗?
最佳答案
Java 11 添加了“ChaCha20-Poly1305/None/NoPadding Cipher现在可以在没有任何第三方库或其他魔法的情况下使用 -->
这是引用实现
注意此实现使用非随机 Nonce,除测试目的外不应使用:
package chaCha20Poly1305Encryption;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;
public class ChaCha20Poly1305 {
public static byte[] encrypt(byte[] data, SecretKey key) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
if(key == null) throw new InvalidKeyException("SecretKey must NOT be NULL");
byte[] nonceBytes = new byte[12];
// Get Cipher Instance
Cipher cipher = Cipher.getInstance("ChaCha20-Poly1305/None/NoPadding");
// Create IvParamterSpec
AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(nonceBytes);
// Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "ChaCha20");
// Initialize Cipher for ENCRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);
// Perform Encryption
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] cipherText, SecretKey key) throws Exception {
if(key == null) throw new InvalidKeyException("SecretKey must NOT be NULL");
byte[] nonceBytes = new byte[12];
// Get Cipher Instance
Cipher cipher = Cipher.getInstance("ChaCha20-Poly1305/None/NoPadding");
// Create IvParamterSpec
AlgorithmParameterSpec ivParameterSpec = new IvParameterSpec(nonceBytes);
// Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "ChaCha20");
// Initialize Cipher for DECRYPT_MODE
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
// Perform Decryption
return cipher.doFinal(cipherText);
}
public static void main(String[] args) throws Exception {
SecretKey key = ChaCha20Poly1305KeyGenerator.generateKey();
String testMessage = "hallo!";
byte[] encryptedBytes = encrypt(testMessage.getBytes(), key);
String decryptedMessage = new String(decrypt(encryptedBytes,key));
System.out.println("testMessage: " + testMessage);
System.out.println(key.getAlgorithm() + " SecretKey: " + Base64.getEncoder().encodeToString(key.getEncoded()));
System.out.println("encryptedBytes: " + Base64.getEncoder().encodeToString(encryptedBytes));
System.out.println("decryptedMessage: "+ decryptedMessage);
}
}
以及相应的 key 生成器:
package chaCha20Poly1305Encryption;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class ChaCha20Poly1305KeyGenerator {
public static SecretKey generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("ChaCha20");
//Keysize MUST be 256 bit - as of Java11 only 256Bit is supported
keyGenerator.init(256);
return keyGenerator.generateKey();
}
public static void main(String[] args) throws NoSuchAlgorithmException {
SecretKey key = generateKey();
System.out.println(key.getAlgorithm() + " SecretKey: " + Base64.getEncoder().encodeToString(key.getEncoded()));
}
}
我的 Github GIST 中使用的代码: https://gist.github.com/eXspir3/7a0821a6cfdb0495ccc3e69b475a61b9
关于Java:ChaCha20 w/Poly1305 作为通用文件加密的 MAC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36531479/
我正在尝试在客户端/前端上对图像进行多边形剪辑。一旦我获得积分,是否有办法吸引客户(即无需往返客户)? 据我所知,canvas 只能剪切矩形。也许有一种我不知道的方法?有没有我可以使用的轻量级 Can
我正在尝试使用 shapeless 创建一个可以采用余积的 poly2 函数: case class IndexedItem( item1: Item1, item2: Item2, it
是否可以使用 Poly/ML 构建共享库? 我希望能够创建一个 C Poly/ML 库的 API,并从不同的编程语言(例如 Python)调用/加载它。有人尝试这样做吗? 我知道这可以在 OCaml
我通过 js 模拟了标签区域悬停,并为每个区域创建了 js 函数。因此,正如您所看到的,矩形区域( bool 什维克主义)被绘制成 incide,而保利区域(乌托邦社会主义)只有一个轮廓。 var h
R 中的函数 poly() 用于生成正交向量,有助于解释系数的重要性。但是,我不认为将其用于预测的意义。在我看来,以下两个模型(model_1 和 model_2)应该产生相同的预测。 q=1:11
我已经通读了手册页?poly(我承认我没有完全理解),并且还阅读了书Introduction to Statistical Learning中该函数的描述。 。 我目前的理解是,调用poly(hors
到目前为止,我一直在使用 Poly/ML 进行几个所有源代码文件都在同一目录中的小项目。要构建这些项目,我所要做的就是在 REPL 中运行以下命令: > PolyML.make "Main"; 但现在
在 Poly/ML 中,可以通过以下方式获取全局值的名称: map #1 ((#allVal PolyML.globalNameSpace) ()); 可以使用#allStruct 类似地获得顶级结构
我正在尝试编译一串源代码并使用 Poly/ML 打印解析树。以下代码可以编译,但解析树为空: fun main () = let val stream = TextIO.ope
到目前为止,我一直在使用 Poly/ML 进行几个所有源代码文件都在同一目录中的小项目。要构建这些项目,我所要做的就是在 REPL 中运行以下命令: > PolyML.make "Main"; 但现在
这个问题已经有答案了: Fitting polynomial model to data in R (5 个回答) 已关闭10 年前。 我正在尝试使用 lm(poly) 获得某些点的多项式回归,但对它
我想将多项式系数附加到 data.frame,如下所示: df1 % dplyr::mutate( Linear = poly(x = Y, degree = 3, raw = TR
如何在 Cocos2D 框架中绘制填充多边形? 下面的代码绘制多边形但没有抗锯齿。我应该更改什么? void ccFillPoly( CGPoint *poli, int points, BOOL c
我在 svg 上绘制了人体部位图并将其集成到我的 html 中: .st0{stroke:#010101;stroke-width:0.4;stroke-miterlimit:10;}
我正在尝试了解如何使用 scikit-learn(或其他模块)在 R 中复制 poly() 函数。 例如,假设我在 R 中有一个向量: a <- c(1:10) 我想生成三次多项式: polynomi
我有一个关于公式和用户定义函数的问题: 情况1: clotting 这是coef=的地方添加属性。但还要注意,它会检查调用是否来自“poly”函数本身。由于您的函数名为“xpoly”但返回一个“
如果我有一个像这样的空间线对象: require(sp) x <- c(18.25721, 18.25763,18.25808,18.25846,18.25864,18.25886,18.25892,
我注意到大多数 3d 游戏/渲染环境将实体表示为(通常是三角形)3d 多边形的网格。但是一些示例,例如 Second Life , 或 PovRay使用由一组 3d 图元(立方体、球体、圆锥体、环面等
我正在使用 GeoDjango 查找多边形内的所有点,但它似乎使用边界(NMW、NME、SME、SMW)来查找点。因此,它会返回原始形状之外的结果。 polygon = Polygon((18.382
来自Tiltbrush section的多边形动画无法在 Linux 中为我渲染(在 Chrome 或 Firefox 下):https://poly.google.com/tiltbrush (但是
我是一名优秀的程序员,十分优秀!