2012-07-04 4 views
0

AES, JCE 및 무제한 256 비트 키에 대한 무제한 정책 파일에 대해 많이 읽은 후에이 Oracle/Sun guide을 구현했습니다.Base64 오류를 해결하는 방법은 무엇입니까?

UPDATE

나는 태양에 의해 제공되는 코드를 결합

/** 
* 
* @author MUDASSIR 
*/ 

import java.security.*; 
import javax.crypto.*; 
import javax.crypto.spec.SecretKeySpec; 
import sun.misc.*; 

public class AESencrp { 

    private static final String ALGO = "AES"; 
// private static final byte[] keyValue = 
//  new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 
//'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' }; 

public static String encrypt(String Data, byte[] keyValue) throws Exception { 
     Key key = generateKey(keyValue); 
     Cipher c = Cipher.getInstance(ALGO); 
     c.init(Cipher.ENCRYPT_MODE, key); 
     byte[] encVal = c.doFinal(Data.getBytes()); 
     String encryptedValue = new BASE64Encoder().encode(encVal); 
     return encryptedValue; 
    } 

    public static String decrypt(String encryptedData, byte[] keyValue) throws Exception { 
     Key key = generateKey(keyValue); 
     Cipher c = Cipher.getInstance(ALGO); 
     c.init(Cipher.DECRYPT_MODE, key); 
     byte[] decodedValue = new BASE64Decoder().decodeBuffer(encryptedData); 
     byte[] decValue = c.doFinal(decodedValue); 
     String decryptedValue = new String(decValue); 
     return decryptedValue; 
    } 
    private static Key generateKey(byte[] keyValue) throws Exception { 
     Key key = new SecretKeySpec(keyValue, ALGO); 
     return key; 
} 

} 

이 잘 작동하지만 난 내 프로젝트를 빌드 할 때마다이 경고를 AESencrp.java

자바 클래스 을 만들어 그
"Base64encoder는 독점 소프트웨어이므로 향후 릴리스에서 제거해야합니다".

내가 base64 인코더를 제거하고 대신 asHex 메서드를 사용하면 (태양 가이드 here에 의해 제공됨) BadPadding 예외가 발생했습니다. 스레드 "주요"javax.crypto.BadPaddingException에서
예외 : 마지막 블록을 감안하지 제대로이이 내가 코드를 시도 할 경우, 메인입니다

/** 
* 
* @author MUDASSIR 
*/ 

import java.security.*; 
import javax.crypto.*; 
import javax.crypto.spec.SecretKeySpec; 
import sun.misc.*; 

public class AESencrp2 { 

    private static final String ALGO = "AES"; 
// private static final byte[] keyValue = 
//  new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 
//'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' }; 

    public static String asHex(byte buf[]) { 
     StringBuilder strbuf = new StringBuilder(buf.length * 2); 
     int i; 

     for (i = 0; i < buf.length; i++) { 
      if (((int) buf[i] & 0xff) < 0x10) { 
       strbuf.append("0"); 
      } 

      strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); 
     } 

     return strbuf.toString(); 
    } 

public static String encrypt(String Data, byte[] keyValue) throws Exception { 
     Key key = generateKey(keyValue); 
     Cipher c = Cipher.getInstance(ALGO); 
     c.init(Cipher.ENCRYPT_MODE, key); 
     byte[] encryptedValue = c.doFinal(Data.getBytes()); 
     return asHex(encryptedValue); 
    } 

    public static String decrypt(String encryptedData, byte[] keyValue) throws Exception { 
     Key key = generateKey(keyValue); 
     Cipher c = Cipher.getInstance(ALGO); 
     c.init(Cipher.DECRYPT_MODE, key); 
     byte[] decValue = c.doFinal(encryptedData.getBytes()); 
     String decryptedValue = new String(decValue); 
     return decryptedValue; 
    } 
    private static Key generateKey(byte[] keyValue) throws Exception { 
     Key key = new SecretKeySpec(keyValue, ALGO); 
     return key; 
} 

} 

base64로 인코더없이 내 코드입니다

패딩

public static void main(String args[]) throws Exception { 

     byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' }; 

     String password = "This is the data that is going to be encrypted"; 

     String passwordEnc = AESencrp.encrypt(password, keyValue); 
     //String passwordEnc = AESencrp2.encrypt(password, keyValue); 

     String passwordDec = AESencrp.decrypt(passwordEnc, keyValue); 
     //String passwordDec = AESencrp.decrypt(passwordEnc, keyValue); 

     System.out.println("Plain Text : " + password); 
     System.out.println("Encrypted Text : " + passwordEnc); 
     System.out.println("Decrypted Text : " + passwordDec); 
} 

문제,

  1. 1 - i가 바스를 사용하면 e64 인코더, 독점적 인.
  2. 2 - sun에서 제공하는 asHex 메서드를 사용하는 경우 잘못된 패딩 예외가 발생합니다.

도와주세요.

+0

정확히 무엇입니까? AES를 사용하여 데이터를 암호화하는 방법은 무엇입니까? 또는 제공 한 소스를 실행하는 방법? 또는 다른 것? –

답변

2

사용자 고유의 Base64 인코딩/디코딩 루틴을 사용하여 문제를 해결할 수 있습니다.

+0

내 자신의 인코딩/디코딩 루틴을 무엇을 의미합니까 ?? –

+1

Base64 인코더/디코더를 사용하거나 코딩합니다. 예를 들어, Apache Commons에는 하나가 있습니다. – belgther

+0

가져 오기 가져 오기 org.apache.commons.codec.binary.Base64; 그리고 새로운 Base64()를 시도했습니다. encode (- byte--); 하지만 작동하지 않습니다. –

관련 문제