gpt4 book ai didi

java - 尝试调用加密算法生成 NoSuchAlgorithmException

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:12:05 26 4
gpt4 key购买 nike

尝试编译以下代码时,我每次都会生成 NoSuchAlgorithmException,无论我将什么字符串分配给算法变量(AES、DESede 等)。错误总是在构造函数中抛出,当我第一次尝试为键赋值时,我终究无法弄清楚问题是什么。我已经包含了整个文件,以防万一相关,但唯一的问题似乎是构造函数。如果有人熟悉这个问题并能指出我哪里出错了,我将不胜感激。

public class Seclib {


private static String algorithm = "AES";
private static Key key = null;
private static Cipher cipher = null;

public Seclib(){
key = KeyGenerator.getInstance(algorithm).generateKey();
cipher = Cipher.getInstance(algorithm);
}

public static String messageHash(String message) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(message.getBytes());
return md.digest(message.getBytes()).toString();
}

public static byte[] encryptMessage(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] inputBytes = input.getBytes();
return cipher.doFinal(inputBytes);
}

private static String decrypt(byte[] encryptionBytes) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] recoveredBytes =
cipher.doFinal(encryptionBytes);
String recovered = new String(recoveredBytes);
return recovered;
}

public static String initializeSecurityParameters(Scanner reader, int securityArray[]){

int flagC = 0;
int flagI = 0;
int flagA = 0;

String securityArrayString = "";

String s;

while(flagC == 0){

System.out.println("Does this session require confidentiality? Y/N");
System.out.println();
s = reader.next();
System.out.println();

if(s.equals("Y") || s.equals("y") || s.equals("N") || s.equals("n")){

if(s.equals("Y") || s.equals("y")){
securityArray[0] = 1;
flagC = 1;
}else{
securityArray[0] = 0;
flagC = 2;
}
}

if(flagC == 0){
System.out.println("Invalid entry, must be one of Y,y,N,n");
System.out.println();
}
}

while(flagI == 0){

System.out.println("Does this session require integrity? Y/N");
System.out.println();
s = reader.next();
System.out.println();

if(s.equals("Y") || s.equals("y") || s.equals("N") || s.equals("n")){
flagI = 1;
if(s.equals("Y") || s.equals("y")){
securityArray[1] = 1;
flagI = 1;
}else{
securityArray[1] = 0;
flagI = 2;
}
}

if(flagI == 0){
System.out.println("Invalid entry, must be one of Y,y,N,n");
System.out.println();
}
}

while(flagA == 0){

System.out.println("Does this session require authentication? Y/N");
System.out.println();
s = reader.next();
System.out.println();

if(s.equals("Y") || s.equals("y") || s.equals("N") || s.equals("n")){

if(s.equals("Y") || s.equals("y")){
securityArray[2] = 1;
flagA = 1;
}else{
securityArray[2] = 0;
flagA = 2;
}
}

if(flagA == 0){
System.out.println("Invalid entry, must be one of Y,y,N,n");
System.out.println();
}
}

//create security string

for(int i = 0; i < 3; i++){
securityArrayString = securityArrayString + String.valueOf(securityArray[i]);
}

//Use security array values to create key, etc based on need

if(securityArray[0] == 1){
System.out.println("C operations needed");
}

if(securityArray[1] == 1){
System.out.println("I operations needed");
}

if(securityArray[2] == 1){
System.out.println("A operations needed");
}

return securityArrayString;
}







}

最佳答案

Cipher 和 KeyGenerator 的 getInstance() 需要不同的输入:

key = KeyGenerator.getInstance("AES").generateKey();
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

关于java - 尝试调用加密算法生成 NoSuchAlgorithmException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47197753/

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