Manual/OpenSSL

[OpenSSL] enc ( ์•”ํ˜ธํ™” ) ๋ช…๋ น์–ด

JayKim๐Ÿ™‚ 2023. 5. 4. 10:48

OpenSSL ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์•”/๋ณตํ˜ธํ™”๋ฅผ ํ•ด๋ณด์ž
ํ•ด๋‹น ๋ช…๋ น์–ด์— ๋Œ€ํ•œ ๋ฉ”๋‰ด์–ผ์„ ์ฐธ์กฐ ํ•˜์˜€๋‹ค.

๋จผ์ž ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ธ”๋Ÿญ ์•”ํ˜ธ ๋ชฉ๋ก์„ ํ™•์ธ ํ•ด๋ณด์ž

openssl enc -ciphers

[RANIX@DESKTOP-VOGBKQM ~]$ openssl enc -ciphers
Supported ciphers:
-aes-128-cbc               -aes-128-cfb               -aes-128-cfb1
-aes-128-cfb8              -aes-128-ctr               -aes-128-ecb
-aes-128-ofb               -aes-192-cbc               -aes-192-cfb
-aes-192-cfb1              -aes-192-cfb8              -aes-192-ctr
-aes-192-ecb               -aes-192-ofb               -aes-256-cbc
-aes-256-cfb               -aes-256-cfb1              -aes-256-cfb8
-aes-256-ctr               -aes-256-ecb               -aes-256-ofb
-aes128                    -aes128-wrap               -aes192
-aes192-wrap               -aes256                    -aes256-wrap
-aria-128-cbc              -aria-128-cfb              -aria-128-cfb1
-aria-128-cfb8             -aria-128-ctr              -aria-128-ecb
-aria-128-ofb              -aria-192-cbc              -aria-192-cfb
-aria-192-cfb1             -aria-192-cfb8             -aria-192-ctr
-aria-192-ecb              -aria-192-ofb              -aria-256-cbc

OpenSSL ๋ช…๋ น์–ด๋ฅผ ์“ฐ๋‹ค ๋ณด๋ฉด Base64 ์ธ์ฝ”๋”ฉ/๋””์ฝ”๋”ฉ์ด ํ•„์š” ํ• ๋•Œ ๊ฐ€ ๋งŽ์•„์„œ ๊ฐ„๋‹จํžˆ Base64 ์ธ์ฝ”๋”ฉ/๋””์ฝ”๋”ฉ ๋ช…๋ น์–ด ์‚ฌ์šฉ๋ฒ•์„ ๊ธฐ๋กํ–ˆ๋‹ค. Base64๋Š” ์•”ํ˜ธํ™”๊ธฐ๋Šฅ์€ ์•„๋‹ˆ๊ณ  ์ธ์ฝ”๋”ฉ/๋””์ฝ”๋”ฉ์„ ์œ„ํ•œ ๊ฒƒ์ด๋‹ค.

Base64 ์ธ์ฝ”๋”ฉ

openssl base64 -in file.bin -out file.b64

Base64 ๋””์ฝ”๋”ฉ

openssl base64 -d -in file.b64 -out file.bin

์ด์ œ ๋ฐ์ดํƒ€ ์•”ํ˜ธํ™” ๋ณตํ˜ธํ™”๋ฅผ ํ•ด๋ณด์ž ์šฐ์„  ํ•˜๋Š” ๋ฐฉ์‹์€ KDF ๋ฅผ ์ด์šฉํ•ด ํ‚ค๋ฅผ ์ƒ์„ฑ ํ›„ ์•”ํ˜ธํ™”/๋ณตํ˜ธํ™” ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
์ด ๋ฐฉ์‹์—์„œ๋Š” ํŒจ์Šค์›Œ๋“œ๋ฅผ ๋ฐ›์œผ๋ฉด ๋‚ด๋ถ€์ ์œผ๋กœ Iter ๊ฐ’๊ณผ Salt ๊ฐ’์„ ์‚ฌ์šฉ ํ•ด ํ‚ค๋ฅผ ์ƒ์„ฑ ํ•˜๊ณ  ์ƒ์„ฑ๋œ ํ‚ค๋กœ ์•”ํ˜ธํ™” ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
๋ฌผ๋ก  iter ๊ฐ’๊ณผ Salt ๊ฐ’์€ ์ง€์ •๋„ ๊ฐ€๋Šฅ ํ•˜์ง€๋ฉด ์—ฌ๊ธฐ์„œ๋Š” ๋‚ด๋ถ€ ์ง€์ •๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

