gpt4 book ai didi

java - 如何实现双鱼加密来加密/解密java中的字符串?

转载 作者:行者123 更新时间:2023-12-05 07:40:11 24 4
gpt4 key购买 nike

我最近从 https://www.schneier.com/academic/twofish/download.html 下载了 twofish-java.zip .里面有一个jar文件twofish.jar。有人向我解释了如何在 java 中实现 twofish 加密以使用此 jar 文件在 java 中加密/解密字符串

最佳答案

您需要实现函数 blockEncrypt()blockDecrypt() 并围绕它们构建操作模式。 key 是使用 makeKey() 函数创建的,它要求 key 长度为 8、16、24 或 32 字节。确保将明文填充为 16 字节的倍数。我已将我的 ECB 和 CBC 模式代码附在 Twofish 上。如果您想要最好的安全性,我建议您实现自己的填充功能和 key 扩展功能。在我的示例中,每个 block 使用相同的 key ,明文仅在末尾填充 0。

static byte[] ecbEncrypt(byte[] plaintext, byte[] key) throws InvalidKeyException {
byte[] pt = pad(plaintext, 16);
byte[] ciphertext = new byte[pt.length];
for (int i = 0; i < pt.length; i += 16) {
byte[] t = Twofish_Algorithm.blockEncrypt(pt, i, Twofish_Algorithm.makeKey(key));
for (int j = i; j < i + 16; j++)
ciphertext[j] = t[j % 16];
}
return ciphertext;
}

static byte[] ecbDecrypt(byte[] ciphertext, byte[] key) throws InvalidKeyException {
byte[] plaintext = new byte[ciphertext.length];
for (int i = 0; i < ciphertext.length; i += 16) {
byte[] t = Twofish_Algorithm.blockDecrypt(ciphertext, i, Twofish_Algorithm.makeKey(key));
for (int j = i; j < i + 16; j++)
plaintext[j] = t[j % 16];
}
return unpad(plaintext);
}
static byte[] cbcEncrypt(byte[] plaintext, byte[] key, byte[] iv) throws InvalidKeyException {
byte[] pt = pad(plaintext, 16);
byte[] ciphertext = new byte[pt.length];
for (int i = 0; i < 16; i++)
pt[i] ^= iv[i];
byte[] t = Twofish_Algorithm.blockEncrypt(pt, 0, Twofish_Algorithm.makeKey(key));
for (int i = 0; i < 16; i++)
ciphertext[i] = t[i];
for (int i = 16; i < pt.length; i += 16) {
for (int j = 0; j < 16; j++)
pt[i + j] ^= ciphertext[i - 16 + j];
byte[] t2 = Twofish_Algorithm.blockEncrypt(pt, i, Twofish_Algorithm.makeKey(key));
for (int j = 0; j < 16; j++)
ciphertext[i + j] = t2[j];
}
return ciphertext;
}

static byte[] cbcDecrypt(byte[] ciphertext, byte[] key, byte[] iv) throws InvalidKeyException {
byte[] plaintext = new byte[ciphertext.length];
byte[] t = Twofish_Algorithm.blockDecrypt(ciphertext, 0, Twofish_Algorithm.makeKey(key));
for (int i = 0; i < 16; i++)
plaintext[i] = (byte) (t[i] ^ iv[i]);
for (int i = 16; i < ciphertext.length; i += 16) {
byte[] t2 = Twofish_Algorithm.blockDecrypt(ciphertext, i, Twofish_Algorithm.makeKey(key));
for (int j = 0; j < 16; j++)
plaintext[i + j] = (byte) (t2[j] ^ ciphertext[i - 16 + j]);
}
return unpad(plaintext);
}
static byte[] pad(byte[] in, int blockSize) {
byte[] ret = new byte[in.length + blockSize - in.length % blockSize];
for (int i = 0; i < in.length; i++)
ret[i] = in[i];
byte paddedBytes = 0;
for (int i = in.length; i < ret.length - 1; i++) {
ret[i] = 0;
paddedBytes++;
}
ret[ret.length - 1] = paddedBytes;
return ret;
}

static byte[] unpad(byte[] in) {
byte[] ret = new byte[in.length - in[in.length - 1] - 1];
for (int i = 0; i < ret.length; i++)
ret[i] = in[i];
return ret;
}

这个实现可能不会很完美,所以请随时给我改进,以便我可以编辑我的回复。

你可以这样调用函数:

SecureRandom sr = new SecureRandom();

byte[] plaintext = "This is secret plaintext".getBytes();
byte[] key = new byte[16];
byte[] iv = new byte[16];
sr.nextBytes(key);
sr.nextBytes(iv);

System.out.println("Plaintext: " + new String(plaintext));
byte[] ecbCiphertext = ecbEncrypt(plaintext, key);
System.out.println("ECB-Ciphertext: " + new String(ecbCiphertext));
byte[] ecbPlaintext = ecbDecrypt(ecbCiphertext, key);
System.out.println("ECB-Plaintext: " + new String(ecbPlaintext));
byte[] cbcCiphertext = cbcEncrypt(plaintext, key, iv);
System.out.println("CBC-Ciphertext: " + new String(cbcCiphertext));
byte[] cbcPlaintext = cbcDecrypt(cbcCiphertext, key, iv);
System.out.println("CBC-Plaintext: " + new String(cbcPlaintext));
Plaintext: This is secret plaintext
ECB-Ciphertext: ÑìÔõ¬ŽÁ@γ÷ÊÑ–1—N,/ )ë+$
ECB-Plaintext: This is secret plaintext
CBC-Ciphertext: JÉÌÙ•=Hæí_•¤iÔ[%˜Å?ÆÍæ“!Ø?%
CBC-Plaintext: This is secret plaintext

如果您想要一种使用计数器模式加密/解密的方法,我也附上了我的实现。

static byte[] ctrEncrypt(byte[] plaintext, byte[] key, byte[] iv) throws InvalidKeyException {
byte[] initializationVector = Arrays.copyOf(iv, iv.length);
byte[] counter = { -128, -128, -128, -128, -128, -128, -128, -128 };
byte[] ciphertext = new byte[plaintext.length];
for (int i = 0; i < plaintext.length / 16 + 1; i++) {
for (int j = 0; j < 8; j++) {
if (counter[j] == 127 && j != 7) { counter[j] = -128;
counter[j + 1]++;
}
if (counter[j] != -128)
initializationVector[15 - j] = counter[j];
else
initializationVector[15-j] = iv[15-j];
}
byte[] t = Twofish_Algorithm.blockEncrypt(initializationVector, 0, Twofish_Algorithm.makeKey(key));
for (int j = 0; i < plaintext.length / 16 ? j < 16 : j < plaintext.length % 16; j++)
ciphertext[i * 16 + j] = (byte) (plaintext[i * 16 + j] ^ t[j]);
counter[0]++;
}
return ciphertext;
}

static byte[] ctrDecrypt(byte[] ciphertext, byte[] key, byte[] iv) throws InvalidKeyException {
return ctrEncrypt(ciphertext, key, iv);
}

关于java - 如何实现双鱼加密来加密/解密java中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46397301/

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