[java] 대칭 키(AES) 알고리즘을 사용해서 데이터를 Base64 방식으로 인코딩/디코딩하는 방법.

2022. 11. 16. 15:53Language/java

728x90
728x90

-Base64, Encoding, Decoding 설명 참고.


-AES algorithm 설명 참고.


-AES key를 사용해서 데이터를 Base64 방식으로 암호화/복호화하는 *.java 코드.

//AES algorithm
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

//Base64 enc, dec 
import org.apache.commons.codec.binary.Base64;

public class aesEncDec {

  public static void main(String[] args) {
    String Key = "12345678901234567890123456789012";
    String encMsg = "abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde";

    System.out.println("====================================================");
    System.out.println(" Before encMsg () : " + new String(encMsg));
    System.out.println("====================================================");

    //Encrpytion
    try {
      encMsg = encrypt(Key, encMsg);
    } catch (Exception e) {
      e.printStackTrace();
    }

    System.out.println("====================================================");
    System.out.println(" After encMsg () : " + new String(encMsg));
    System.out.println("====================================================\n");

    String decMsg = encMsg;

    System.out.println("====================================================");
    System.out.println(" Before decMsg () : " + new String(decMsg));
    System.out.println("====================================================");

    //Decryption  
    try {
      decMsg = decrypt(Key, decMsg);
    } catch (Exception e) {
      e.printStackTrace();
    }

    System.out.println("====================================================");
    System.out.println(" After decMsg () : " + new String(decMsg));
    System.out.println("====================================================");

  }

  public static String encrypt(String secretKey, String data) throws Exception {
    //SecretKey 생성.
    SecretKey skey = new SecretKeySpec(secretKey.getBytes(), "AES");
    SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(), "AES");
    //Cipher 객체 인스턴스화
    Cipher cipher = Cipher.getInstance("AES");
    //Cipher 객체 초기화
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    //Encrpytion
    byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));
    return new String(Base64.encodeBase64(encrypted));
  }

  public static String decrypt(String secretKey, String encrypted)
      throws Exception {
    //SecretKey 생성.
    SecretKey skey = new SecretKeySpec(secretKey.getBytes(), "AES");
    SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(), "AES");
    //Cipher 객체 인스턴스화
    Cipher cipher = Cipher.getInstance("AES");
    //Cipher 객체 초기화
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    //Decryption
    byte[] encryptedBytes = Base64.decodeBase64(encrypted.getBytes());
    byte[] originalBytes = cipher.doFinal(encryptedBytes);
    return new String(originalBytes, "UTF-8");
  }
}

-Apache Commons lib(org.apache.commons.codec.binary.Base64)을 사용하여, Base64 방식으로 인코딩/디코딩 수행하는 예제이기 때문에, 하단의 링크에서 라이브러리(commons-codec-1.7.jar) 다운로드.


-컴파일 수행.

$ javac -classpath commons-codec-1.7.jar -encoding UTF8 aesEncDec.java

-컴파일한 클래스 실행.

$ java -classpath .;commons-codec-1.7.jar aesEncDec
====================================================
 Before encMsg () : abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde
====================================================
====================================================
 After encMsg () : +LE3YUHMeJXCOtREOUfuNvZ5udNeLqWzI2elolcx+xO8R5NACufXdK8f8tSlWCA5RNAkI+pVdawUqMs4wt6LwQ==
====================================================

====================================================
 Before decMsg () : +LE3YUHMeJXCOtREOUfuNvZ5udNeLqWzI2elolcx+xO8R5NACufXdK8f8tSlWCA5RNAkI+pVdawUqMs4wt6LwQ==
====================================================
====================================================
 After decMsg () : abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde
====================================================
728x90
728x90