我是安全新手,我尝试了很多来删除异常(在代码下方)。RC2 和 RC6 密码都给出了这个例外。输入应该是 128 位String
和 128 位的密钥,输出应该是 128 位密文。
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;
public class RC2Encrypt
{
public static void main(String args []) throws Exception
{
Scanner s=new Scanner(System.in);
System.out.println("Enter PlainTextString:");
String input=s.nextLine();
System.out.println();
System.out.println("Enter 16 digit key:");
String strPassword=s.nextLine();
SecretKeySpec key = new SecretKeySpec(strPassword.getBytes(), "RC2");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(strPassword.getBytes());
Cipher cipher = Cipher.getInstance("RC2");
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] encrypted = cipher.doFinal(input.getBytes());
String b1 = new String(encrypted);
System.out.println("Original string: " + input);
System.out.println("Encrypted string: " + b1);
}
}
这将产生以下异常:
Exception in thread "main" java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 8 bytes long
at com.sun.crypto.provider.SunJCE_f.a(DashoA13*..)
at com.sun.crypto.provider.RC2Cipher.engineInit(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at RC2Encrypt.main(RC2Encrypt.java:40)