当尝试编译以下代码时,无论我分配给算法变量(AES,DESede等)的字符串,每次都会生成NoSuchAlgorithmException。当我第一次尝试为键赋值时,错误总是抛在构造函数中,对于我的生活,我无法弄清楚问题是什么。我已经将整个文件包含在某些相关内容中,但唯一的问题区域似乎是构造函数。如果有人熟悉这个问题并且可以指出我哪里出错了,我会非常感激。
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;
}
}
答案 0 :(得分:1)
Cipher和KeyGenerator的getInstance()需要不同的输入:
key = KeyGenerator.getInstance("AES").generateKey();
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");