def __init__(self): self.rsa = RSAClass() #generate the Public Private key pair for this client self.public_key, self.private_key = self.rsa.generate_keys() # read in the Public key of the Server from XML File self.pubKey = chilkat.CkPublicKey() self.pubKey.LoadXmlFile("Serverpublickey.xml") self.ServerPublicKey = self.pubKey.getXml() #objects for hashing and diffie hellman self.md5_crypt = chilkat.CkCrypt2() self.hashcrypt = chilkat.CkCrypt2() self.dhAlice = chilkat.CkDh() self.md5_crypt.put_EncodingMode("hex") # Set the hash algorithm: self.md5_crypt.put_HashAlgorithm("md5") self.hashcrypt.put_EncodingMode("hex") self.hashcrypt.put_HashAlgorithm("md5") # Unlock components above self.UnlockComponents() #create a oscket and connect to the server self.client = socket(AF_INET, SOCK_STREAM) self.client.connect(ADDR) # setup an AES object with cipher block chaining, 128-bit key, padding size and format self.a = AESClass("cbc", 128, 0, "hex") # source port of Client for use in communications later self.client_src_port = self.client.getsockname()[1]
def __compute_dsa_sig(self, sig_str): encode_mode = 'base64' crypt = chilkat.CkCrypt2() success = crypt.UnlockComponent("Anything for 30-day trial.") if (success != True): print(crypt.lastErrorText()) sys.exit() crypt.put_EncodingMode(encode_mode) crypt.put_HashAlgorithm("sha-1") hash_str = crypt.hashStringENC(sig_str) success = self.dsa.SetEncodedHash(encode_mode, hash_str) if success != True: print(self.dsa.lastErrorText()) sys.exit() # Now that the DSA object contains both the private key and hash, # it is ready to create the signature: success = self.dsa.SignHash() if success != True: print(self.dsa.lastErrorText()) sys.exit() # If SignHash is successful, the DSA object contains the # signature. It may be accessed as a hex or base64 encoded # string. (It is also possible to access directly in byte array form via # the "Signature" property.) # hex_sig = base64.b64encode(self.dsa.getEncodedSignature("hex").encode('utf-8')) hex_sig = self.dsa.getEncodedSignature(encode_mode) # print("Signature:") # print(hex_sig) return hex_sig
def __dsa_verify(self, hash_str, dsa, sigma): # dsa = self.dsa crypt = chilkat.CkCrypt2() success = crypt.UnlockComponent("Anything for 30-day trial.") if not success: print crypt.lastErrorText() return False # crypt.put_EncodingMode("hex") crypt.put_EncodingMode("base64") crypt.put_HashAlgorithm("sha-1") hash_str = crypt.hashStringENC(hash_str) # hash_str = crypt.hashStringENC(hash_str) # Load the hash to be verified against the signature. success = dsa.SetEncodedHash("base64", hash_str) if (success != True): print(dsa.lastErrorText()) sys.exit() # Load the signature: success = dsa.SetEncodedSignature("base64", sigma) if (success != True): print(dsa.lastErrorText()) sys.exit() # Verify: success = dsa.Verify() if (success != True): print "abort for verify fail" return False
def DecFile(file, key_file): # Read file with encrypted 3DES key # index key_file = open(key_file, 'rb') r = key_file.read() f = rsa_enc_file.decode('RSAEncodedFile', r) key_des = f['keyset']['key']['ciphertext']['c'] key_file.close() des1 = str(hex(key_des)) des = des1[des1.rindex('x') + 1:len(des1)] des1 = int(des[0:32], 16) des2 = int(des[32:64], 16) tdes1 = pow(des1, secret, module) tdes2 = pow(des2, secret, module) tdes1 = str(hex(tdes1)) tdes1 = tdes1[tdes1.rindex('x') + 1:len(tdes1)] tdes2 = str(hex(tdes2)) tdes2 = tdes2[tdes2.rindex('x') + 1:len(tdes2)] while (len(tdes1) < 24): tdes1 = "0" + tdes1 while (len(tdes2) < 24): tdes2 = "0" + tdes2 fulldes = tdes1 + tdes2 crypt = chilkat.CkCrypt2() success = crypt.UnlockComponent("Anything for 30-day trial") if success != True: print(crypt.lastErrorText()) return crypt.put_CryptAlgorithm("3des") # выбор алгоритма crypt.put_CipherMode("ecb") # выбор режима простой замены crypt.put_KeyLength(192) # длина ключа crypt.put_PaddingScheme(0) crypt.put_EncodingMode("hex") # вывод в hex crypt.SetEncodedKey(fulldes, "hex") # ключ шифрования index = file.rindex('.') # индекс последней точки file_extension = file[index:len(file)] # чтения расширения out_file = file[ 0:index] + "_dec" + file_extension # создание файла для шифра success = crypt.CkDecryptFile(file, out_file) # шифрование файла 3des if success != True: print(crypt.lastErrorText()) return print("File successfully decrypted to " + out_file) return out_file
def encrypt(s, p): crypt = chilkat.CkCrypt2() p.replace(".txt", "decrypt.txt") success = crypt.UnlockComponent("Anything for 30-day trial") if (success != True): print(crypt.lastErrorText()) sys.exit() # AES is also known as Rijndael. crypt.put_CryptAlgorithm("aes") # CipherMode may be "ecb", "cbc", "ofb", "cfb", "gcm", etc. # Note: Check the online reference documentation to see the Chilkat versions # when certain cipher modes were introduced. crypt.put_CipherMode("cbc") # KeyLength may be 128, 192, 256 crypt.put_KeyLength(256) # The padding scheme determines the contents of the bytes # that are added to pad the result to a multiple of the # encryption algorithm's block size. AES has a block # size of 16 bytes, so encrypted output is always # a multiple of 16. crypt.put_PaddingScheme(0) # EncodingMode specifies the encoding of the output for # encryption, and the input for decryption. # It may be "hex", "url", "base64", or "quoted-printable". crypt.put_EncodingMode("hex") # An initialization vector is required if using CBC mode. # ECB mode does not use an IV. # The length of the IV is equal to the algorithm's block size. # It is NOT equal to the length of the key. ivHex = "000102030405060708090A0B0C0D0E0F" crypt.SetEncodedIV(ivHex, "hex") # The secret key must equal the size of the key. For # 256-bit encryption, the binary secret key is 32 bytes. # For 128-bit encryption, the binary secret key is 16 bytes. keyHex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F" crypt.SetEncodedKey(keyHex, "hex") # Encrypt a string... # The input string is 44 ANSI characters (i.e. 44 bytes), so # the output should be 48 bytes (a multiple of 16). # Because the output is a hex string, it should # be 96 characters long (2 chars per byte). encStr = crypt.encryptStringENC(s) fil1 = open(p, "w+") fil1.write(encStr) fil1.close()
def HAVAL_saltless(self, i, data): crypt = chilkat.CkCrypt2() crypt.put_HashAlgorithm("haval") crypt.put_HavalRounds(5) crypt.put_KeyLength(256) start_time = time.time() crypt.hashStringENC(data) end_time = time.time() print("HAVAL:", end_time - start_time, "s") self.fast_time_saltless[i].append(end_time - start_time)
def __init__(self, address=('localhost', 0)): asyncore.dispatcher.__init__(self) #import the Servers Keys, Saved to XML in FileSystem privkey = chilkat.CkPrivateKey() privkey.LoadXmlFile("Serverprivatekey.xml") self.ServerPrivateKey = privkey.getXml() #create RSA object self.rsa = RSAClass() #Chilkat object forCreating hashes self.hashcrypt = chilkat.CkCrypt2() success = self.hashcrypt.UnlockComponent("T12302015Crypt_sHyDCAFgIR1v") if (success != True): print(hself.ashcrypt.lastErrorText()) sys.exit() # setting encoding mode for hashing algorithm self.hashcrypt.put_EncodingMode("hex") self.hashcrypt.put_HashAlgorithm("md5") # setup data for Diffie Hellman Key exchange self.dhBob = chilkat.CkDh() success = self.dhBob.UnlockComponent("T12302015Diffie_eegQ20BTIR5q") if (success != True): print(self.dhBob.lastErrorText()) sys.exit() self.dhBob.UseKnownPrime(2) self.p = self.dhBob.p() self.g = self.dhBob.get_G() self.eBob = self.dhBob.createE(256) #initially set the the AES object with cipher block chaining 128bit self.aesObj = AESClass("cbc",128,0,"hex") self.inital_setup = "0" #store client IDS and nonces # nonces used in replay protection self.CLIENT_ID_STORE ={} self.CLIENT_ID = [] #placeholders fo the keys (session and shared the same really) self.sharedKey = None self.sessionkey = None # self.set_reuse_addr() #bind to socket and listen self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.bind(address) #print ("Address Server", address) # accept with no backlog # syncore dispatcher handles it all self.listen(1) #store connected clients in LIST self.remote_clients = []
def hash_dict(dict): """ :param dict: utf-8 charset dictionary to hash :return: sha256 hashed and base64 encoded json serialization of dict """ crypt = chilkat.CkCrypt2() crypt.put_HashAlgorithm("SHA256") crypt.put_Charset("utf-8") crypt.put_EncodingMode("base64") json_serialized_dict = json.dumps(dict, sort_keys=True) return crypt.hashStringENC(json_serialized_dict)
def decrypt_bf(keyHex, data): crypt = chilkat.CkCrypt2() crypt.put_CryptAlgorithm("blowfish2") crypt.put_CipherMode("cbc") crypt.put_KeyLength(8) crypt.put_PaddingScheme(0) crypt.put_EncodingMode("hex") ivHex = "0001020304050607" crypt.SetEncodedIV(ivHex, "hex") crypt.SetEncodedKey(keyHex, "hex") decStr = crypt.decryptStringENC((data)) return decStr
def encrypt_AES(keyHex, data): crypt = chilkat.CkCrypt2() crypt.put_CryptAlgorithm("aes") crypt.put_CipherMode("cbc") crypt.put_KeyLength(8) crypt.put_PaddingScheme(0) crypt.put_EncodingMode("hex") ivHex = "0001020304050607" crypt.SetEncodedIV(ivHex, "hex") crypt.SetEncodedKey(keyHex, "hex") encStr = crypt.encryptStringENC(data) return encStr
def __init__(self, mode, keylen, padding, enc_mode): self.mode = mode if keylen != 128 or 192 or 265: self.keylen = 256 self.padding = padding self.enc_mode = enc_mode self.setup = False self.ivHex = None self.keyHex = None self.keylen = keylen self.session_flag = False self.crypt = chilkat.CkCrypt2() self.zip = chilkat.CkZip() self.UnlockComponents()
def get_crypt(base): crypt = chilkat.CkCrypt2() success = crypt.UnlockComponent("Unlocked") if not success: print(crypt.lastErrorText()) sys.exit() crypt.put_CryptAlgorithm("aes") crypt.put_CipherMode("cbc") crypt.put_KeyLength(256) crypt.put_PaddingScheme(3) crypt.put_EncodingMode("hex") ivHex = "000102030405060708090A0B0C0D0E0F" crypt.SetEncodedIV(ivHex, "salt") keyHex = "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F" crypt.SetEncodedKey(keyHex, base) return crypt
def __generate_HMAC(self, id_m): crypt = chilkat.CkCrypt2() uid = self.get_uid_to_num() key_diversed = crypt.pbkdf2(self.__mac_sk, "ansi", "sha256", str(uid), 2048, 256, "hex") crypt.put_EncodingMode("hex") crypt.put_HashAlgorithm("sha256") crypt.SetHmacKeyEncoded(key_diversed, "ascii") hmac_key = crypt.hmacStringENC(str(id_m) + str(uid)) return hmac_key
def sign_string(priv_key, string): """ :param priv_key: secp256k1 elliptic curve private key in Pkcs1 DER format in base64 encode :param string: string :return: secp256k1 signed sha256 hashed and base64 encoded json serialization of dict """ crypt = chilkat.CkCrypt2() crypt.put_HashAlgorithm("SHA256") crypt.put_Charset("utf-8") crypt.put_EncodingMode("base64") sha256_hash = crypt.hashStringENC(string) chilkat_ecdsa = chilkat.CkEcc() prng = chilkat.CkPrng() chilkat_byte_data = chilkat.CkByteData() chilkat_byte_data.appendEncoded(priv_key, 'base64') chilkat_private_key = chilkat.CkPrivateKey() chilkat_private_key.LoadPkcs1(chilkat_byte_data) return chilkat_ecdsa.signHashENC(sha256_hash,"base64",chilkat_private_key,prng)
Applications=["systemAdminMIS","humanResourceMIS","outPatientMIS","frontDeskMIS","pharmacyMIS", "laboratoryMIS","inPatientMIS","radiologyMIS","padeatricsMIS","surgeryMIS", "triageMIS","emergencyMIS","obstetricsAndGynacologyMIS","nurseMIS","storeMIS", "hospitalDirectorMIS"] Serials=['000','001','002','003','004','005','006','007','008','009', '010','011','012','013','014','015','016','017','018','019', '020','021','022','023','024','025','026','027','028','029', '030','031','032','033','034','035','036','037','038','039', '040','041','042','043','044','045','046','047','048','049', '050','051','052','053','054','055','056','057','058','059', '060','061','062','063','064','065','066','067','068','069', '070','071','072','073','074','075','076','077','078','079', '080','081','082','083','084','085','086','087','088','089', '090','091','092','093','094','095','096','097','098','099'] NurseDepartments=["D000","D001","D002","D003","D004","D006","D007","D008","D009"] Crypto = chilkat.CkCrypt2() success = Crypto.UnlockComponent("MPS-Crypto") if (success != True): sys.exit() Crypto.put_CryptAlgorithm("aes") Crypto.put_CipherMode("cbc") Crypto.put_KeyLength(256) Crypto.put_PaddingScheme(0) Crypto.put_EncodingMode("hex") ivHex = "000102030405060708090A0B0C0D0E0F" Crypto.SetEncodedIV(ivHex,"hex") CryptoKeyHex = "000102030405060708090A0B0C0DDD0E0F101112131415161718191A1B1C1D1E1F" Crypto.SetEncodedKey(CryptoKeyHex,"hex") class AssignNurseToOneMoreDepartment(object): def __init__(self, **kwags): super(AssignNurseToOneMoreDepartment, self).__init__()
def EncFile(file): crypt = chilkat.CkCrypt2() success = crypt.UnlockComponent("Anything for 30-day trial") if success != True: print(crypt.lastErrorText()) return crypt.put_CryptAlgorithm("3des") # выбор алгоритма crypt.put_CipherMode("ecb") # выбор режима простой замены crypt.put_KeyLength(192) # длина ключа crypt.put_PaddingScheme(0) crypt.put_EncodingMode("hex") # вывод в hex crypt.SetEncodedKey(deskey, "hex") # ключ шифрования index = file.rindex('.') # индекс последней точки fileExtension = file[index:len(file)] # чтения расширения # Encrypting file in_file = file # вход. файл out_file = file[ 0:index] + "_enc" + fileExtension # создание файла для шифра success = crypt.CkEncryptFile(in_file, out_file) # шифрование файла 3des if not success: print(crypt.lastErrorText()) return # Encrypting 3DES key with RSA encrypted_des_key1 = int(deskey[0:24], 16) encrypted_des_key2 = int(deskey[24:48], 16) encrypted_des_key1 = pow(encrypted_des_key1, exp, module) encrypted_des_key2 = pow(encrypted_des_key2, exp, module) des1 = str(hex(encrypted_des_key1)) des2 = str(hex(encrypted_des_key2)) des = des1[des1.rindex('x') + 1:len(des1)] + des2[des2.rindex('x') + 1:len(des1)] key_file = open(file + "_key.dat", 'wb') encoded = rsa_enc_file.encode( 'RSAEncodedFile', { 'keyset': { 'key': { 'algid': b'\x00\x01', 'test': 'test', 'keydata': { 'module': module, 'exp': exp }, 'param': {}, 'ciphertext': { 'c': int(des, 16) } } }, 'last': { 'algid': b'\x01\x32', 'length': des.__len__() }, 'file': des }) key_file.write(encoded) key_file.close() print("File successfully encrypted to " + out_file) # print("File successfully encrypted to " + key_file) return out_file, file + "_key.dat"
import sys import chilkat crypt = chilkat.CkCrypt2() success = crypt.UnlockComponent("Anything for 30-day trial") if not success: print(crypt.lastErrorText()) sys.exit() # AES is also known as Rijndael. symmetric algorithm block ciphering crypt.put_CryptAlgorithm("aes") # CipherMode may be "ecb", "cbc", "ofb", "cfb", "gcm", etc. # Note: Check the online reference documentation to see the Chilkat versions # when certain cipher modes were introduced. crypt.put_CipherMode("cbc") # KeyLength may be 128, 192, 256 crypt.put_KeyLength(256) # The padding scheme determines the contents of the bytes # that are added to pad the result to a multiple of the # encryption algorithm's block size. AES has a block # size of 16 bytes, so encrypted output is always # a multiple of 16. crypt.put_PaddingScheme(0) # EncodingMode specifies the encoding of the output for # encryption, and the input for decryption. # It may be "hex", "url", "base64", or "quoted-printable". crypt.put_EncodingMode("hex")