这段代码有什么问题?

时间:2012-03-23 15:27:46

标签: java security encryption

Thanx让一个人帮助我..但编辑代码后还有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.

这些行表示为*

4 个答案:

答案 0 :(得分: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();
    }

}

答案 1 :(得分:2)

您需要在SecretKeySpec包下导入javax.crypto.spec

答案 2 :(得分:1)

您没有正确调用方法,因为您使用参数传递参数类型。参数类型仅在声明方法时显示,而不是在调用方法时显示。

换句话说,它不是

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

而不是

Key key = new SecretKeySpec(keyBytes, RC4);

在尝试将keyBytes变量传递给此方法的参数之前,您当然需要声明并初始化keyBytes变量。

答案 3 :(得分: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;
    }
}