不知道如何解决此错误“ java.security.InvalidKeyException”

时间:2019-04-18 12:39:23

标签: java encryption integer rc4-cipher

我正在Java中使用RC4加密来加密纯文本,然后使用密钥将其转换为密文。但是,在运行程序时,我将在下面遇到此错误。我不知道如何解决该问题。非常感谢您的帮助。谢谢!

  

线程“ main”中的异常java.security.InvalidKeyException:密钥长度必须在40到1024位之间       位于com.sun.crypto.provider.ARCFOURCipher.getEncodedKey(ARCFOURCipher.java:202)       在com.sun.crypto.provider.ARCFOURCipher.engineGetKeySize(ARCFOURCipher.java:259)       在javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1067)       在javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1025)       在javax.crypto.Cipher.implInit(Cipher.java:801)       在javax.crypto.Cipher.chooseProvider(Cipher.java:864)       在javax.crypto.Cipher.init(Cipher.java:1249)       在javax.crypto.Cipher.init(Cipher.java:1186)       在Host(Host.java:80)       在Host.main(Host.java:134)

        byte[] decodedKey = Base64.getDecoder().decode(pwwithoutspace);

        SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "RC4"); 
        /* Encryption */

        byte[] plainTextByteArray = gxmodp.getBytes();

        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, originalKey);
        byte[] encrypted = cipher.doFinal(plainTextByteArray);
        out.writeUTF("Ciphertext from Host: " + encrypted); // send to Client
        cipher.init(Cipher.DECRYPT_MODE, originalKey);
        byte[] decrypted = cipher.doFinal(encrypted);

        System.out.println("Decrypted: " + new String(decrypted));

0 个答案:

没有答案