2012-03-23 75 views
-1

感谢名单向埃夫里一个可以帮助我..但仍有2问题编辑代码这段代码有什么问题?

import java.io.*; 
import java.math.*; 
import java.security.*; 
import javax.crypto.*; 
import javax.crypto.spec.*; 

public class RCC4 { 
    public RCC4(){} 

    public static void main(String[] args)throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException{ 
     String test = "testisperfect"; 
     System.out.println(RCC4.keyGet()); 
     byte b[] = RCC4.keyGet().getBytes(); 
     byte plain[] = test.getBytes(); 
     **byte c[] = RCC4.encrypt(plain,b);** 
     **byte p[] = RCC4.decrypt(c,b);** 

     **System.out.println(new String(c)) ; 
     System.out.println(new String(p));** 

    } 
    public static byte[] encrypt(byte[] plaintext,byte[] keyBytes) 
    { 
     byte[] e = null; 
     try 
     { 
      Key key = new SecretKeySpec(keyBytes,"RC4"); 
      Cipher enCipher = Cipher.getInstance("RC4"); 
      **enCipher.init(Cipher.ENCRYPT_MODE ,key);** 
      e = enCipher.doFinal(plaintext);   
     } 
     catch(Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
     return e; 
    } 
    public static byte[] decrypt(byte[] ciphertext,byte[] keyBytes) 
    { 
     byte de[] = null; 
     try 
     { 
      Key key = new SecretKeySpec(keyBytes,"RC4"); 
      Cipher deCipher = Cipher.getInstance("RC4"); 
      **deCipher.init(Cipher.DECRYPT_MODE, key);** 
      de = deCipher.doFinal(ciphertext); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return de; 

    } 

    public static Key getKey() 
    { 
     Key key = null; 
     try 
     { 
      SecureRandom sr = new SecureRandom(); 
      KeyGenerator kg = KeyGenerator.getInstance("RC4"); 
      kg.init(128,sr); 
      key = kg.generateKey(); 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return key; 
    } 
    public static String keyGet() 
    { 
     Key k = RCC4.getKey(); 
     byte[] b = k.getEncoded(); 
    BigInteger big = new BigInteger(b); 
     String s = big.toString(); 
     return s; 
    } 

    } 

后,当我按下“生成文件”,它说的过程完成,但在运行文件的消息说

112670544188765215715791498302542646231 


java.security.InvalidKeyException: Illegal key size or default parameters 
     at RCC4.encrypt(RCC4.java:37) 
    at RCC4.main(RCC4.java:23) 


java.security.InvalidKeyException: Illegal key size or default parameters 
    at RCC4.decrypt(RCC4.java:53) 
    at RCC4.main(RCC4.java:24) 


Exception in thread "main" java.lang.NullPointerException 
    at java.lang.String.<init>(String.java:479) 
    at RCC4.main(RCC4.java:26) 

Process completed. 

这些行显示为*

+0

我不知道为什么downvotes。他为需要回答的问题提供了足够的信息。 – 2012-03-23 15:39:30

+0

唯一可能出错的是,考虑到简单的错误,他提供了太多的信息。但是,降薪对此有点苛刻。 – Jochen 2012-03-23 16:05:32

+0

...... ** ** byte c [] = RCC4.encrypt(plain,b); **'请发布[SSCCE](http://sscce.org/)。另外,请使用代码格式来输出错误。 – 2012-03-24 17:38:57

回答

2

您需要导入SecretKeySpec,这是在包javax.crypto.spec下。

+0

@Andrew和Chickei:你们都是对的。抱歉! – 2012-03-23 16:21:58

1

由于您使用参数传入参数类型,因此您没有正确调用方法。仅当声明是一种方法时才显示参数类型,而不是在调用方法时显示。

换句话说,它不是

Key key = new SecretKeySpec(byte[] keyBytes, RC4); 

它代替

Key key = new SecretKeySpec(keyBytes, RC4); 

你当然需要有一个keyBytes变量声明和初始化试图将其传递给该方法的参数之前。

+0

Thanx非常.. ..帮你.. – 2012-03-23 22:22:45

+0

但是发现一个新问题,它说 – 2012-03-23 22:24:26

+0

java.security.InvalidKeyException:非法的密钥大小或默认参数线程“main”中的异常java.lang.NullPointerException at java.lang。串。 (String.java:479) at RCC4.main(RCC4.java:26) – 2012-03-23 22:25:29

3

回答到原来的问题:

Key key = new SecretKeySpec(byte[]keyBytes,RC4); 

应该

Key key = new SecretKeySpec(keyBytes, "RC4"); 

此外,

deCipher.init(Cipher.WHATEVER, keyBytes); 

应该

deCipher.init(Cipher.WHATEVER, key); 

然后编译,但是应用程序逻辑仍然存在一些问题。这取决于你了:)。

回答新问题:

问题是的SecretKeySpec不需要的用法。介于getKey()keyGet()之间的所有byte[]游戏和SecretKeySpec之间的某处出错了。我没有耐心跟踪它,所以我只是删除它,并使代码更具可读性,以确保我没有错过任何东西。我认为你仍然会理解它,因为它基本上仍然是你的代码,现在它变得更简单。

import java.security.*; 
import javax.crypto.*; 

public class RCC4 { 

    public static void main(String[] args) throws Exception { 
     String plain = "testisperfect"; 
     Key key = RCC4.getKey(); 
     String encrypted = RCC4.encrypt(plain, key); 
     String decrypted = RCC4.decrypt(encrypted, key); 
     System.out.println(encrypted); 
     System.out.println(decrypted); 
    } 

    private static String rc4(String plaintext, int mode, Key key) throws Exception { 
     Cipher cipher = Cipher.getInstance("RC4"); 
     cipher.init(mode, key); 
     return new String(cipher.doFinal(plaintext.getBytes())); 
    } 

    public static String encrypt(String plaintext, Key key) throws Exception { 
     return rc4(plaintext, Cipher.ENCRYPT_MODE, key); 
    } 

    public static String decrypt(String ciphertext, Key key) throws Exception { 
     return rc4(ciphertext, Cipher.DECRYPT_MODE, key); 
    } 

    public static Key getKey() throws Exception { 
     KeyGenerator kg = KeyGenerator.getInstance("RC4"); 
     SecureRandom sr = new SecureRandom(); 
     kg.init(128, sr); 
     return kg.generateKey(); 
    } 

} 
+0

呃,顺便说一句,我只是想出了你在原代码中的错误。 BigInteger小提琴搞砸了。你可以使用'return new String(RCC4.getKey()。getEncoded());'作为你的keyGet()方法。 但是,我仍然认为我的新代码是一个改进。 – 2012-03-25 08:50:52

1
package test; 

import java.io.*; 
import java.math.*; 
import java.security.*; 

import javax.crypto.*; 
import javax.crypto.spec.*; 

public class RCC4 { 

    public RCC4() { 
    } 

    public static void main(String[] args) throws NoSuchAlgorithmException, 
      NoSuchPaddingException, InvalidKeyException, IOException { 
     String test = "testisperfect"; 
     System.out.println(RCC4.keyGet()); 
     byte b[] = RCC4.keyGet().getBytes(); 
     byte plain[] = test.getBytes(); 
     byte c[] = RCC4.encrypt(plain, b); 
     byte p[] = RCC4.decrypt(c, b); 

     System.out.println(new String(c)); 
     System.out.println(new String(p)); 
    } 

    public static byte[] encrypt(byte[] plaintext, byte[] keyBytes) { 
     byte[] e = null; 
     try { 
      Key key = new SecretKeySpec(keyBytes, "RC4"); 
      Cipher enCipher = Cipher.getInstance("RC4"); 
      enCipher.init(Cipher.ENCRYPT_MODE, key); 
      e = enCipher.doFinal(plaintext); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return e; 
    } 

    public static byte[] decrypt(byte[] ciphertext, byte[] keyBytes) { 
     byte de[] = null; 
     try { 
      Key key = new SecretKeySpec(keyBytes, "RC4"); 
      Cipher deCipher = Cipher.getInstance("RC4"); 
      deCipher.init(Cipher.DECRYPT_MODE, RCC4.getKey()); 
      de = deCipher.doFinal(ciphertext); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return de; 
    } 

    public static Key getKey() { 
     Key key = null; 
     try { 
      SecureRandom sr = new SecureRandom(); 
      KeyGenerator kg = KeyGenerator.getInstance("RC4"); 
      kg.init(128, sr); 
      key = kg.generateKey(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return key; 
    } 

    public static String keyGet() { 
     Key k = RCC4.getKey(); 
     byte[] b = k.getEncoded(); 
     BigInteger big = new BigInteger(b); 
     String s = big.toString(); 
     return s; 
    } 
} 
+0

该代码无法正常工作,我不知道每次发生错误时都会出现相同的错误消息Illagle密钥大小或默认参数 – 2012-03-24 17:32:29