def _exercise_primitive(self, rsaObj): # Since we're using a randomly-generated key, we can't check the test # vector, but we can make sure encryption and decryption are inverse # operations. ciphertext = a2b_hex(self.ciphertext) # Test decryption plaintext = rsaObj.decrypt((ciphertext,)) # Test encryption (2 arguments) (new_ciphertext2,) = rsaObj.encrypt(plaintext, b("")) self.assertEqual(b2a_hex(ciphertext), b2a_hex(new_ciphertext2)) # Test blinded decryption blinding_factor = Random.new().read(len(ciphertext) - 1) blinded_ctext = rsaObj.blind(ciphertext, blinding_factor) blinded_ptext = rsaObj.decrypt((blinded_ctext,)) unblinded_plaintext = rsaObj.unblind(blinded_ptext, blinding_factor) self.assertEqual(b2a_hex(plaintext), b2a_hex(unblinded_plaintext)) # Test signing (2 arguments) signature2 = rsaObj.sign(ciphertext, b("")) self.assertEqual((bytes_to_long(plaintext),), signature2) # Test verification self.assertEqual(1, rsaObj.verify(ciphertext, (bytes_to_long(plaintext),)))
def _check_encryption(self, rsaObj): plaintext = a2b_hex(self.plaintext) ciphertext = a2b_hex(self.ciphertext) # Test encryption (2 arguments) (new_ciphertext2,) = rsaObj.encrypt(plaintext, b("")) self.assertEqual(b2a_hex(ciphertext), b2a_hex(new_ciphertext2))
def _check_decryption(self, paillierObj): plaintext = a2b_hex(self.plaintext) ciphertext = a2b_hex(self.ciphertext) # Test plain decryption new_plaintext = paillierObj.decrypt((ciphertext,)) self.assertEqual(b2a_hex(plaintext), b2a_hex(new_plaintext))
def _exercise_primitive(self, paillierObj): # Since we're using a randomly-generated key, we can't check the test # vector, but we can make sure encryption and decryption are inverse # operations. plaintext = a2b_hex(self.plaintext) # Test encryption ciphertext = paillierObj.encrypt(plaintext, b("")) # Test decryption new_plaintext2 = paillierObj.decrypt((ciphertext)) self.assertEqual(b2a_hex(plaintext), b2a_hex(new_plaintext2))
def _check_decryption(self, rsaObj): plaintext = a2b_hex(self.plaintext) ciphertext = a2b_hex(self.ciphertext) # Test plain decryption new_plaintext = rsaObj.decrypt((ciphertext,)) self.assertEqual(b2a_hex(plaintext), b2a_hex(new_plaintext)) # Test blinded decryption blinding_factor = Random.new().read(len(ciphertext) - 1) blinded_ctext = rsaObj.blind(ciphertext, blinding_factor) blinded_ptext = rsaObj.decrypt((blinded_ctext,)) unblinded_plaintext = rsaObj.unblind(blinded_ptext, blinding_factor) self.assertEqual(b2a_hex(plaintext), b2a_hex(unblinded_plaintext))
def _check_decryption(self, rsaObj): plaintext = a2b_hex(self.plaintext) ciphertext = a2b_hex(self.ciphertext) # Test plain decryption new_plaintext = rsaObj.decrypt((ciphertext, )) self.assertEqual(b2a_hex(plaintext), b2a_hex(new_plaintext)) # Test blinded decryption blinding_factor = Random.new().read(len(ciphertext) - 1) blinded_ctext = rsaObj.blind(ciphertext, blinding_factor) blinded_ptext = rsaObj.decrypt((blinded_ctext, )) unblinded_plaintext = rsaObj.unblind(blinded_ptext, blinding_factor) self.assertEqual(b2a_hex(plaintext), b2a_hex(unblinded_plaintext))
def encrypt(self, password): cryptor = AES.new(self.key_one, self.mode, self.key_two) # 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用 length = 16 count = len(password) add = length - (count % length) text = password + ('\0' * add) ciphertext = cryptor.encrypt(text) # 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题 # 所以这里统一把加密后的字符串转化为16进制字符串 return b2a_hex(ciphertext)
class DSATest(unittest.TestCase): # Test vector from "Appendix 5. Example of the DSA" of # "Digital Signature Standard (DSS)", # U.S. Department of Commerce/National Institute of Standards and Technology # FIPS 186-2 (+Change Notice), 2000 January 27. # http://csrc.nist.gov/publications/fips/fips186-2/fips186-2-change1.pdf y = _sws( b("""19131871 d75b1612 a819f29d 78d1b0d7 346f7aa7 7bb62a85 9bfd6c56 75da9d21 2d3a36ef 1672ef66 0b8c7c25 5cc0ec74 858fba33 f44c0669 9630a76b 030ee333""")) g = _sws( b("""626d0278 39ea0a13 413163a5 5b4cb500 299d5522 956cefcb 3bff10f3 99ce2c2e 71cb9de5 fa24babf 58e5b795 21925c9c c42e9f6f 464b088c c572af53 e6d78802""")) p = _sws( b("""8df2a494 492276aa 3d25759b b06869cb eac0d83a fb8d0cf7 cbb8324f 0d7882e5 d0762fc5 b7210eaf c2e9adac 32ab7aac 49693dfb f83724c2 ec0736ee 31c80291""")) q = _sws(b("""c773218c 737ec8ee 993b4f2d ed30f48e dace915f""")) x = _sws(b("""2070b322 3dba372f de1c0ffc 7b2e3b49 8b260614""")) k = _sws(b("""358dad57 1462710f 50e254cf 1a376b2b deaadfbf""")) k_inverse = _sws(b("""0d516729 8202e49b 4116ac10 4fc3f415 ae52f917""")) m = b2a_hex(b("abc")) m_hash = _sws(b("""a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d""")) r = _sws(b("""8bac1ab6 6410435c b7181f95 b16ab97c 92b341c0""")) s = _sws(b("""41e2345f 1f56df24 58f426d1 55b4ba2d b6dcd8c8""")) def setUp(self): global DSA, Random, bytes_to_long, size from Crypto.PublicKey import DSA from Crypto import Random from Crypto.Util.number import bytes_to_long, inverse, size self.dsa = DSA def test_generate_1arg(self): """DSA (default implementation) generated key (1 argument)""" dsaObj = self.dsa.generate(1024) self._check_private_key(dsaObj) pub = dsaObj.publickey() self._check_public_key(pub) def test_generate_2arg(self): """DSA (default implementation) generated key (2 arguments)""" dsaObj = self.dsa.generate(1024, Random.new().read) self._check_private_key(dsaObj) pub = dsaObj.publickey() self._check_public_key(pub) def test_construct_4tuple(self): """DSA (default implementation) constructed key (4-tuple)""" (y, g, p, q) = [ bytes_to_long(a2b_hex(param)) for param in (self.y, self.g, self.p, self.q) ] dsaObj = self.dsa.construct((y, g, p, q)) self._test_verification(dsaObj) def test_construct_5tuple(self): """DSA (default implementation) constructed key (5-tuple)""" (y, g, p, q, x) = [ bytes_to_long(a2b_hex(param)) for param in (self.y, self.g, self.p, self.q, self.x) ] dsaObj = self.dsa.construct((y, g, p, q, x)) self._test_signing(dsaObj) self._test_verification(dsaObj) def _check_private_key(self, dsaObj): # Check capabilities self.assertEqual(1, dsaObj.has_private()) self.assertEqual(1, dsaObj.can_sign()) self.assertEqual(0, dsaObj.can_encrypt()) self.assertEqual(0, dsaObj.can_blind()) # Check dsaObj.[ygpqx] -> dsaObj.key.[ygpqx] mapping self.assertEqual(dsaObj.y, dsaObj.key.y) self.assertEqual(dsaObj.g, dsaObj.key.g) self.assertEqual(dsaObj.p, dsaObj.key.p) self.assertEqual(dsaObj.q, dsaObj.key.q) self.assertEqual(dsaObj.x, dsaObj.key.x) # Sanity check key data self.assertEqual(1, dsaObj.p > dsaObj.q) # p > q self.assertEqual(160, size(dsaObj.q)) # size(q) == 160 bits self.assertEqual(0, (dsaObj.p - 1) % dsaObj.q) # q is a divisor of p-1 self.assertEqual(dsaObj.y, pow(dsaObj.g, dsaObj.x, dsaObj.p)) # y == g**x mod p self.assertEqual(1, 0 < dsaObj.x < dsaObj.q) # 0 < x < q def _check_public_key(self, dsaObj): k = a2b_hex(self.k) m_hash = a2b_hex(self.m_hash) # Check capabilities self.assertEqual(0, dsaObj.has_private()) self.assertEqual(1, dsaObj.can_sign()) self.assertEqual(0, dsaObj.can_encrypt()) self.assertEqual(0, dsaObj.can_blind()) # Check dsaObj.[ygpq] -> dsaObj.key.[ygpq] mapping self.assertEqual(dsaObj.y, dsaObj.key.y) self.assertEqual(dsaObj.g, dsaObj.key.g) self.assertEqual(dsaObj.p, dsaObj.key.p) self.assertEqual(dsaObj.q, dsaObj.key.q) # Check that private parameters are all missing self.assertEqual(0, hasattr(dsaObj, 'x')) self.assertEqual(0, hasattr(dsaObj.key, 'x')) # Sanity check key data self.assertEqual(1, dsaObj.p > dsaObj.q) # p > q self.assertEqual(160, size(dsaObj.q)) # size(q) == 160 bits self.assertEqual(0, (dsaObj.p - 1) % dsaObj.q) # q is a divisor of p-1 # Public-only key objects should raise an error when .sign() is called self.assertRaises(TypeError, dsaObj.sign, m_hash, k) # Check __eq__ and __ne__ self.assertEqual(dsaObj.publickey() == dsaObj.publickey(), True) # assert_ self.assertEqual(dsaObj.publickey() != dsaObj.publickey(), False) # failIf def _test_signing(self, dsaObj): k = a2b_hex(self.k) m_hash = a2b_hex(self.m_hash) r = bytes_to_long(a2b_hex(self.r)) s = bytes_to_long(a2b_hex(self.s)) (r_out, s_out) = dsaObj.sign(m_hash, k) self.assertEqual((r, s), (r_out, s_out)) def _test_verification(self, dsaObj): m_hash = a2b_hex(self.m_hash) r = bytes_to_long(a2b_hex(self.r)) s = bytes_to_long(a2b_hex(self.s)) self.assertEqual(1, dsaObj.verify(m_hash, (r, s))) self.assertEqual(0, dsaObj.verify(m_hash + b("\0"), (r, s)))
def _encrypt(self, passwd, key_file): self._import_rsa_pri_key(key_file) (cipher,) = self.rsa_obj.encrypt(passwd, b'') return b2a_hex(cipher)