gpt4 book ai didi

java - 你能弄清楚为什么这个程序会触发 IllegalStateException 吗?

转载 作者:行者123 更新时间:2023-11-30 09:58:53 24 4
gpt4 key购买 nike

可以找到~/Cipher/nsdl/crypto 中的所有文件here使用 gcj 编译的 java 文件,参见 compile.sh

nmint@nqmk-mint ~/Cipher/nsdl/crypto $ echo test | ./cryptTest encrypt deadbeefdeadbeefdeadbeefdeadbeef deadbeef Blowfish CBC > test
null
Exception in thread "main" java.lang.IllegalStateException: cipher is not for encrypting or decrypting
at javax.crypto.Cipher.update(libgcj.so.81)
at javax.crypto.CipherOutputStream.write(libgcj.so.81)
at nsdl.crypto.BlockCrypt.encrypt(cryptTest)
at nsdl.crypto.cryptTest.main(cryptTest)

BlockCrypt.java:

package nsdl.crypto;

import java.io.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class BlockCrypt {
Cipher ecipher;
Cipher dcipher;
byte[] keyBytes;
byte[] ivBytes;
SecretKey key;
AlgorithmParameterSpec iv;
byte[] buf = new byte[1024];

BlockCrypt(String keyStr, String ivStr, String algorithm, String mode) {
try {
ecipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding");
dcipher = Cipher.getInstance(algorithm + "/" + mode + "/PKCS5Padding");

keyBytes = hexStringToByteArray(keyStr);
ivBytes = hexStringToByteArray(ivStr);

key = new SecretKeySpec(keyBytes, algorithm);
iv = new IvParameterSpec(ivBytes);

ecipher.init(Cipher.ENCRYPT_MODE, key, iv);
dcipher.init(Cipher.DECRYPT_MODE, key, iv);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}

public void encrypt(InputStream in, OutputStream out) {
try {
// out: where the plaintext goes to become encrypted
out = new CipherOutputStream(out, ecipher);

// in: where the plaintext comes from
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}
out.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}

public void decrypt(InputStream in, OutputStream out) {
try {
// in: where the plaintext come from, decrypted on-the-fly
in = new CipherInputStream(in, dcipher);

// out: where the plaintext goes
int numRead = 0;
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}
out.flush();
out.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}

cryptTest.java:

package nsdl.crypto;

import nsdl.crypto.BlockCrypt;

public class cryptTest {

public static void main (String args[]) {
if (args.length != 5) {
System.err.println("Usage: cryptTest (encrypt|decrypt) key iv algorithm mode");
System.err.println("Takes input from STDIN. Output goes to STDOUT.");
} else {
String operation = args[0];
String key = args[1];
String iv = args[2];
String algorithm = args[3];
String mode = args[4];
BlockCrypt blockCrypt = new BlockCrypt(key, iv, algorithm, mode);
if (operation.equalsIgnoreCase("encrypt")) {
blockCrypt.encrypt(System.in, System.out);
} else if (operation.equalsIgnoreCase("decrypt")) {
blockCrypt.decrypt(System.in, System.out);
} else {
System.err.println("Invalid operation. Use (encrypt|decrypt).");
}
}
}
}

最佳答案

密码 ecipher 未初始化,当您尝试使用它时它会抛出 IllegalStateException,就好像它已在 ENCRYPT_MODE 中初始化一样>.

注意 BlockCrypt 的构造函数中的 catch block 。它正在捕获没有消息的异常,并将“null”打印到 System.err。与其中止执行(也许通过从构造函数中抛出异常),不如继续航行。

System.err.println(e.getMessage()) 替换为 e.printStackTrace() 或至少 System.err.println(e) 应该给你更多的细节。我的猜测是 ecipher.init() 抛出异常,因为您提供的是 32 位 IV 而不是 64 位。

关于java - 你能弄清楚为什么这个程序会触发 IllegalStateException 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/319634/

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