이게 무슨 일이야!

aes256 암복호화 java 인코딩 -> javascript 디코딩 본문

프론트엔드 개발/React

aes256 암복호화 java 인코딩 -> javascript 디코딩

명동섞어찌개 2023. 1. 2. 17:35

 

 

하는 방법 이 페이지 하단에 있음

 

 

https://gist.github.com/kcak11/86f73703eff5bbd2f7bd6b6b3efded34

 

AES/CBC/PKCS5PADDING - Java/Javascript (Encryption & Decryption)

AES/CBC/PKCS5PADDING - Java/Javascript (Encryption & Decryption) - 0000.md

gist.github.com

 

필요한 라이브러리 : crypto-js

https://www.npmjs.com/package/crypto-js

 

crypto-js

JavaScript library of crypto standards.. Latest version: 4.1.1, last published: a year ago. Start using crypto-js in your project by running `npm i crypto-js`. There are 8753 other projects in the npm registry using crypto-js.

www.npmjs.com

 

일단 위 참조 링크의 소스를 베이스로 하되, 각자 상황에 따라서 모드 등을 커스터마이징 하면 됨

 

 

*(링크 폭파를 대비한) 위 링크의 소스 백업

/* AESUtil.java */

package com.ashish.crypto;

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {

  public static void main(String args[]) throws Exception {
    byte[] cipherText = encrypt("Testing AES/CBC/PKCS5PADDING stuff from Java and with JavaScript - some random text".getBytes(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456".getBytes()); // 32 length Key
    System.out.println(new String(cipherText));
    byte[] origText = decrypt(new String(cipherText).getBytes(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456".getBytes());
    System.out.println(new String(origText));
  }

  public static byte[] encrypt(byte[] plainTextData, byte[] secretKey) throws Exception {
    try {
      String iv = new String(secretKey).substring(0, 16);

      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");

      byte[] dataBytes = plainTextData;
      int plaintextLength = dataBytes.length;
      byte[] plaintext = new byte[plaintextLength];
      System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

      SecretKeySpec keyspec = new SecretKeySpec(secretKey, "AES");
      IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

      cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
      byte[] encrypted = cipher.doFinal(plaintext);

      return new String(Base64.getEncoder().encode(encrypted)).getBytes();

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  public static byte[] decrypt(byte[] cipherTextData, byte[] secretKey) throws Exception {
    try {
      String iv = new String(secretKey).substring(0, 16);

      byte[] encrypted = Base64.getDecoder().decode(cipherTextData);

      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
      SecretKeySpec keyspec = new SecretKeySpec(secretKey, "AES");
      IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

      cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

      byte[] original = cipher.doFinal(encrypted);
      String originalString = new String(original);
      return originalString.getBytes();
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}

 

 

/* AESUtil.js */

var secretkey = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123456'; //Length 32
var key = CryptoJS.enc.Utf8.parse(secretkey);
var iv = CryptoJS.enc.Utf8.parse(secretkey.substring(0, 16));

/*-- Encryption --*/
var cipherText = CryptoJS.AES.encrypt("Testing AES/CBC/PKCS5PADDING stuff from Java and with JavaScript - some random text", key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
}).toString();
console.log(cipherText);

/*-- Decryption --*/
var decrypted = CryptoJS.AES.decrypt("RQ/SEoGFF9IHmiMNbo/vlPTHuPWCGgDeEK5ZZBZjk/Kh5AIdgmVEeD42gciaK7gDKMP9odpjjZB/PGjebwpYSLzvEONS2jUiDtGPj7C0iNexmK5v5Gw9C8jsvqJdlmVK", key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
});
console.log(decrypted.toString(CryptoJS.enc.Utf8));

 

Comments