Пример #1
0
 def testEncryptDecrypt3(self):
     # Verify that OAEP supports labels
     pt = self.rng(35)
     xlabel = self.rng(22)
     cipher = PKCS.new(self.key1024, label=xlabel)
     ct = cipher.encrypt(pt)
     self.assertEqual(cipher.decrypt(ct), pt)
Пример #2
0
    def decrypt_store_message(self, encrypted_message):
        key = self.private_key
        if not key.startswith("-----BEGIN RSA PRIVATE KEY-----"):
            key = "-----BEGIN RSA PRIVATE KEY-----\n" + key + "\n-----END RSA PRIVATE KEY-----"
        key = RSA.importKey(key)
        entc_list = []
        i = 0
        for x in (key.size_in_bytes(), 16, 16, -1):
            if i == 3:
                entc_list.append(message)
            else:
                entc_list.append(message[:x])
            i += 1
            message = message[x:]
        enc_session_key, nonce, tag, ciphertext = entc_list
        # Decrypt the session key with the private RSA key

        cipher_rsa = PKCS1_OAEP.new(key)
        session_key = cipher_rsa.decrypt(enc_session_key)

        # Decrypt the data with the AES session key
        cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
        encoded_message = cipher_aes.decrypt_and_verify(ciphertext, tag)
        decoded_message = encoded_message.decode("utf-8")

        # add to log and return
        self.log_messages.append(decoded_message)
        return decoded_message
Пример #3
0
 def testEncryptDecrypt1(self):
     # Encrypt/Decrypt messages of length [0..128-2*20-2]
     for pt_len in range(0, 128 - 2 * 20 - 2):
         pt = self.rng(pt_len)
         cipher = PKCS.new(self.key1024)
         ct = cipher.encrypt(pt)
         pt2 = cipher.decrypt(ct)
         self.assertEqual(pt, pt2)
Пример #4
0
 def testDecrypt1(self):
     # Verify decryption using all test vectors
     for test in self._testData:
         # Build the key
         comps = [int(rws(test[0][x]), 16) for x in ('n', 'e', 'd')]
         key = RSA.construct(comps)
         # The real test
         cipher = PKCS.new(key, test[4])
         pt = cipher.decrypt(t2b(test[2]))
         self.assertEqual(pt, t2b(test[1]))
Пример #5
0
    def encrypt_fernet_key(self):
        with open('fernet_key.txt', 'rb') as fk:
            fernet_key = fk.read()
        with open('fernet_key.txt', 'wb') as f:
            self.public_key = RSA.import_key(open('public.pem').read())
            public_crypter = PKCS1_OAEP.new(self.public_key)
            enc_fernent_key = public_crypter.encrypt(fernet_key)
            f.write(enc_fernet_key)

        #with open(f'{self.sysRoot}Desktop/EMAIL_ME.txt', 'wb') as fa:
        #fa.write(enc_fernent_key)

        self.key = enc_fernent_key
        self.crypter = None
Пример #6
0
    def testEncryptDecrypt4(self):
        # Verify that encrypt() uses the custom MGF
        global mgfcalls

        # Helper function to monitor what's requested from MGF
        def newMGF(seed, maskLen):
            global mgfcalls
            mgfcalls += 1
            return bchr(0x00) * maskLen

        mgfcalls = 0
        pt = self.rng(32)
        cipher = PKCS.new(self.key1024, mgfunc=newMGF)
        ct = cipher.encrypt(pt)
        self.assertEqual(mgfcalls, 2)
        self.assertEqual(cipher.decrypt(ct), pt)
Пример #7
0
    def encrypt_message(self, plaintext, public_key):
        if not public_key.startswith("-----BEGIN RSA PUBLIC KEY-----"):
            public_key = "-----BEGIN RSA PUBLIC KEY-----\n" + public_key + "\n-----END RSA PUBLIC KEY-----"
        recipient_key = RSA.importKey(public_key)
        session_key = get_random_bytes(16)

        # Encrypt the session key with the public RSA key
        cipher_rsa = PKCS1_OAEP.new(recipient_key)
        enc_session_key = cipher_rsa.encrypt(session_key)

        # Encrypt the data with the AES session key
        cipher_aes = AES.new(session_key, AES.MODE_EAX)
        ciphertext, tag = cipher_aes.encrypt_and_digest(
            plaintext.encode("UTF-8"))
        encrypted_message = b"".join(
            [x for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext)])
        return encrypted_message
Пример #8
0
    def testEncryptDecrypt2(self):
        # Helper function to monitor what's requested from RNG
        global asked

        def localRng(N):
            global asked
            asked += N
            return self.rng(N)

        # Verify that OAEP is friendly to all hashes
        for hashmod in (MD2, MD5, SHA1, SHA256, RIPEMD160):
            # Verify that encrypt() asks for as many random bytes
            # as the hash output size
            asked = 0
            pt = self.rng(40)
            cipher = PKCS.new(self.key1024, hashmod, randfunc=localRng)
            ct = cipher.encrypt(pt)
            self.assertEqual(cipher.decrypt(ct), pt)
            self.assertEqual(asked, hashmod.digest_size)
Пример #9
0
    def testEncrypt1(self):
        # Verify encryption using all test vectors
        for test in self._testData:
            # Build the key
            comps = [int(rws(test[0][x]), 16) for x in ('n', 'e')]
            key = RSA.construct(comps)

            # RNG that takes its random numbers from a pool given
            # at initialization
            class randGen:
                def __init__(self, data):
                    self.data = data
                    self.idx = 0

                def __call__(self, N):
                    r = self.data[self.idx:N]
                    self.idx += N
                    return r

            # The real test
            cipher = PKCS.new(key, test[4], randfunc=randGen(t2b(test[3])))
            ct = cipher.encrypt(t2b(test[1]))
            self.assertEqual(ct, t2b(test[2]))
Пример #10
0
 def testMemoryview(self):
     pt = b("XER")
     cipher = PKCS.new(self.key1024)
     ct = cipher.encrypt(memoryview(bytearray(pt)))
     pt2 = cipher.decrypt(memoryview(bytearray(ct)))
     self.assertEqual(pt, pt2)
Пример #11
0
 def testDecrypt2(self):
     # Simplest possible negative tests
     for ct_size in (127, 128, 129):
         cipher = PKCS.new(self.key1024)
         self.assertRaises(ValueError, cipher.decrypt, bchr(0x00) * ct_size)
Пример #12
0
 def testEncrypt2(self):
     # Verify that encryption fails if plaintext is too long
     pt = '\x00' * (128 - 2 * 20 - 2 + 1)
     cipher = PKCS.new(self.key1024)
     self.assertRaises(ValueError, cipher.encrypt, pt)
Пример #13
0
import time

from crypto import Random
from crypto.PublicKey import RSA
from crypto.Cipher import PKCS1_OAEP

start_time = time.time()
random_generator = Random.new().read

# Generates public and private key
key = RSA.generate(1024, random_generator)

# Exporting public key to global
public_key = key.publickey()

encryptor = PKCS1_OAEP.new(public_key)
pause1 = time.time()

# Choosing file to read

message_file = input('Input filename for reading: ')

with open(message_file, 'r') as message:
    data1 = message.read()
    data1 = str(data1)
    data1 = bytes(data1, 'utf-8')

# Encryption data from read file
continue1 = time.time()
encrypted = encryptor.encrypt(data1)
pause2 = time.time()