달력

42024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

JAVA AES128 암호화 복호화 소스

package test;

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


public class FirstApp {

 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  
  FirstApp f = new FirstApp(); 
  
  System.out.println(f.encrypt("TEST08"));
  
 }
 private final String KEYNAME = "nfaator!plaeemo!";
private final String ALGORITHM = "AES"; public static final String AES_ECB_NOPADDING = "AES/ECB/NoPadding"; public String encrypt(final String source) throws Exception { byte[] eArr = null; SecretKeySpec skeySpec = new SecretKeySpec(KEYNAME.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(AES_ECB_NOPADDING); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); eArr = cipher.doFinal(this.addPadding(source.getBytes())); return fromHex(eArr); } public byte[] encryptToByteArray(final String source) throws Exception { byte[] eArr = null; SecretKeySpec skeySpec = new SecretKeySpec(KEYNAME.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(AES_ECB_NOPADDING); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); eArr = cipher.doFinal(this.addPadding(source.getBytes())); return eArr; } public String GetEncryptString(String source) throws Exception { StringBuilder sbResult = new StringBuilder(); byte[] eArr = null; SecretKeySpec skeySpec = new SecretKeySpec(KEYNAME.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(AES_ECB_NOPADDING); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); eArr = cipher.doFinal(this.addPadding(source.getBytes())); for (byte b : eArr) { sbResult.append(String.format("%02x", b)); } return sbResult.toString(); } public String decrypt(final String source) throws Exception { Cipher cipher = Cipher.getInstance(AES_ECB_NOPADDING); SecretKeySpec skeySpec = new SecretKeySpec(KEYNAME.getBytes(), ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] eArr = this.removePadding(cipher.doFinal(this.toBytes(source))); return new String(eArr); } private byte[] removePadding(final byte[] pBytes) { int pCount = pBytes.length; int index = 0; boolean loop = true; while (loop) { if (index == pCount || pBytes[index] == 0x00) { loop = false; index--; } index++; } byte[] tBytes = new byte[index]; System.arraycopy(pBytes, 0, tBytes, 0, index); return tBytes; } private byte[] toBytes(final String pSource) { StringBuffer buff = new StringBuffer(pSource); int bCount = buff.length() / 2; byte[] bArr = new byte[bCount]; for (int bIndex = 0; bIndex < bCount; bIndex++) { bArr[bIndex] = (byte) Long.parseLong(buff.substring(2 * bIndex, (2 * bIndex) + 2), 16); } return bArr; } private byte[] addPadding(final byte[] pBytes) { int pCount = pBytes.length; int tCount = pCount + (16 - (pCount % 16)); byte[] tBytes = new byte[tCount]; System.arraycopy(pBytes, 0, tBytes, 0, pCount); for (int rIndex = pCount; rIndex < tCount; rIndex++) { tBytes[rIndex] = 0x00; } return tBytes; } public String fromHex(byte[] pBytes) { int pCount = pBytes.length; StringBuffer buff = new StringBuffer(pCount * 2); for (int pIndex = 0; pIndex < pCount; pIndex++) { if (((int) pBytes[pIndex] & 0xff) < 0x10) { buff.append(0); } buff.append(Long.toString((int) pBytes[pIndex] & 0xff, 16)); } return buff.toString(); } }

[출처] C# JAVA AES128 암호화 복호화 소스|작성자 실버캐슬

'Dev Language > JAVA' 카테고리의 다른 글

자바 엑셀 내리기 BigGrid 10만 라인 약 2초  (0) 2018.11.26
JAVA 바코드 생성  (0) 2018.11.16
숫자형식 포멧 방법  (0) 2017.07.13
AES, SHA 암호화 1, JAVA  (0) 2017.03.30
ResultSet을 컬렉션으로 반환  (0) 2016.06.16
Posted by 타카스 류지
|