728x90

AES256 암호화/복호화에 대한 예제이다.

Java 에서 AES256 암호화/복호화시에는 key를 16바이트 키를 사용해도 문제가 되지 않는다.

Java 에서 암호화 한 것을 PHP에서 복호화할 때에는 16바이트 키를 사용한 것은 엉뚱한 결과를 반환하더라.

key 길이가 다르면 PHP 복호화시 문제가 된다는 걸 잊지마시길 !!

 

Java 에서 암호화/복호화 하는 경우와 Android 에서 사용하는 경우 import 문이 달라서인지 코드가 약간 다르다.

 

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 java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;
 
public class AES256Cipher {
    private static String key = "testggisuitken1959imy91jb7076jas"// AES 암호화  키
//    private static String key = "cde0123456789abf"; // 16 바이트키
    public static byte[] ivBytes = { 0x000x000x000x000x000x000x000x000x000x000x000x000x000x000x000x00 };
 
    public static String AES_Encode(String str, String key)    throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
 
        byte[] textBytes = str.getBytes("UTF-8");
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
 
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(cipher.doFinal(textBytes));
    }
 
    public static String AES_Decode(String str, String key)    throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
 
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] textBytes =decoder.decode(str);
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(key.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");
    }
 
    // 암호화
    public static String encrypt(String data) {
        try {
            data = AES_Encode(data, AES256Cipher.key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return data;
    }
 
    // 복호화
    public static String decrypt(String data) {
        try {
            data = AES_Decode(data, AES256Cipher.key);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return data;
    }
}
 

 

 

PHP 코드

<?php
ini_set("display_startup_errors"1);
ini_set("display_errors"1);
error_reporting(E_ALL);
 
$key = "testggisuitken1959imy91jb7076jas";
//$key = "cde0123456789abf";
 
$encypted_text = "t7VJP1+3mcZlcJsh9nChPSUYOZSw+QIl2UFJlvkis8Y=";
$text = "암호화 예제";
 
echo '평문 : '.$text.'<br/>';
echo 'PHP 암호화  : '.AES_encrypt($text).'<br/>';
echo 'Java 암호문 : '.$encypted_text.'<br/>';
echo 'PHP  복호화 : '.AES_decrypt($encypted_text).'<br/>';
 
// AES 암호화
function AES_encrypt($plain_text){
    global $key;
    $encryptedMessage = openssl_encrypt($plain_text"aes-256-cbc"$keytrue,str_repeat(chr(0), 16));
    return base64_encode($encryptedMessage);
}
 
// AES 복호화
function AES_decrypt($base64_text){
    global $key;
    $decryptedMessage = openssl_decrypt(base64_decode($base64_text), "aes-256-cbc"$keytrue, str_repeat(chr(0), 16));
    return $decryptedMessage;
}
 
?>
 

 

Java 에서 암호화하고 PHP 에서 복호화하는 것 뿐만 아니라

PHP 에서 암호화하고 Java 에서 복호화하는 것도 문제없이 잘 된다.

 

블로그 이미지

Link2Me

,