PBKDF2 ์ด์šฉ AES-128 ์•”ํ˜ธํ™”

openssl enc -aes128 -pbkdf2 -in file.txt -out file.aes128

์ด๋•Œ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค. ์ด ํŒจ์Šค์›Œ๋“œ๋Š” ๋ณตํ˜ธํ™” ํ• ๋•Œ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ -pass ์˜ต์…˜์—์„œ ๊ฐ’์„ ๋„ฃ์–ด ์ฃผ๋ฉด ๋œ๋‹ค.
์—ฌ๊ธฐ์„œ pbkdf2 ๋ฐฉ์‹์œผ๋กœ ํŒจ์Šค์›Œ๋“œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์•”ํ˜ธํ™” ํ‚ค๋ฅผ ๋งŒ๋“ค์–ด์„œ ์•”ํ˜ธํ™”๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค.

PBKDF2 ์ด์šฉ ๋ณตํ˜ธํ™”

openssl enc -aes128 -pbkdf2 -d -in file.aes128 -out file.txt -pass pass:<password>

์šด์˜ ๋ชจ๋“œ ์ง€์ • ์•”๋ณตํ˜ธํ™”

์•”ํ˜ธํ™”

openssl enc -e -aes-128-cbc -in test.txt -out test.enc

์—ฌ๊ธฐ์„œ aes-128-cbc ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
์ด๋•Œ ์•”ํ˜ธํ™” ํ•˜๋ฉด ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋‚ด๋ถ€์ ์œผ๋กœ kdf ๋ฅผ ์ด์šฉํ•ด ์•”ํ˜ธํ™” ํ•œ๋‹ค.

๋ณตํ˜ธํ™”

openssl enc -d -aes-128-cbc -in test.enc -out test.dec

์•”ํ˜ธํ™” ์ž…๋ ฅ์‹œ ์‚ฌ์šฉํ•œ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋ณตํ˜ธํ™” ํ•œ๋‹ค.

์‹ค์ œ ํ‚ค์™€ IV ์ง€์ • ์•”๋ณตํ˜ธํ™”๋ฅผ ํ•ด๋ณด์ž
์•”ํ˜ธํ™”

openssl enc -e -aes-128-cbc -in test.txt -out test.e1 -K 83A0423EB66693020B7A78AA0F08DE6C -iv EBA02B3EF93F14FDEB64E09A815DE8E8

๋ณตํ˜ธํ™”

openssl enc -d -aes-128-cbc -in test.e1 -out test.d1 -K 83A0423EB66693020B7A78AA0F08DE6C -iv EBA02B3EF93F14FDEB64E09A815DE8E8

์•”/๋ณตํ˜ธํ™”์˜ -K ์˜ ํ‚ค๊ฐ’๊ณผ -iv ์˜ Initial Vector ๊ฐ’์œผ๋กœ Hex ์ธ์ฝ”๋”ฉ ๋œ ๊ฐ’์„ ๋„ฃ์–ด ์ฃผ๋ฉด ๋œ๋‹ค.
์ฐธ๊ณ ๋กœ ์‹ค์ œ๋กœ -K ์˜ต์…˜์„ ์ง์ ‘ ์ž…๋ ฅํ•˜๋ฉด KDF ๋ฐฉ์‹์ด ํ•„์š” ์—†์–ด์„œ Salt ๊ฐ’์€ ํ•„์š”๊ฐ€ ์—†๋‹ค.

ํ˜„์žฌ OpenSSL ๋ช…๋ น์–ด์—์„œ๋Š” GCM ๋ชจ๋“œ์™€ CCM ๋ชจ๋“œ๋Š” ์ง€์› ํ•˜์ง€ ์•Š๋Š”๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์•ž์œผ๋กœ๋„ ์ง€์›์€ ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•˜๋‹ˆ OpenSSL ๋ช…๋ น์–ด๋กœ๋Š” ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ๋Š” ์–ด๋ ต๊ฒ ๋„ค์š”.

๊ทธ๋ž˜๋„ GCM ๋ช…๋ น์–ด์™€ CCM ์— ๋Œ€ํ•œ ๋ฐ์ดํƒ€๋Š” BerEditor์—์„œ ์•”/๋ณตํ˜ธํ™” ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.