반응형
AES 암복호화
- IOS url-encode로 인해 '+'가 공백으로 바뀌는 증상 대응코드
- key값의 길이에 따라 AES128, AES192, AES256 으로 구분됨
- AES128 : 키값 16bytes
- AES192 : 키값 24bytes
- AES256 : 키값 32bytes
- Android, IOS 대응(하단 참조)
예제 코드(AES256)
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AES256Util {
//키값 32바이트: AES256(24: AES192, 16: AES128)
public static String secretKey = "01234567890123450123456789012345";
public static byte[] ivBytes = "0123456789012345".getBytes();
//AES256 암호화
public static String aesEncode(String str) {
try {
byte[] textBytes = str.getBytes("UTF-8");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "AES");
Cipher cipher = null;
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
return Base64.getMimeEncoder().encodeToString(cipher.doFinal(textBytes));
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
//AES256 복호화
public static String aesDecode(String str) {
try {
//IOS url-encode 대응
str = str.replace(" ", "+");
byte[] textBytes = Base64.getMimeDecoder().decode(str);
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
return new String(cipher.doFinal(textBytes), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
Android
srctree.tistory.com/58
Android AES(128, 192, 256) 암복호화
AES 암복호화 key값의 길이에 따라 AES128, AES192, AES256 으로 구분됨 AES128 : 키값 16bytes AES192 : 키값 24bytes AES256 : 키값 32bytes 예제 코드(AES256) import android.util.Base64; import java.securi..
srctree.tistory.com
IOS Swift
srctree.tistory.com/59?category=917274
IOS Swift AES(128, 192, 256) 암복호화
AES 암복호화 CryptoSwift 라이브러리 Pod 추가 추가 : pod 'CryptoSwift', '~> 1.3.8' 설치 : pod install 링크 : https://github.com/krzyzanowskim/CryptoSwift key값의 길이에 따라 AES128, AES192, AES256 으..
srctree.tistory.com
반응형
'웹 > jsp' 카테고리의 다른 글
jQuery [이벤트] 페이지 로드, 클릭, Text변경 (0) | 2021.11.18 |
---|---|
JSP 세션(Session)을 이용한 중복 로그인 처리 (0) | 2021.11.18 |
최근댓글