def testBadValueType(self): try: encoder.encode('not an Asn1Item') except PyAsn1Error: pass else: assert 0, 'Invalid value type accepted'
def testEmpty(self): try: encoder.encode(self.s) except PyAsn1Error: pass else: assert False, 'encoded unset choice'
def loadPrivateKey(): with open('keys/id.rsa', 'r') as priv: private64 = ''.join(priv.readlines()[1:-1]) der_serialization = b64decode(private64) privateKey, rest_of_input = der_decoder(der_serialization, asn1Spec=RSAPrivateKeyASN1()) py_private = encode(privateKey) return RSAPrivateKey(*tuple(list(py_private.values())))
def loadPublicKey(): with open('keys/pub.rsa', 'r') as pub: public64 = ''.join(pub.readlines()[1:-1]) der_serialization = b64decode(public64) publicKey, rest_of_input = der_decoder(der_serialization, asn1Spec=RSAPublicKeyASN1()) py_public = encode(publicKey) return RSAPublicKey(*tuple(list(py_public.values())))
def testSimple(self): s = self.s.clone() s[0] = univ.Null('') s[1] = 'abc' s[2] = 123 assert encoder.encode(s) == { 'place-holder': None, 'first-name': str2octs('abc'), 'age': 123 }
def main(argv): if len(argv) != 5: print("Usage: {0} input output iv key".format(sys.argv[0])) exit() input = sys.argv[1] output = sys.argv[2] iv = binascii.unhexlify(sys.argv[3]) key = binascii.unhexlify(sys.argv[4]) img4 = open(input, "rb").read() img4 = pyasn1_decoder.decode(img4, IMG4()) serialized = pyasn1_encode.encode(img4[0]) encrypted_data = serialized["data"] print("{0:15}: {1}".format("Signature", serialized["signature"])) print("{0:15}: {1}".format("Type", serialized["type"])) print("{0:15}: {1}".format("Description", serialized["description"])) print("{0:15}: {1} Bytes".format("Data size", len(encrypted_data))) rows = pyasn1_decoder.decode(serialized['kbag'], Rows()) serialized = pyasn1_encode.encode(rows[0]) print("{0:15}:".format("KBAG values")) for index, row in enumerate(serialized): print("\t#{0}".format(index)) row_key = serialized[row]["key"] row_iv = serialized[row]["iv"] print("\t{0:15}: {1}".format("Key", str(binascii.hexlify(row_key))[2:-1])) print("\t{0:15}: {1}".format("IV", str(binascii.hexlify(row_iv))[2:-1])) print() cipher = AES.new(key, AES.MODE_CBC, iv) decrypted_data = cipher.decrypt(encrypted_data) with open(output, "wb") as img4decrypt: img4decrypt.write(decrypted_data) print("All Done!")
def testOne(self): assert encoder.encode(univ.ObjectIdentifier( (1, 3, 6, 0, 12345))) == '1.3.6.0.12345'
def testValue(self): assert encoder.encode(self.o) == str2octs('Quick brown fox')
def testFalse(self): assert encoder.encode(univ.Boolean(0)) is False
def testNegInt(self): assert encoder.encode(univ.Integer(-12)) == -12
def testSimple(self): assert encoder.encode(self.s) == str2octs('fox')
def testFilled(self): self.s.setComponentByPosition(0, univ.Null('')) assert encoder.encode(self.s) == {'place-holder': None}
def testValue(self): assert encoder.encode(self.o) == 'Quick brown fox'
def testPosInt(self): assert encoder.encode(univ.Integer(12)) == 12
def SHA_1_Scenario(n, e, d, data): # make digest md = hashlib.sha1() md.update(data) md = md.digest() # to asn1 public_key = decode({'n': n, 'e': e}, asn1Spec=RsaKey()) der_public_key = der_encode(public_key) # send public key with open('public_key', 'wb') as key_file: key_file.write(der_public_key) # recieve public key with open('public_key', 'rb') as key_file: public_key = key_file.read() # from asn1 get e,n public_key, _ = der_decode(public_key, asn1Spec=RsaKey()) public_key = encode(public_key) e = public_key['e'] n = public_key['n'] #encrypt hash with RSA hash_as_number = int.from_bytes(md, 'big') rsa_ecnrypted_hash = RSA_encrypt(n, d, hash_as_number) # to asn1 file_with_hash = decode( { 'alg_id': b'0006', 'key_id': b'key_for_sha', 'n': n, 'e': e, 'c': rsa_ecnrypted_hash }, asn1Spec=RsaFile()) der_file_with_hash = der_encode(file_with_hash) # send encrypted 3DES's key with open('file_with_hash', 'wb') as file: file.write(der_file_with_hash) # recieve encrypted 3DES's key with open('file_with_hash', 'rb') as file: encrypted_hash = file.read() # from asn1 get c encrypted_hash, _ = der_decode(encrypted_hash, asn1Spec=RsaFile()) encrypted_hash = encode(encrypted_hash) c = encrypted_hash['c'] # rsa decrypt hash = RSA_decrypt(n, e, c) hash = hash.to_bytes((hash.bit_length() + 7) // 8, 'big') # send file copyfile("data", "accepted_data") input("Continue?\n") # compare hashes path = "accepted_data" file = open(path, "rb") data = file.read() file.close() md = hashlib.sha1() md.update(data) md = md.digest() if (hash == md): print('Hashes matches\n') else: print('File corrupted\n')
def setUp(self): BaseTestCase.setUp(self) self.s = univ.Any(encoder.encode(univ.OctetString('fox')))
def testMinusInf(self): assert encoder.encode(univ.Real('-inf')) == float('-inf')
def testTrue(self): assert encoder.encode(univ.Boolean(1)) is True
def testSimple(self): assert encoder.encode(self.s) == 'fox'
def testValue(self): assert encoder.encode(self.b) == '101010011000101'
def testNull(self): assert encoder.encode(univ.Null('')) is None
def TRI_DES_Scenario(n, e, d, data): # to asn1 public_key = decode({'n': n, 'e': e}, asn1Spec=RsaKey()) der_public_key = der_encode(public_key) # send public key with open('public_key', 'wb') as key_file: key_file.write(der_public_key) # recieve public key with open('public_key', 'rb') as key_file: public_key = key_file.read() # from asn1 get e,n public_key, _ = der_decode(public_key, asn1Spec=RsaKey()) public_key = encode(public_key) e = public_key['e'] n = public_key['n'] # encrypt 3DES's key by RSA key_as_number = int.from_bytes(KEY_FOR_3DES, 'big') rsa_ecnrypted_key = RSA_encrypt(n, e, key_as_number) #print(rsa_ecnrypted_key) # to asn1 open_key = decode( { 'alg_id': b'0001', 'key_id': b'key_for_3des', 'n': n, 'e': e, 'c': rsa_ecnrypted_key }, asn1Spec=RsaFile()) der_file_with_3DES_key = der_encode(open_key) # send encrypted 3DES's key with open('file_with_3des_key', 'wb') as file: file.write(der_file_with_3DES_key) # recieve encrypted 3DES's key with open('file_with_3des_key', 'rb') as file: encrypted_3DES_key = file.read() # from asn1 get c encrypted_3DES_key, _ = der_decode(encrypted_3DES_key, asn1Spec=RsaFile()) encrypted_3DES_key = encode(encrypted_3DES_key) c = encrypted_3DES_key['c'] # rsa decrypt tri_des_key = RSA_decrypt(n, d, c) # encrypt data with 3DES tri_des_key = tri_des_key.to_bytes((tri_des_key.bit_length() + 7) // 8, 'big') tri_des = triple_des(tri_des_key, CBC, b"AWDADADA", pad=None, padmode=PAD_PKCS5) encrypted = tri_des.encrypt(data) #print(encrypted) # send encrypted data with open('ciphertext', 'wb') as ciphertext_file: ciphertext_file.write(encrypted) # recieve encrypted data with open('ciphertext', 'rb') as ciphertext_file: ciphertext = ciphertext_file.read() # decrypt data tri_des = triple_des(KEY_FOR_3DES, CBC, b"AWDADADA", pad=None, padmode=PAD_PKCS5) opentext = tri_des.decrypt(ciphertext) with open('decrypted_data', 'wb') as data: data.write(opentext)
def testChar(self): assert encoder.encode(univ.Real((123, 10, 11))) == 1.23e+13
def setUp(self): self.s = univ.Any(encoder.encode(univ.OctetString('fox')))
def testOne(self): assert encoder.encode(univ.ObjectIdentifier((1, 3, 6, 0, 12345))) == '1.3.6.0.12345'
def testSimple(self): s = self.s.clone() s[0] = univ.Null('') s[1] = 'abc' s[2] = 123 assert encoder.encode(s) == {'place-holder': None, 'first-name': str2octs('abc'), 'age': 123}