티스토리 뷰
package asdfasds;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AES128 {
private String ips;
private Key keySpec;
public AES128(String key) {
try {
byte[] keyBytes = new byte[16];
byte[] b = key.getBytes("UTF-8");
System.arraycopy(b, 0, keyBytes, 0, keyBytes.length);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
this.ips = key.substring(0, 16);
this.keySpec = keySpec;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public String encrypt(String str) {
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec,
new IvParameterSpec(ips.getBytes()));
byte[] encrypted = cipher.doFinal(str.getBytes("UTF-8"));
String Str = new String(Base64.encodeBase64(encrypted));
return Str;
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IllegalBlockSizeException | BadPaddingException
| UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public String decrypt(String str) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec,
new IvParameterSpec(ips.getBytes("UTF-8")));
byte[] byteStr = Base64.decodeBase64(str.getBytes());
String Str = new String(cipher.doFinal(byteStr), "UTF-8");
return Str;
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IllegalBlockSizeException | BadPaddingException
| UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
출처: https://boxfoxs.tistory.com/270 [박스여우 - BoxFox]
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AES128 {
private String ips;
private Key keySpec;
public AES128(String key) {
try {
byte[] keyBytes = new byte[16];
byte[] b = key.getBytes("UTF-8");
System.arraycopy(b, 0, keyBytes, 0, keyBytes.length);
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
this.ips = key.substring(0, 16);
this.keySpec = keySpec;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public String encrypt(String str) {
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec,
new IvParameterSpec(ips.getBytes()));
byte[] encrypted = cipher.doFinal(str.getBytes("UTF-8"));
String Str = new String(Base64.encodeBase64(encrypted));
return Str;
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IllegalBlockSizeException | BadPaddingException
| UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public String decrypt(String str) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec,
new IvParameterSpec(ips.getBytes("UTF-8")));
byte[] byteStr = Base64.decodeBase64(str.getBytes());
String Str = new String(cipher.doFinal(byteStr), "UTF-8");
return Str;
} catch (NoSuchAlgorithmException | NoSuchPaddingException
| InvalidKeyException | InvalidAlgorithmParameterException
| IllegalBlockSizeException | BadPaddingException
| UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
출처: https://boxfoxs.tistory.com/270 [박스여우 - BoxFox]
'자바(Java)' 카테고리의 다른 글
[JAVA] 자바 rmi(remote method invocation) 샘플 예제 코드 (3) | 2020.01.17 |
---|---|
자바 jsp 파일 다운로드 (0) | 2019.12.12 |
톰캣 로그 설정 [WAS] Tomcat 로그 설정(무분별하게 커지는 catalina.out 용량 설정하기) (0) | 2019.12.04 |
[Java] 암호화를 위한 Cipher 클래스 (Java로 AES, RSA 암호화, 복호화) (0) | 2019.11.26 |
파일명 밀리세컨드 (0) | 2019.11.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 이클립스
- 문자열
- Python
- ocajp
- ocjap
- xe addon
- C언어
- KG
- ocpjp
- XE
- webix
- 인포믹스
- XE3
- 파이썬
- esql
- 자바 셀레니움
- 스크래핑
- proc
- php
- C
- EC
- 포인터
- MySQL
- 오라클
- 프로씨
- 자바
- JDBC
- 파싱
- 자바 smtp
- xe애드온
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함