def testVerify1(self): for test in self._testData: # Build the key key = RSA.importKey(test[0]) # The real test cipher = PKCS.new(key) pt = cipher.decrypt(t2b(test[2]), "---") self.assertEqual(pt, b(test[1]))
def decrypt(self, ciphertext, keyfile=os.path.expanduser('KEY_FILES/SimulatorNew.pem')): input = open(keyfile) key = RSA.importKey(input.read()) input.close() cipher = PKCS1_v1_5.new(key) plaintext = cipher.decrypt(ciphertext, None) return plaintext
def testEncryptVerify1(self): # Encrypt/Verify messages of length [0..RSAlen-11] # and therefore padding [8..117] for pt_len in range(0, 128 - 11 + 1): pt = self.rng(pt_len) cipher = PKCS.new(self.key1024) ct = cipher.encrypt(pt) pt2 = cipher.decrypt(ct, "---") self.assertEqual(pt, pt2)
def testVerify2(self): # Verify that decryption fails if ciphertext is not as long as # RSA modulus cipher = PKCS.new(self.key1024) self.assertRaises(ValueError, cipher.decrypt, '\x00' * 127, "---") self.assertRaises(ValueError, cipher.decrypt, '\x00' * 129, "---") # Verify that decryption fails if there are less then 8 non-zero padding # bytes pt = b('\x00\x02' + '\xFF' * 7 + '\x00' + '\x45' * 118) pt_int = bytes_to_long(pt) ct_int = self.key1024._encrypt(pt_int) ct = long_to_bytes(ct_int, 128) self.assertEqual("---", cipher.decrypt(ct, "---"))
def testEncrypt1(self): for test in self._testData: # Build the key key = RSA.importKey(test[0]) # 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:self.idx + N] self.idx += N return r # The real test cipher = PKCS.new(key, randfunc=randGen(t2b(test[3]))) ct = cipher.encrypt(b(test[1])) self.assertEqual(ct, t2b(test[2]))
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)
def testEncrypt2(self): # Verify that encryption fail if plaintext is too long pt = '\x00' * (128 - 11 + 1) cipher = PKCS.new(self.key1024) self.assertRaises(ValueError, cipher.encrypt, pt)