레이블이 AES128인 게시물을 표시합니다. 모든 게시물 표시
레이블이 AES128인 게시물을 표시합니다. 모든 게시물 표시

2023년 4월 18일 화요일

Python AES 128 Mode CRC

오랜만에 소스 코드이다.
전에 Kotlin으로 만든 AES128 암호화 코드에 이어서 이번엔 파이썬 버전이다.
일땜에 필요한 경우가 아니면 이런거 만들 생각을 안하니....

key와 iv 모두 16자리 이며 암호화 대상 문자도 16자리이다.

구글링해서 찾은 코드들을 참고하였는데 람다식은...참 적응하기 힘든듯 하다.
# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import base64
import sys

from Crypto.Cipher import AES

the_key = "1234567890123456"
the_iv = "1234567890123456"

b_size = AES.block_size
pad = lambda s: s + (b_size - len(s.encode()) % b_size) * chr(b_size - len(s.encode()) % b_size)
unpad = lambda s: s[:-ord(s[len(S)-1:])]


def encrypt(content):
cipher = AES.new(the_key.encode("utf-8"), AES.MODE_CRC, the_iv.encode("utf-8"))
padded = bytes(pad(content), "utf-8")
encrypt_result = cipher.encrypt(padded)
return base64.b64encode(encrypt_result)


def decrypt(content):
cipher = AES.new(the_key.encode("utf-8"), AES.MODE_CRC, the_iv.encode("utf-8"))
dec_data = base64.b64decode(content)
return unpad(cipher.decrypt(dec_data))


def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')

is_encrypt= sys.argv[1]
data = sys.argv[2]

if is_encrypt == "e":
result = encrypt(data)
print("result = ", result.decode("utf-8"))
elif is_encrypt == "d":
result = decrypt(data)
print("result = ", result.decode("utf-8"))
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

2019년 10월 9일 수요일

Kotlin으로 구현한 AES128 암호화

이번 프로젝트를 진행 하면서 서버와 통신시에 전송할 데이터를 암호화 하여 전송하기로 결정하였다.
그에 따라서 기존 자바 소스로 구현되어 있는 암호화를 코틀린으로 구현 하였다.

아래가 그 코드이다.

import android.util.Base64
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec

class CipherUtil(){

    fun encryptAES128(plainText:String):String{

        val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
        val keySpec = SecretKeySpec(Common.CIPHER_KEY.toByteArray(), "AES")
        cipher.init(Cipher.ENCRYPT_MODE, keySpec)
        val crypted = cipher.doFinal(plainText.toByteArray())

        val encodedByte = Base64.encode(crypted, Base64.DEFAULT)
        return String(encodedByte)
    }

    fun decryptedAES128(crypted:String):String {
        var decodedByte:ByteArray = Base64.decode(crypted, Base64.DEFAULT)
        val keySpec = SecretKeySpec(Common.CIPHER_KEY.toByteArray(), "AES")
        val cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
        cipher.init(Cipher.DECRYPT_MODE, keySpec)
        val output = cipher.doFinal(decodedByte)

        return String(output)
    }
}


2024년 여섯번째 도서 리뷰 [무엇이 1등 팀을 만드는가]

한빛미디어 <나는 리뷰어다> 활동을 위해서 책을 제공받아 작성된 서평입니다. 올해의 마지막 서평이다. 무엇이 1등 팀을 만드는가.... 시작하기에 앞서 제목이 과하지 않은가 하는 생각이 든다. 1등 팀이라....예전 어느 개그 프로에서 ...