gpt4 book ai didi

java - Blowfish CBC算法空间Padding如何实现

转载 作者:行者123 更新时间:2023-12-02 10:46:55 25 4
gpt4 key购买 nike

我必须实现java密码空间填充。我正在尝试编写支持 Blowfish/CBC/space 模式的 Perl 代码的实现。我在Java Cipher列表中找不到相关模式,仅支持PCK5Padding和NoPadding。 Java 是否支持空格填充,如果有人可以帮助我的话。

最佳答案

Space padding是在加密时添加空格使长度为8的倍数,或者在解密后修剪空格。在 Java 中,您可以使用 NoPadding 并自行管理间距。

例如,用 Perl 加密,用 Java 解密:

Perl:

use Crypt::CBC;

$cipher = Crypt::CBC->new(
-literal_key => 1,
-key => pack("H*",
"11223344556677889900112233445566778899001122334"
. "455667788990011223344556677889900112233445566"
. "77889900112233445566"),
-cipher => 'Blowfish',
-header => 'none',
-padding => 'space',
-iv => pack("H*", '1234567812345678')
);

$ciphertext = $cipher->encrypt_hex("Message");
print(join('',unpack('H*',$cipher->iv())));
print $ciphertext;

输出: 1234567812345678e70b9ab0a4262ba8

Java:

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Hex;

public class Main {
public static void main(String[] args) throws Exception {
Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
SecretKeySpec key = new SecretKeySpec(Hex.decodeHex(
"11223344556677889900112233445566778899001122334"
+ "455667788990011223344556677889900112233445566"
+ "77889900112233445566"),
"Blowfish");
byte[] iv = Hex.decodeHex("1234567812345678");

cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] result = cipher.doFinal(Hex.decodeHex("e70b9ab0a4262ba8"));

System.out.println(new String(result, Charsets.UTF_8).trim());
}
}

输出: 消息

关于java - Blowfish CBC算法空间Padding如何实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52473447/

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