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

标签 java algorithm encryption cryptography encryption-symmetric

尝试编译以下代码时,我每次都会生成 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/

相关文章:

algorithm - 带消除的最大子数组

arrays - 在段树中的一个范围内可以被三整除的子数组

python - pysqlcipher 安装 - SyntaxError : Missing parentheses in call to 'print'

c++ - 使用 crypto++ 库验证 openssl 签名

Powershell 和 AES : If the Salt and IV are both fixed or known, 加密本质上不安全还是更容易破解?

java - Spring MVC 无法解析 View 名称

java - 在android中定义起点?

java - Listview 和 OnItemClickListener(蓝牙设备)

c - 对数组的索引进行排序

java - 并发修改异常不一致