我是新來的安全人員,並且我嘗試了很多以移除異常(在代碼下面)。 RC2和RC6密碼都給出了這個例外。輸入應爲128位String
,密鑰爲128位,輸出應爲128位密文。java.security.InvalidAlgorithmParameterException:在RC6以及RC2中
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)
我試圖改變參數,我也使用keyGenerator但也有同樣的錯誤,在執行過程中,我看到這個異常 – user1972920
你沒有向我們展示異常。除非你這樣做,否則不應該試圖回答這個問題。 –
線程「main」中的@GregS異常java.security.InvalidAlgorithmParameterException:錯誤的IV長度:必須是8字節長在com.sun.crypto.provider.SunJCE_f.a(DashoA13 * ..) 必須是8字節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) – user1972920