45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
import base64
|
|
import secrets
|
|
from base64 import b64encode
|
|
|
|
import Crypto.Cipher.PKCS1_OAEP
|
|
from Crypto.Util.number import getPrime
|
|
from Crypto.PublicKey import ECC, RSA
|
|
|
|
from classes.Crypto.CommutativeCipher import CommutativeCipher
|
|
from classes.MessageTypes.Introduction import IntroductionMessage
|
|
|
|
p = getPrime(1500)
|
|
q = getPrime(1000)
|
|
cipher1 = CommutativeCipher(p, q)
|
|
cipher2 = CommutativeCipher(p, q)
|
|
message = 'Hei på deg'.encode('utf-8 ')
|
|
c1 = cipher1.encode(message)
|
|
c2 = cipher2.encode(c1)
|
|
|
|
d1 = cipher1.decode(c2)
|
|
print(cipher2.decode(d1))
|
|
|
|
key = ECC.generate(curve='p256')
|
|
|
|
test = key.public_key().export_key(format='OpenSSH')
|
|
|
|
seed = secrets.randbits(256).to_bytes(32)
|
|
|
|
key1 = ECC.import_key(test)
|
|
key2 = ECC.import_key(test)
|
|
|
|
print(f'Keys are equal: {key1 == key2}')
|
|
|
|
test = IntroductionMessage('Martin', key.public_key(), seed)
|
|
print(test.generate_and_sign(key))
|
|
print(test.check_signature(key.public_key()))
|
|
|
|
rsa_key = RSA.generate(2048)
|
|
message = 'Keep it secret! Keep it safe!'.encode('utf-8')
|
|
cipher = Crypto.Cipher.PKCS1_OAEP.new(rsa_key.public_key())
|
|
ciphertext = cipher.encrypt(message)
|
|
decoder = Crypto.Cipher.PKCS1_OAEP.new(rsa_key)
|
|
print(decoder.decrypt(ciphertext))
|
|
|
|
print(len(b64encode(rsa_key.public_key().export_key(format='DER')))) |