gpt4 book ai didi

Java:ChaCha20 w/Poly1305 作为通用文件加密的 MAC

转载 作者:行者123 更新时间:2023-12-05 04:13:50 30 4
gpt4 key购买 nike

我需要使用 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com