{"id":9440,"date":"2024-09-26T11:42:57","date_gmt":"2024-09-26T02:42:57","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9440"},"modified":"2024-10-11T17:26:54","modified_gmt":"2024-10-11T08:26:54","slug":"%ec%95%94%ed%98%b8%ed%99%94-seed-aria-aes","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9440","title":{"rendered":"\uc554\ud638\ud654 &#8211; SEED \/ ARIA \/ AES"},"content":{"rendered":"<h1>\uc554\ud638\ud654 &#8211; SEED \/ ARIA \/ AES<\/h1>\n<p>\uba87\uac00\uc9c0 \uc554\ud638\ud654 \uae30\ubc95\uc744 \uc815\ub9ac\ud574 \ubcf4\ub824\ud569\ub2c8\ub2e4.<\/p>\n<p>SEED\/ARIA \ub294 \uad6d\uc0b0 \uc554\ud638\ud654 \uc54c\uace0\ub9ac\uc998\uc774\uace0,<br \/>\nAES \ub294 \ubbf8\uad6d\uc5d0\uc11c \uac1c\ubc1c\ub41c \uc554\ud638\ud654 \uc54c\uace0\ub9ac\uc998\uc774\ub2e4.<\/p>\n<h2>\uae30\ucd08\uc9c0\uc2dd : \ube14\ub85d \uc554\ud638\ud654\uc5d0\uc11c\uc758 \uc6b4\uc601 \ubaa8\ub4dc \ubc0f \ud328\ub529<\/h2>\n<p>\uc554\ud638\ud654 \uae30\ubc95\uc740 \ub370\uc774\ud0c0\ub97c \uc77c\uc815 \ud06c\uae30(8byte \ub610\ub294 16byte) \ub85c \ucabc\uac20 \ud6c4,<br \/>\n\uac01\uac01\uc758 \uc870\uac01\uc744 \uc554\ud638\ud654\ud558\ub294 \ubc29\ubc95\uc5d0 \ub300\ud574\uc11c\ub9cc \uc801\uc6a9\ub41c\ub2e4.<\/p>\n<p>\ub530\ub77c\uc11c, \ud55c \uc870\uac01(8byte \ub610\ub294 16byte)\uc758 \ubcf5\ud638\ud654\ub9cc \uc131\uacf5\ud558\uac8c \ub418\uba74,<br \/>\n\ub098\uba38\uc9c0 \uc804\ubd80\uc758 \ubcf5\ud638\ud654\ub3c4 \uc131\uacf5\ud55c\ub2e4\ub294 \ubb38\uc81c\uac00 \uc0dd\uae34\ub2e4.<\/p>\n<p>\uc5ec\uae30\uc11c \uc6b4\uc601 \ubaa8\ub4dc \ub77c\ub294 \uae30\ubc95\uc774 \uc0dd\uae34\ub2e4.<br \/>\n\uc608\ub97c \ub4e4\uc5b4 CBC(Cipher Block Chaining) Mode \uac19\uc740 \uacbd\uc6b0,<br \/>\n\uac01\uac01\uc758 \ube14\ub85d\uc744 XOR \uc5f0\uc0b0\uc744 \ud574\uc11c 2\uc911\uc73c\ub85c \uc554\ud638\ud654\ub97c \uc9c4\ud589\ud55c\ub2e4.<\/p>\n<p>\ub610 \ud558\ub098\uc758 \ubb38\uc81c\uac00 \ubc1c\uc0dd\ud558\ub294\ub370 \uc77c\uc815 \ud06c\uae30(8byte \ub610\ub294 16byte) \ub85c \ub370\uc774\ud0c0\ub97c \ucabc\uac24 \ub54c,<br \/>\n\ud574\ub2f9 \ubc14\uc774\ud2b8\ub85c \ub098\ub204\uc5b4 \ub5a8\uc5b4\uc9c0\uc9c0 \uc54a\ub294 \ub370\uc774\ud0c0\ub294 \uc5b4\ub5bb\uac8c \uc554\ud638\ud654\ud560 \uac83\uc778\uac00\uc758 \ubb38\uc81c\uc778\ub2e4.<\/p>\n<p>\uc5ec\uae30\uc11c \ubd80\uc871\ud55c \ubd80\ubd84\uc744 \ud2b9\uc815 \ubb38\uc790\ub85c \ucc44\uc6cc\uc11c \uc77c\uc815\ud06c\uae30\ub85c \ub9de\ucdb0\uc8fc\ub294 \uc791\uc5c5\uc744 \ud558\uac8c\ub418\ub294\ub370,<br \/>\n\uc774\uac83\uc744 \ud328\ub529\uc774\ub77c\uace0 \ud55c\ub2e4.<\/p>\n<p>\uc870\uc2ec\ud560 \ubd80\ubd84\uc740 16\ubc14\uc774\ud2b8\uc640 \uac19\uc774 \uc0ac\uc774\uc988\uac00 \ub531 \ub5a8\uc5b4\uc9c0\ub294 \uacbd\uc6b0,<br \/>\n\ud328\ub529\uc744 \ud558\uc9c0 \uc54a\ub294 \uac83\uc774 \uc544\ub2c8\ub77c,<br \/>\n\ucd94\uac00\ub85c 16\ubc14\uc774\ud2b8\ub97c \ud328\ub529\ud55c\ub2e4\ub294 \uac83\uc774\ub2e4.<\/p>\n<h2>ARIA \/ SEED<\/h2>\n<p>\uacf5\uacf5\uae30\uad00 \ub0a9\ud488\uc774 \ud544\uc694\ud55c \uacbd\uc6b0 \uc368\uc57c \ud558\ub294 \uacbd\uc6b0\uac00 \uc0dd\uae34\ub2e4.<\/p>\n<h3>JAVA<\/h3>\n<pre><code class=\"language-groovy\">dependencies {\n    implementation group: &#039;org.bouncycastle&#039;, name: &#039;bcprov-jdk18on&#039;, version: &#039;1.78.1&#039;\n}<\/code><\/pre>\n<p>\uc704 \ub77c\uc774\ube0c\ub7ec\ub9ac\uc5d0\uc11c ARIA\/SEED \ub97c \uc9c0\uc6d0\ud574 \uc8fc\uace0 \uc788\ub2e4.<br \/>\n<code>ARIA\/CBC\/PKCS7Padding<\/code> \ubd80\ubd84\ub9cc \ubcc0\uacbd\ud574 \uc8fc\uba74 \uc554\ud638\ud654\ubc29\uc2dd\/\uc6b4\uc601\ubaa8\ub4dc\/\ud328\ub529\uc744 \ubcc0\uacbd\ud574 \uc904 \uc218 \uc788\ub2e4.<\/p>\n<pre><code class=\"language-java\">public class AriaUtil {\n\n    private static final String privateKey_256 = MyConstants.PRIVATE_KEY;\n\n    public static String ariaCBCEncode(String plainText) throws Exception {\n        byte[] rawKeyBytes = privateKey_256.getBytes(StandardCharsets.UTF_8);\n        byte[] keyBytes = new byte[16];\n        System.arraycopy(rawKeyBytes, 0, keyBytes, 0, Math.min(rawKeyBytes.length, keyBytes.length));\n\n        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, &quot;ARIA&quot;);\n        IvParameterSpec IV = new IvParameterSpec(keyBytes);\n\n        Cipher c = Cipher.getInstance(&quot;ARIA\/CBC\/PKCS7Padding&quot;);\n        c.init(Cipher.ENCRYPT_MODE, secretKey, IV);\n        byte[] encryptionByte = c.doFinal(plainText.getBytes(StandardCharsets.UTF_8));\n\n        return Base64.getEncoder().encodeToString(encryptionByte);\n    }\n\n    public static String ariaCBCDecode(String encodeText) throws Exception {\n        byte[] rawKeyBytes = privateKey_256.getBytes(StandardCharsets.UTF_8);\n        byte[] keyBytes = new byte[16];\n        System.arraycopy(rawKeyBytes, 0, keyBytes, 0, Math.min(rawKeyBytes.length, keyBytes.length));\n\n        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, &quot;ARIA&quot;);\n        IvParameterSpec IV = new IvParameterSpec(keyBytes);\n\n        byte[] decryptionByte = Base64.getDecoder().decode(encodeText);\n        Cipher c = Cipher.getInstance(&quot;ARIA\/CBC\/PKCS7Padding&quot;);\n        c.init(Cipher.DECRYPT_MODE, secretKey, IV);\n\n        return new String(c.doFinal(decryptionByte), StandardCharsets.UTF_8);\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">public class SeedUtil {\n\n    private static final String privateKey_256 = MyConstants.PRIVATE_KEY;\n\n    public static String seedCBCEncode(String plainText) throws Exception {\n        byte[] rawKeyBytes = privateKey_256.getBytes(StandardCharsets.UTF_8);\n        byte[] keyBytes = new byte[16];\n        System.arraycopy(rawKeyBytes, 0, keyBytes, 0, Math.min(rawKeyBytes.length, keyBytes.length));\n\n        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, &quot;SEED&quot;);\n        IvParameterSpec IV = new IvParameterSpec(keyBytes);\n\n        Cipher c = Cipher.getInstance(&quot;SEED\/CBC\/PKCS7Padding&quot;);\n        c.init(Cipher.ENCRYPT_MODE, secretKey, IV);\n        byte[] encryptionByte = c.doFinal(plainText.getBytes(StandardCharsets.UTF_8));\n\n        return Base64.getEncoder().encodeToString(encryptionByte);\n    }\n\n    public static String seedCBCDecode(String encodeText) throws Exception {\n        byte[] rawKeyBytes = privateKey_256.getBytes(StandardCharsets.UTF_8);\n        byte[] keyBytes = new byte[16];\n        System.arraycopy(rawKeyBytes, 0, keyBytes, 0, Math.min(rawKeyBytes.length, keyBytes.length));\n\n        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, &quot;SEED&quot;);\n        IvParameterSpec IV = new IvParameterSpec(keyBytes);\n\n        byte[] decryptionByte = Base64.getDecoder().decode(encodeText);\n        Cipher c = Cipher.getInstance(&quot;SEED\/CBC\/PKCS7Padding&quot;);\n        c.init(Cipher.DECRYPT_MODE, secretKey, IV);\n\n        return new String(c.doFinal(decryptionByte), StandardCharsets.UTF_8);\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">public class Main {\n    public static void main(String[] args) throws Exception {\n        String plainText = &quot;Hello world! \uc548\ub155\ud558\uc138\uc694.&quot;;\n\n        String encData;\n\n        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {\n            Security.addProvider(new BouncyCastleProvider());\n        }\n\n        encData = AriaUtil.ariaCBCEncode(plainText);\n\n        System.out.println(&quot;# AriaUtil \ud3c9\ubb38 \ub370\uc774\ud130 : &quot; + plainText);\n        System.out.println(&quot;# AriaUtil \uc554\ud638\ud654 \ub370\uc774\ud130 : &quot; + encData);\n        System.out.println(&quot;# AriaUtil \ubcf5\ud638\ud654 \ub370\uc774\ud130 : &quot; + AriaUtil.ariaCBCDecode(encData));\n\n        encData = SeedUtil.seedCBCEncode(plainText);\n\n        System.out.println(&quot;# SeedUtil \ud3c9\ubb38 \ub370\uc774\ud130 : &quot; + plainText);\n        System.out.println(&quot;# SeedUtil \uc554\ud638\ud654 \ub370\uc774\ud130 : &quot; + encData);\n        System.out.println(&quot;# SeedUtil \ubcf5\ud638\ud654 \ub370\uc774\ud130 : &quot; + SeedUtil.seedCBCDecode(encData));\n    }\n}<\/code><\/pre>\n<h3>C&#35; &#8211; \ubb38\uc790\uc5f4<\/h3>\n<pre><code class=\"language-bash\">NuGet\\Install-Package BouncyCastle.Cryptography -Version 2.4.0<\/code><\/pre>\n<pre><code class=\"language-csharp\">public class SeedUtil\n{\n    \/\/ SEED128(128 bit == 16 byte)\n    public static string PRIVATE_KEY = &quot;1234567890123456&quot;; \/\/ 16byte\n\n    public static string Encrypt(string plaintxt, string keys)\n    {\n        if (string.IsNullOrEmpty(plaintxt)) return plaintxt;\n\n        var skeyBytes = Encoding.UTF8.GetBytes(keys);\n        var iv = new byte[16];\n        Array.Copy(skeyBytes, iv, Math.Min(iv.Length, skeyBytes.Length));\n\n        IBufferedCipher cipher = CipherUtilities.GetCipher(&quot;SEED\/CBC\/PKCS7Padding&quot;);\n        KeyParameter key = ParameterUtilities.CreateKeyParameter(&quot;SEED&quot;, skeyBytes);\n        cipher.Init(true, new ParametersWithIV(key, iv));\n\n        byte[] encrypted = cipher.DoFinal(Encoding.UTF8.GetBytes(plaintxt));\n        return Convert.ToBase64String(encrypted);\n    }\n\n    public static string Decrypt(string encrypted, string keys)\n    {\n        if (string.IsNullOrEmpty(encrypted)) return encrypted;\n\n        var skeyBytes = Encoding.UTF8.GetBytes(keys);\n        var iv = new byte[16];\n        Array.Copy(skeyBytes, iv, Math.Min(iv.Length, skeyBytes.Length));\n\n        IBufferedCipher cipher = CipherUtilities.GetCipher(&quot;SEED\/CBC\/PKCS7Padding&quot;);\n        KeyParameter key = ParameterUtilities.CreateKeyParameter(&quot;SEED&quot;, skeyBytes);\n        cipher.Init(false, new ParametersWithIV(key, iv));\n\n        byte[] dec = cipher.DoFinal(Convert.FromBase64String(encrypted));\n        return Encoding.UTF8.GetString(dec);\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-csharp\">public class AriaUtil\n{\n    \/\/ ARIA128(128 bit == 16 byte)\n    \/\/ ARIA192(192 bit == 24 byte)\n    \/\/ ARIA256(256 bit == 32 byte)\n    public static string PRIVATE_KEY = &quot;12345678901234567890123456789012&quot;; \/\/ 32byte\n\n    public static string Encrypt(string plaintxt, string keys)\n    {\n        if (string.IsNullOrEmpty(plaintxt)) return plaintxt;\n\n        var skeyBytes = Encoding.UTF8.GetBytes(keys);\n        var iv = new byte[16];\n        Array.Copy(skeyBytes, iv, Math.Min(iv.Length, skeyBytes.Length));\n\n        IBufferedCipher cipher = CipherUtilities.GetCipher(&quot;ARIA\/CBC\/PKCS7Padding&quot;);\n        KeyParameter key = ParameterUtilities.CreateKeyParameter(&quot;ARIA&quot;, skeyBytes);\n        cipher.Init(true, new ParametersWithIV(key, iv));\n\n        byte[] encrypted = cipher.DoFinal(Encoding.UTF8.GetBytes(plaintxt));\n        return Convert.ToBase64String(encrypted);\n    }\n\n    public static string Decrypt(string encrypted, string keys)\n    {\n        if (string.IsNullOrEmpty(encrypted)) return encrypted;\n\n        var skeyBytes = Encoding.UTF8.GetBytes(keys);\n        var iv = new byte[16];\n        Array.Copy(skeyBytes, iv, Math.Min(iv.Length, skeyBytes.Length));\n\n        IBufferedCipher cipher = CipherUtilities.GetCipher(&quot;ARIA\/CBC\/PKCS7Padding&quot;);\n        KeyParameter key = ParameterUtilities.CreateKeyParameter(&quot;ARIA&quot;, skeyBytes);\n        cipher.Init(false, new ParametersWithIV(key, iv));\n\n        byte[] dec = cipher.DoFinal(Convert.FromBase64String(encrypted));\n        return Encoding.UTF8.GetString(dec);\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-csharp\">string encText = AriaUtil.Encrypt(plainText, AriaUtil.PRIVATE_KEY);\nstring decText = AriaUtil.Decrypt(encText, AriaUtil.PRIVATE_KEY);\n\nConsole.WriteLine(&quot;AriaUtil \ud3c9\ubb38 : &quot; + plainText);\nConsole.WriteLine(&quot;AriaUtil \uc554\ud638\ud654 : &quot; + encText);\nConsole.WriteLine(&quot;AriaUtil \ubcf5\ud638\ud654 : &quot; + decText);\n\nencText = SeedUtil.Encrypt(plainText, SeedUtil.PRIVATE_KEY);\ndecText = SeedUtil.Decrypt(encText, SeedUtil.PRIVATE_KEY);\n\nConsole.WriteLine(&quot;SeedUtil \ud3c9\ubb38 : &quot; + plainText);\nConsole.WriteLine(&quot;SeedUtil \uc554\ud638\ud654 : &quot; + encText);\nConsole.WriteLine(&quot;SeedUtil \ubcf5\ud638\ud654 : &quot; + decText);<\/code><\/pre>\n<h3>C&#35; &#8211; \ud30c\uc77c<\/h3>\n<p>\ud30c\uc77c\uc778 \uacbd\uc6b0 \uc0ac\uc774\uc988\uac00 \ud06c\ubbc0\ub85c \uc870\uae08 \ub2e4\ub974\uac8c \uc791\uc131\ud574\uc57c \ud55c\ub2e4.<\/p>\n<pre><code class=\"language-csharp\">public class AriaUtil\n{\n    \/\/ ARIA128(128 bit == 16 byte)\n    \/\/ ARIA192(192 bit == 24 byte)\n    \/\/ ARIA256(256 bit == 32 byte)\n    public static string PRIVATE_KEY = &quot;12345678901234567890123456789012&quot;; \/\/ 32byte\n\n    public static IBufferedCipher GetCipher(bool isEncMode, string keys)\n    {\n        var skeyBytes = Encoding.UTF8.GetBytes(keys);\n        var iv = new byte[16];\n        Array.Copy(skeyBytes, iv, Math.Min(iv.Length, skeyBytes.Length));\n\n        IBufferedCipher cipher = CipherUtilities.GetCipher(&quot;ARIA\/CBC\/PKCS7Padding&quot;);\n        KeyParameter key = ParameterUtilities.CreateKeyParameter(&quot;ARIA&quot;, skeyBytes);\n        cipher.Init(isEncMode, new ParametersWithIV(key, iv));\n\n        return cipher;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-csharp\">string filename_src = &quot;plain.txt&quot;;\nstring filename_enc = &quot;enc.txt&quot;;\nstring filename_dec = &quot;dec.txt&quot;;\n\nIBufferedCipher cipherEnc = AriaUtil.GetCipher(true, AriaUtil.PRIVATE_KEY);\nIBufferedCipher cipherDec = AriaUtil.GetCipher(false, AriaUtil.PRIVATE_KEY);<\/code><\/pre>\n<pre><code class=\"language-csharp\">FileStream file_enc = new FileStream(filename_enc, FileMode.Create);\nusing (FileStream fs = new FileStream(filename_src, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: BUFFERSIZE * 4, useAsync: true))\n{\n    long fileSize = fs.Length;\n    long fileRead = 0;\n    int blockSize = cipherEnc.GetBlockSize();\n    byte[] buffer = new byte[blockSize * 128];  \/\/ 16 * 128 = 2048\n\n    do\n    {\n        int bytesRead = fs.Read(buffer, 0, buffer.Length);\n        if (bytesRead == 0) { break; }\n\n        fileRead = fileRead + bytesRead;\n\n        byte[] outputBytes = new byte[cipherEnc.GetOutputSize(bytesRead)];\n        if (outputBytes.Length &lt;= buffer.Length || fileSize == fileRead)\n        {\n            \/\/ last read\n            int processSize = cipherEnc.ProcessBytes(buffer, 0, bytesRead, outputBytes, 0);\n            file_enc.Write(outputBytes, 0, processSize);\n            int finalSize = cipherEnc.DoFinal(outputBytes, processSize);\n            file_enc.Write(outputBytes, processSize, finalSize);\n            break;\n        }\n        else\n        {\n            int processSize = cipherEnc.ProcessBytes(buffer, 0, bytesRead, outputBytes, 0);\n            file_enc.Write(outputBytes, 0, processSize);\n        }\n    } while (true);\n}\nfile_enc.Flush();\nfile_enc.Close();<\/code><\/pre>\n<pre><code class=\"language-csharp\">FileStream file_dec = new FileStream(filename_dec, FileMode.Create);\nusing (FileStream fs = new FileStream(filename_enc, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: BUFFERSIZE * 4, useAsync: true))\n{\n    long fileSize = fs.Length;\n    long fileRead = 0;\n    int blockSize = cipherDec.GetBlockSize();\n    byte[] buffer = new byte[blockSize * 128];  \/\/ 16 * 128 = 2048\n\n    do\n    {\n        int bytesRead = fs.Read(buffer, 0, buffer.Length);\n        if (bytesRead == 0) { break; }\n\n        fileRead = fileRead + bytesRead;\n\n        byte[] outputBytes = new byte[cipherDec.GetOutputSize(bytesRead)];\n        if (outputBytes.Length &lt; buffer.Length || fileSize == fileRead)\n        {\n            \/\/ last read\n            int processSize = cipherDec.ProcessBytes(buffer, 0, bytesRead, outputBytes, 0);\n            file_dec.Write(outputBytes, 0, processSize);\n            int finalSize = cipherDec.DoFinal(outputBytes, processSize);\n            file_dec.Write(outputBytes, processSize, finalSize);\n            break;\n        }\n        else\n        {\n            int processSize = cipherDec.ProcessBytes(buffer, 0, bytesRead, outputBytes, 0);\n            file_dec.Write(outputBytes, 0, processSize);\n        }\n    } while (true);\n}\nfile_dec.Flush();\nfile_dec.Close();<\/code><\/pre>\n<h2>AES<\/h2>\n<p>\uc804 \uc138\uacc4\uc801\uc73c\ub85c \ub9ce\uc774 \uc4f0\uc774\uba70,<br \/>\nJAVA \/ C# \ub4f1 \uae30\ubcf8 \ub77c\uc774\ube0c\ub7ec\ub9ac\uc5d0 \ud3ec\ud568\ub418\uc5b4 \uc788\ub294 \uacbd\uc6b0\uac00 \ub9ce\ub2e4.<\/p>\n<h3>JAVA &#8211; \ubb38\uc790\uc5f4<\/h3>\n<p>\uad6c \ubc84\uc804\uc758 JDK \uc5d0\uc11c\ub294 AES256 \uc0ac\uc6a9\uc744 \uc704\ud574 \ubcc4\ub3c4 \uc124\uc815\uc774 \ud544\uc694\ud558\ub2e4.<\/p>\n<pre><code class=\"language-java\">public class MyConstants {\n\n    \/\/ AES128(128 bit == 16 byte)\n    \/\/ AES192(192 bit == 24 byte)\n    \/\/ AES256(256 bit == 32 byte)\n    public static final String PRIVATE_KEY = &quot;12345678901234567890123456789012&quot;; \/\/ 32byte\n}<\/code><\/pre>\n<p>AES \uc554\ud638\ud654\ub97c \uc0ac\uc6a9\ud558\ub294 \uac83 \uc774\uc678\uc5d0 \uc6b4\uc601\ubaa8\ub4dc(CBC) \ubc0f \ud328\ub529(PKCS5Padding) \uc744 \uc9c0\uc815\ud574 \uc8fc\uc5b4\uc57c \ud55c\ub2e4.<\/p>\n<p>CBC \uac00 \ubcf4\uc548\uc774 \uac00\uc7a5 \uac15\ud558\ub2e4\uace0 \uc54c\ub824\uc838 \uc788\ub2e4.<br \/>\nJDK \uc5d0\uc11c\ub294 PKCS5Padding \ub77c\uace0 \uba85\uce6d\uc774 \ub418\uc5b4 \uc788\uc9c0\ub9cc \uc2e4\uc81c\ub85c\ub294 PKCS7Padding \ub85c \uc791\ub3d9\ud55c\ub2e4.<\/p>\n<p>Base64 \ub85c \uc778\ucf54\ub529\/\ub514\ucf54\ub529\ud558\ub294 \uac83\uc740 AES \uc758 \ubc94\uc704\uc5d0\uc11c \ubc97\uc5b4\ub09c\ub2e4.<\/p>\n<p>\uc989, AES \uc554\ud638\ud654\ub97c \uc0ac\uc6a9\ud55c\ub2e4\ub294 \uac83\uacfc KEY \ub97c \uc544\ub294 \uac83\uc73c\ub85c \ubd80\uc871\ud558\uace0,<br \/>\n\uc5b4\ub5a4 \ubc29\uc2dd\uc73c\ub85c \uc554\ud638\ud654\ub418\uc5b4 \uc788\ub294\uc9c0\ub97c \uc54c\uc544\uc57c \ubcf5\ud638\ud654\ub97c \uc9c4\ud589\ud560 \uc218 \uc788\ub2e4.<\/p>\n<pre><code class=\"language-java\">public class AesUtil {\n\n    private static final String privateKey_256 = MyConstants.PRIVATE_KEY;\n\n    public static String aesCBCEncode(String plainText) throws Exception {\n        byte[] rawKeyBytes = privateKey_256.getBytes(StandardCharsets.UTF_8);\n        byte[] keyBytes = new byte[16];\n        System.arraycopy(rawKeyBytes, 0, keyBytes, 0, Math.min(rawKeyBytes.length, keyBytes.length));\n\n        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, &quot;AES&quot;);\n        IvParameterSpec IV = new IvParameterSpec(keyBytes);\n\n        \/\/ PKCS5\uac00 PKCS7\ub85c \uc218\ud589\ub418\uc9c0\ub9cc, PKCS5\ub780 \uc774\ub984\uc73c\ub85c JCA(Java Cryptography Architecture)\uc5d0 \uc815\uc758\ub418\uc5b4 \uc788\ub2e4\n        Cipher c = Cipher.getInstance(&quot;AES\/CBC\/PKCS5Padding&quot;);\n\n        c.init(Cipher.ENCRYPT_MODE, secretKey, IV);\n\n        byte[] encryptionByte = c.doFinal(plainText.getBytes(&quot;UTF-8&quot;));\n\n        return Base64.getEncoder().encodeToString(encryptionByte);\n    }\n\n    public static String aesCBCDecode(String encodeText) throws Exception {\n        byte[] rawKeyBytes = privateKey_256.getBytes(StandardCharsets.UTF_8);\n        byte[] keyBytes = new byte[16];\n        System.arraycopy(rawKeyBytes, 0, keyBytes, 0, Math.min(rawKeyBytes.length, keyBytes.length));\n\n        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, &quot;AES&quot;);\n        IvParameterSpec IV = new IvParameterSpec(keyBytes);\n\n        byte[] decryptionByte = Base64.getDecoder().decode(encodeText);\n\n        \/\/ PKCS5\uac00 PKCS7\ub85c \uc218\ud589\ub418\uc9c0\ub9cc, PKCS5\ub780 \uc774\ub984\uc73c\ub85c JCA(Java Cryptography Architecture)\uc5d0 \uc815\uc758\ub418\uc5b4 \uc788\ub2e4\n        Cipher c = Cipher.getInstance(&quot;AES\/CBC\/PKCS5Padding&quot;);\n\n        c.init(Cipher.DECRYPT_MODE, secretKey, IV);\n\n        return new String(c.doFinal(decryptionByte), &quot;UTF-8&quot;);\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">public class Main {\n    public static void main(String[] args) throws Exception {\n        String plainText = &quot;Hello world! \uc548\ub155\ud558\uc138\uc694.&quot;;\n\n        String encData = AesUtil.aesCBCEncode(plainText);\n\n        System.out.println(&quot;# \ud3c9\ubb38 \ub370\uc774\ud130 : &quot; + plainText);\n        System.out.println(&quot;# \uc554\ud638\ud654 \ub370\uc774\ud130 : &quot; + encData);\n        System.out.println(&quot;# \ubcf5\ud638\ud654 \ub370\uc774\ud130 : &quot; + AesUtil.aesCBCDecode(encData));\n    }\n}<\/code><\/pre>\n<h3>JAVA &#8211; \ud30c\uc77c<\/h3>\n<p>Bouncy Castle \ub77c\uc774\ube0c\ub7ec\ub9ac\uc5d0\uc11c AES \uc554\ud638\ud654\ub3c4 \uc9c0\uc6d0\ud558\ubbc0\ub85c,<br \/>\nBouncy Castle \ub77c\uc774\ube0c\ub7ec\ub9ac\ub97c \uc774\uc6a9\ud574 \uc791\uc131\ud569\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-java\">public class AesUtil {\n\n    public static final String PRIVATE_KEY = &quot;12345678901234567890123456789012&quot;; \/\/ 32byte\n\n    public static Cipher GetCipher(int isEncMode) throws Exception\n    {\n        byte[] skeyBytes = privateKey_128.getBytes(StandardCharsets.UTF_8);\n        byte[] iv = new byte[16];\n        System.arraycopy(skeyBytes, 0, iv, 0, Math.min(skeyBytes.length, iv.length));\n\n        Cipher cipher = Cipher.getInstance(&quot;AES\/CBC\/PKCS7Padding&quot;, &quot;BC&quot;);\n        SecretKeySpec secretKey = new SecretKeySpec(skeyBytes, &quot;AES&quot;);\n        IvParameterSpec IV = new IvParameterSpec(iv);\n        cipher.init(isEncMode, secretKey, IV);\n\n        return cipher;\n    }\n\n    public static boolean encrypt(Cipher cipherEnc, String plainFileName, String encFileName) {\n        return doEncryptDecrypt(cipherEnc, plainFileName, encFileName);\n    }\n\n    public static boolean decrypt(Cipher cipherDec, String encFileName, String plainFileName) {\n        return doEncryptDecrypt(cipherDec, encFileName, plainFileName);\n    }\n\n    public static boolean doEncryptDecrypt(Cipher cipher, String srcFileName, String destFileName) {\n        byte[] buffer = new byte[2048];\n        try {\n            FileInputStream srcFile = new FileInputStream(srcFileName);\n            FileOutputStream destFile = new FileOutputStream(destFileName);\n            int bytesRead;\n            byte[] outputBytes;\n            while ((bytesRead = srcFile.read(buffer)) != -1) {\n                outputBytes = cipher.update(buffer, 0, bytesRead);\n                if (outputBytes != null) {\n                    destFile.write(outputBytes);\n                }\n            }\n            outputBytes = cipher.doFinal();\n            if (outputBytes != null) {\n                destFile.write(outputBytes);\n            }\n            destFile.flush();\n            destFile.close();\n            srcFile.close();\n        } catch (SecurityException | IOException | IllegalBlockSizeException | BadPaddingException e) {\n            return false;\n        }\n\n        return true;\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-java\">Security.addProvider(new BouncyCastleProvider());\n\nCipher cipherEnc = AesUtil.GetCipher(Cipher.ENCRYPT_MODE);\nAesUtil.encrypt(cipherEnc, &quot;D:\\\\work\\\\plain.gz&quot;, &quot;D:\\\\work\\\\enc.gz&quot;);\n\nCipher cipherDec = AesUtil.GetCipher(Cipher.DECRYPT_MODE);\nAesUtil.decrypt(cipherDec, &quot;D:\\\\work\\\\enc.gz&quot;, &quot;D:\\\\work\\\\dec.gz&quot;);<\/code><\/pre>\n<h3>C&#35;<\/h3>\n<pre><code class=\"language-csharp\">public class AesUtil\n{\n    public AesUtil()\n    {\n    }\n\n    \/\/ AES128(128 bit == 16 byte)\n    \/\/ AES192(192 bit == 24 byte)\n    \/\/ AES256(256 bit == 32 byte)\n    public static string PRIVATE_KEY = &quot;12345678901234567890123456789012&quot;; \/\/ 32byte\n\n    public static string Encrypt(string plaintxt, string keys)\n    {\n        if (string.IsNullOrEmpty(plaintxt)) return plaintxt;\n\n        var b = Encoding.UTF8.GetBytes(plaintxt);\n        var encrypted = getAes(keys).CreateEncryptor().TransformFinalBlock(b, 0, b.Length);\n        return Convert.ToBase64String(encrypted);\n    }\n\n    public static string Decrypt(string encrypted, string keys)\n    {\n        if (string.IsNullOrEmpty(encrypted)) return encrypted;\n\n        var b = Convert.FromBase64String(encrypted);\n        var decrypted = getAes(keys).CreateDecryptor().TransformFinalBlock(b, 0, b.Length);\n        return Encoding.UTF8.GetString(decrypted);\n    }\n\n    public static Aes getAes(string keys)\n    {\n        var keyBytes = new byte[16];\n        var skeyBytes = Encoding.UTF8.GetBytes(keys);\n        Array.Copy(skeyBytes, keyBytes, Math.Min(keyBytes.Length, skeyBytes.Length));\n\n        Aes aes = Aes.Create();\n        aes.Mode = CipherMode.CBC;\n        aes.Padding = PaddingMode.PKCS7;\n        aes.KeySize = 256;\n        aes.Key = keyBytes;\n        aes.IV = keyBytes;\n\n        return aes;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-csharp\">string plainText = &quot;Hello, World! \uc548\ub155\ud558\uc138\uc694.&quot;;\n\nAesUtil aes = new AesUtil();\n\nstring encText = AesUtil.Encrypt(plainText, AesUtil.PRIVATE_KEY);\nstring decText = AesUtil.Decrypt(encText, AesUtil.PRIVATE_KEY);\n\nConsole.WriteLine(&quot;\ud3c9\ubb38 : &quot; + plainText);\nConsole.WriteLine(&quot;\uc554\ud638\ud654 : &quot; + encText);\nConsole.WriteLine(&quot;\ubcf5\ud638\ud654 : &quot; + decText);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uc554\ud638\ud654 &#8211; SEED \/ ARIA \/ AES \uba87\uac00\uc9c0 \uc554\ud638\ud654 \uae30\ubc95\uc744 \uc815\ub9ac\ud574 \ubcf4\ub824\ud569\ub2c8\ub2e4. SEED\/ARIA \ub294 \uad6d\uc0b0 \uc554\ud638\ud654 \uc54c\uace0\ub9ac\uc998\uc774\uace0, AES \ub294 \ubbf8\uad6d\uc5d0\uc11c \uac1c\ubc1c\ub41c \uc554\ud638\ud654 \uc54c\uace0\ub9ac\uc998\uc774\ub2e4. \uae30\ucd08\uc9c0\uc2dd : \ube14\ub85d \uc554\ud638\ud654\uc5d0\uc11c\uc758 \uc6b4\uc601 \ubaa8\ub4dc \ubc0f \ud328\ub529 \uc554\ud638\ud654 \uae30\ubc95\uc740 \ub370\uc774\ud0c0\ub97c \uc77c\uc815 \ud06c\uae30(8byte \ub610\ub294 16byte) \ub85c \ucabc\uac20 \ud6c4, \uac01\uac01\uc758 \uc870\uac01\uc744 \uc554\ud638\ud654\ud558\ub294 \ubc29\ubc95\uc5d0 \ub300\ud574\uc11c\ub9cc \uc801\uc6a9\ub41c\ub2e4. \ub530\ub77c\uc11c, \ud55c \uc870\uac01(8byte \ub610\ub294 16byte)\uc758 \ubcf5\ud638\ud654\ub9cc \uc131\uacf5\ud558\uac8c \ub418\uba74,\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9440\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-9440","post","type-post","status-publish","format-standard","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9440"}],"version-history":[{"count":20,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9440\/revisions"}],"predecessor-version":[{"id":9485,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9440\/revisions\/9485"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}