Enjoy Development

지식관리, 메모장, 기억저장소, 참고자료, ...

Java AES 암호화/복호화 샘플 (ECB/CBC)

2014.02.25 14:28 | 조회 수 : 308666

AES 암호화/복호화 샘플

    - ECB 블럭 암호화

    - CBC 블럭 암호화 (IV사용)

 

소스

import java.util.Arrays;

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

public class AesTest {

    private static byte[] aesEncryptEcb(String sKey, String sText) {
        byte[] key = null;
        byte[] text = null;
        byte[] encrypted = null;
        final int AES_KEY_SIZE_128 = 128;

        try {
            // UTF-8
            key = sKey.getBytes("UTF-8");

            // Key size 맞춤 (128bit, 16byte)
            key = Arrays.copyOf(key, AES_KEY_SIZE_128 / 8);

            // UTF-8
            text = sText.getBytes("UTF-8");

            // AES/EBC/PKCS5Padding
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
            encrypted = cipher.doFinal(text);
        } catch (Exception e) {
            encrypted = null;
            e.printStackTrace();
        }

        return encrypted;
    }

    private static byte[] aesDecryptEcb(String sKey, byte[] encrypted) {
        byte[] key = null;
        byte[] decrypted = null;
        final int AES_KEY_SIZE_128 = 128;

        try {
            // UTF-8
            key = sKey.getBytes("UTF-8");

            // Key size 맞춤 (128bit, 16byte)
            key = Arrays.copyOf(key, AES_KEY_SIZE_128 / 8);

            // AES/EBC/PKCS5Padding
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));
            decrypted = cipher.doFinal(encrypted);
        } catch (Exception e) {
            decrypted = null;
            e.printStackTrace();
        }

        return decrypted;
    }

    private static byte[] aesEncryptCbc(String sKey, String sText) {
        return aesEncryptCbc(sKey, sText, "");
    }

    private static byte[] aesDecryptCbc(String sKey, byte[] encrypted) {
        return aesDecryptCbc(sKey, encrypted, "");
    }

    private static byte[] aesEncryptCbc(String sKey, String sText, String sInitVector) {
        byte[] key = null;
        byte[] text = null;
        byte[] iv = null;
        byte[] encrypted = null;
        final int AES_KEY_SIZE_128 = 128;

        try {
            // UTF-8
            key = sKey.getBytes("UTF-8");

            // Key size 맞춤 (128bit, 16byte)
            key = Arrays.copyOf(key, AES_KEY_SIZE_128 / 8);

            // UTF-8
            text = sText.getBytes("UTF-8");

            if (sInitVector != null) {
                // UTF-8
                iv = sInitVector.getBytes("UTF-8");

                // Key size 맞춤 (128bit, 16byte)
                iv = Arrays.copyOf(iv, AES_KEY_SIZE_128 / 8);

                // AES/EBC/PKCS5Padding
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                IvParameterSpec ips = new IvParameterSpec(iv);
                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), ips);
                encrypted = cipher.doFinal(text);
            } else {
                // AES/EBC/PKCS5Padding
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
                encrypted = cipher.doFinal(text);
            }
        } catch (Exception e) {
            encrypted = null;
            e.printStackTrace();
        }

        return encrypted;
    }

    private static byte[] aesDecryptCbc(String sKey, byte[] encrypted, String sInitVector) {
        byte[] key = null;
        byte[] iv = null;
        byte[] decrypted = null;
        final int AES_KEY_SIZE_128 = 128;

        try {
            // UTF-8
            key = sKey.getBytes("UTF-8");

            // Key size 맞춤 (128bit, 16byte)
            key = Arrays.copyOf(key, AES_KEY_SIZE_128 / 8);

            if (sInitVector != null) {
                // UTF-8
                iv = sInitVector.getBytes("UTF-8");

                // Key size 맞춤 (128bit, 16byte)
                iv = Arrays.copyOf(iv, AES_KEY_SIZE_128 / 8);

                // AES/EBC/PKCS5Padding
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                IvParameterSpec ips = new IvParameterSpec(iv);
                cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), ips);
                decrypted = cipher.doFinal(encrypted);
            } else {
                // AES/EBC/PKCS5Padding
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));
                decrypted = cipher.doFinal(encrypted);
            }
        } catch (Exception e) {
            decrypted = null;
            e.printStackTrace();
        }

        return decrypted;
    }

    private static String toHexString(byte[] b) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < b.length; i++) {
            sb.append(String.format("%02X", b[i]));
            if ((i + 1) % 16 == 0 && ((i + 1) != b.length)) {
                sb.append(" ");
            }
        }

        return sb.toString();
    }

    public static void main(String[] args){
        String sKey = "ABC";
        String sText = "1234123412341234가나다";
        String sInitVector = "123가나다";
        byte[] encrypted = null;
        byte[] decrypted = null;

        try {
            System.out.println("* AES/ECB");
            System.out.println("    - KEY : " + sKey);
            System.out.println("    - TEXT : " + sText);

            // AES/ECB 암호화
            encrypted = aesEncryptEcb(sKey, sText);

            // AES/ECB 복호화
            decrypted = aesDecryptEcb(sKey, encrypted);

            if (encrypted == null) {
                System.out.println("    - Encrypted : ERROR!!!");
            } else {
                System.out.println("    - Encrypted : " + toHexString(encrypted));
            }

            if (decrypted == null) {
                System.out.println("    - Decrypted : ERROR!!!");
            } else {
                System.out.println("    - Decrypted : " + new String(decrypted, "UTF-8"));
            }

            System.out.println("* AES/CBC");
            System.out.println("    - KEY : " + sKey);
            System.out.println("    - TEXT : " + sText);
            System.out.println("    - IV : (Empty)");

            // AES/CBC 암호화
            encrypted = aesEncryptCbc(sKey, sText);

            // AES/CBC 복호화
            decrypted = aesDecryptCbc(sKey, encrypted);

            if (encrypted == null) {
                System.out.println("    - Encrypted : ERROR!!!");
            } else {
                System.out.println("    - Encrypted : " + toHexString(encrypted));
            }

            if (decrypted == null) {
                System.out.println("    - Decrypted : ERROR!!!");
            } else {
                System.out.println("    - Decrypted : " + new String(decrypted, "UTF-8"));
            }

            System.out.println("* AES/CBC/IV");
            System.out.println("    - KEY : " + sKey);
            System.out.println("    - TEXT : " + sText);
            System.out.println("    - IV : " + sInitVector);

            // AES/CBC/IV 암호화
            encrypted = aesEncryptCbc(sKey, sText, sInitVector);

            // AES/CBC/IV 복호화
            encrypted = aesEncryptCbc(sKey, sText, sInitVector);

            if (encrypted == null) {
                System.out.println("    - Encrypted : ERROR!!!");
            } else {
                System.out.println("    - Encrypted : " + toHexString(encrypted));
            }

            if (decrypted == null) {
                System.out.println("    - Decrypted : ERROR!!!");
            } else {
                System.out.println("    - Decrypted : " + new String(decrypted, "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

결과

* AES/ECB
    - KEY : ABC
    - TEXT : 1234123412341234가나다
    - Encrypted : 28469E0E61DD63B04C14354F091314D4 93A9DBF66B28658AFCB3A6BEC102340A
    - Decrypted : 1234123412341234가나다
* AES/CBC
    - KEY : ABC
    - TEXT : 1234123412341234가나다
    - IV : (Empty)
    - Encrypted : 28469E0E61DD63B04C14354F091314D4 2968C73FF6C986E139BB1A4A2F28E580
    - Decrypted : 1234123412341234가나다
* AES/CBC/IV
    - KEY : ABC
    - TEXT : 1234123412341234가나다
    - IV : 123가나다
    - Encrypted : 989098558DE57BD3F79AAD4D64BDF4EA 4DE0B1A8ED35DEB0094B0B1C9408ABF9
    - Decrypted : 1234123412341234가나다

Powered by EnjoyDev | Xpress Engine | DNS Powered by DNSEver.com