파이썬을 이용한 암호화 스터디이다.
from Crypto.Cipher import ARC4
plaintext=input("평문 ㄱ")
key= input("키: ")
#cipher= ARC4.new(key.encode())
#ciphertext=cipher.encrypt(plaintext.encode())
#print(ciphertext.hex())
output="b853337b27ade723d2533ea280d1025cf71d6cea"
key2="2020CERT"
cipher2=ARC4.new(key2.encode())
ciphertext2=cipher2.decrypt(bytes.fromhex(output))
print(ciphertext2.decode("utf-8"))
from Crypto.Cipher import DES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
target="Hello world!"
key=get_random_bytes(8)
cipher=DES.new(key,DES.MODE_ECB)
plaintext=pad(target.encode(),8)
ciphertext=cipher.encrypt(plaintext)
print("encrypt: "+ ciphertext.hex())
decrypted=cipher.decrypt(ciphertext)
print("decrypt: "+unpad(decrypted,8).decode())
문제
#
# Made by Tekiter
#
import json
from base64 import b64encode, b64decode
from Crypto.Cipher import DES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
des = DES.new(get_random_bytes(8), DES.MODE_ECB)
token = {
"bth": 12,
"admin": 0,
"home": "Seoul",
"complain": 1,
"visited": 0
}
enc = des.encrypt(pad(json.dumps(token).encode(), 8))
print("Here is your login token: " + b64encode(enc).decode())
reply = input("Input your token: ")
try:
rawdata = unpad(des.decrypt(b64decode(reply)), 8).decode()
tokr = json.loads(rawdata)
if tokr['admin'] != 0:
print("You are admin!")
else:
print("You are just a user!")
except:
print("Token parse error")
푸는법은 나온토큰을 mytoken에 넣으면된다. ECB모드의 붙여넣기 취약점
import json
from base64 import b64encode, b64decode
from Crypto.Cipher import DES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
mytoken="oc0tO3uCLRt1HRqbq1l3qR2yHt62dTUmd9JEWl33TE172i8A85QW0QhNQPC2ezN3TRVCkITVMN91hETtcJgjU9yBBOAIuBJ5"
mytoken2=b64decode(mytoken)
print( b64encode((mytoken2[0:16]+mytoken2[48:56]+mytoken2[24:])).decode())
CBC모드의 암호화 복호화
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
data="Hello CBC!"
key=get_random_bytes(16)
cipher=AES.new(key,AES.MODE_CBC)
enc=cipher.encrypt(pad(data.encode(),AES.block_size))
print("key:"+key.hex())
print("IV:"+cipher.iv.hex())
print("cipher:"+enc.hex())
cipher2=bytes.fromhex("6ac0f5ba234deb1c16bd8c2b30743831973f4490d452b50660cf4877e7f02d8d")
IV2=bytes.fromhex("2730ee53466bab0d99288a417c1da969")
key2=bytes.fromhex("789f1ded5a29290c655c9b046792b7ee")
cipherr=AES.new(key2,AES.MODE_CBC, iv=IV2)
plain=unpad(cipherr.decrypt(cipher2),AES.block_size)
print(plain.decode())
공개키 비밀키
from Crypto.PublicKey import RSA
key=RSA.generate(2048)
private_key=key.export_key()
file_out=open("private.pem","wb")
file_out.write(private_key)
public_key=key.publickey().export_key()
file_output=open("public.pem","wb")
file_output.write(public_key)
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
data="Hello, RSA!"
public_key=RSA.import_key(open('public.pem').read())
cipher=PKCS1_OAEP.new(public_key)
enc=cipher.encrypt(data.encode())
private_key=RSA.import_key(open('private.pem').read())
cipher=PKCS1_OAEP.new(private_key)
dec=cipher.decrypt(enc)
print(dec.decode())
'꿀팁!' 카테고리의 다른 글
스터디5 (0) | 2020.01.20 |
---|---|
스터디4 (0) | 2020.01.17 |
스터디1 (0) | 2020.01.03 |
long long형 변수가 printf에서 이상한 값(0 등등)으로 나오는 현상 (0) | 2019.08.16 |
python binary data처리 (0) | 2019.05.10 |