简体   繁体   English

RC4算法的KeyGenerator异常

[英]KeyGenerator exception with RC4 algorithm

The code is very easy and clear, but it throws exception at 该代码非常简单明了,但是在

KeyGenerator keyGen = KeyGenerator.getInstance("RC4");

and

Cipher aesCipher = Cipher.getInstance("RC4");

the exception is : unreported exception java.security.NoSuchAlgorithmException; 异常是:未报告的异常java.security.NoSuchAlgorithmException; must be caught or declared to be thrown 必须被抓住或宣布被抛出

import java.io.*;
import java.security.*;
import javax.crypto.*;
import sun.misc.BASE64Encoder;


public class RCCC4 {
public static void main(String[] args) {
    String strDataToEncrypt = new String();
    String strCipherText = new String();
    String strDecryptedText = new String();

    try{ 
    KeyGenerator keyGen = KeyGenerator.getInstance("RC4");
    SecretKey secretKey = keyGen.generateKey();
    Cipher aesCipher = Cipher.getInstance("RC4");
    aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);
    strDataToEncrypt = "Hello World of Encryption using RC4 ";
    byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
    byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
    strCipherText = new BASE64Encoder().encode(byteCipherText);
    System.out.println("Cipher Text generated using RC4 is " +strCipherText);
    aesCipher.init(Cipher.DECRYPT_MODE,secretKey,aesCipher.getParameters());
    byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
    strDecryptedText = new String(byteDecryptedText);
    System.out.println(" Decrypted Text message is " +strDecryptedText);
    }
    catch (NoSuchPaddingException noSuchPad)
        {
            System.out.println(" No Such Padding exists " + noSuchPad);
        }

    catch (InvalidKeyException invalidKey)
        {
                System.out.println(" Invalid Key " + invalidKey);
        }

    catch (BadPaddingException badPadding)
        {
                System.out.println(" Bad Padding " + badPadding);
        }

    catch (IllegalBlockSizeException illegalBlockSize)
        {
                System.out.println(" Illegal Block Size " + illegalBlockSize);
        }

    catch (InvalidAlgorithmParameterException invalidParam)
        {
                System.out.println(" Invalid Parameter " + invalidParam);
        }

}
   }

The code works and is okay as it stands, you just need to add one more catch to catch NoSuchAlgorithmException - which won't ever occur in your program. 该代码可以正常运行,并且只需要添加一个catch即可捕获NoSuchAlgorithmException这在您的程序中永远不会发生。

Because the algorithm name is passed as a String, the method getInstance() could possibly throw NoSuchAlgorithmException when the name was wrong. 因为算法名称是作为字符串传递的,所以当名称错误时,方法getInstance()可能会引发NoSuchAlgorithmException It just wouldn't know what to do with an unknown algorithm. 它只是不知道如何处理未知算法。 This is not your case, but the compiler has to be sure to be happy. 这不是您的情况,但是编译器必须确保满意。

实际上,通过此链接可以告诉您KeyGenerator确实支持RC4,但是将“ ARCFOUR”指定为算法名称

在这里尝试使用ARCFOUR代替RC4 文档

You'll have to do your homework yourself (please mark the question as such btw.) but here's a hint on the exception: 您将需要自己做家庭作业(请顺便说一句),但这是一个有关异常的提示:

A java.security.NoSuchAlgorithmException means that the algorithm you want to use (RC4 in your case) is not supported on that machine. java.security.NoSuchAlgorithmException表示该计算机上不支持您要使用的算法(在您的情况下为RC4)。 That might either be due to a typo (the name might be rc4 or something else) or the algorithm isn't provided out-of-the-box and you'd have to implement/add it yourself (which I assume is the case here). 这可能是由于拼写错误(名称可能是rc4或其他名称)或未提供现成的算法,您必须自己实现/添加(我认为是这种情况)这里)。

You just need to catch this exception as well for it to work out.... 您只需要捕获此异常也可以解决该问题。

catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM