2
128 비트 AES 키에서 256 비트 AES 키로 앱을 업그레이드하려고합니다. 그러나 라인 54를 128에서 256으로 변경하면 다음 키 크기 오류가 발생합니다.Java AES 256 키로 암호화/암호 해독
java.security.InvalidKeyException : 잘못된 키 크기
내 응용 프로그램은 더 이상 키를 생성한다는 사실에 의해 입증 JCE 파일이 제대로 설치되어 있습니다.
package com.company;
import com.hazelcast.util.Base64;
import javax.crypto.*;
import javax.crypto.spec.*;
class Encryption {
public static String encrypt (String strKey, String strIv, String str) {
String secret = "";
try{
byte[] key = Base64.decode(strKey.getBytes());
byte[] iv = Base64.decode(strIv.getBytes());
SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
secret = new String(Base64.encode(cipher.doFinal(str.getBytes())));
}
catch(Exception e){
e.printStackTrace();
}
return secret;
}
public static String decrypt (String strKey, String strIv, String str) {
String secret = "";
try{
byte[] key = Base64.decode(strKey.getBytes());
byte[] iv = Base64.decode(strIv.getBytes());
SecretKeySpec keyspec = new SecretKeySpec(key, "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keyspec,ivspec);
secret = new String(cipher.doFinal(new Base64().decode(str.getBytes())));
}
catch(Exception e){
e.printStackTrace();
}
return secret;
}
public static void main(String[] argv) {
String strIv = "18A5Z/IsHs6g8/65sBxkCQ==";
String strKey = "";
int keyStrength = 256;
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(keyStrength);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
strKey = new String(new Base64().encode(raw));
System.out.println("Secret key is: " + strKey);
} catch (Exception ex) {
System.out.println(ex.toString());
}
String message = "My, it's a lovely day today!!!";
String encrypted = Encryption.encrypt(strKey, strIv, message);
System.out.println("Encrypted string is: " + encrypted);
System.out.println("Decrypted string is: " + Encryption.decrypt(strKey, strIv, encrypted));
}
}
내가 다른 게시물에 언급 된 "AES/CBC/PKCS7Padding"암호화 방법을 본 적이
, 그러나 그것은 단순히 나에게이 예외를 가져옵니다
java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
at javax.crypto.Cipher.getInstance(Cipher.java:540)
at com.company.Encryption.encrypt(Encryption.java:17)
at com.company.Encryption.main(Encryption.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
at javax.crypto.Cipher.getInstance(Cipher.java:540)
at com.company.Encryption.decrypt(Encryption.java:39)
at com.company.Encryption.main(Encryption.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
InvalidKeyException을 throw하는 행은 무엇입니까? –
[InvalidKeyException 잘못된 키 크기]의 복제본이있을 수 있음 (http://stackoverflow.com/questions/3862800/invalidkeyexception-illegal-key-size) –
* "JCE 파일을 올바르게 설치했습니다"* - 무제한 강도 정책을 의미하는 경우 파일, 당신은 정말로 * 정책 파일을 설치 한 JVM에서 코드를 실행하고 있습니까? 확실하지 않은 경우, 모든 JRE (글로벌 JRE와 JDK 내의 JRE의 양쪽 모두)에 인스톨 할 필요가 있습니다. –