반응형

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

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기