def decrypt(self, pk, sk, ct): c1, c2 = ct['c1'], ct['c2'] key = abenc.decrypt(pk, sk, c1) print type(key), key cipher = AuthenticatedCryptoAbstraction(sha1(key)) print cipher return cipher.decrypt(c2)
def collision(msg1, msg2, h): #decrypt rand_key rec_key = maabe.decrypt(public_parameters, user_sk, h['cipher']['rkc']) #rec_key->symkey AE rec_symcrypt = AuthenticatedCryptoAbstraction(extractor(rec_key)) #symdecrypt rec_etdsumstr rec_etdsumbytes = rec_symcrypt.decrypt(h['cipher']['ec']) rec_etdsumstr = str(rec_etdsumbytes, encoding="utf8") #print("etdsumstr type=>",type(rec_etdsumstr)) #sumstr->etd str list rec_etdtolist = cut_text(rec_etdsumstr, 309) # print("rec_etdtolist=>",rec_etdtolist) #etd str list->etd integer list rec_etdint = { 'p1': integer(int(rec_etdtolist[0])), 'q1': integer(int(rec_etdtolist[1])) } #print("rec_etdint=>",rec_etdint) r1 = chamHash.collision(msg1, msg2, h, rec_etdint, pk) #if debug: print("new randomness =>", r1) new_h = { 'h': h['h'], 'r': r1, 'cipher': h['cipher'], 'N1': h['N1'], 'e': h['e'] } return new_h
def decrypt(self, pk, sk, ct): c1, c2 = ct['c1'], ct['c2'] key = abenc.decrypt(pk, sk, c1) if key is False: raise Exception("failed to decrypt!") cipher = AuthenticatedCryptoAbstraction(sha2(key)) return cipher.decrypt(c2)
def symDec(self, ciphtext): try: cipher = AuthenticatedCryptoAbstraction(self.symkey) return cipher.decrypt(ciphtext) except AttributeError: print('Please init a symmetric key') raise
def decrypt(self, sKey, serializedCiphertext): ''' Decrypt the provided ciphertext sing the secret key. Decryption is only successful if the policy embedded in the secret key matches the ciphertext access policy. NOTE: there is a bug in Charm (?) where a policy of a single value causes an error to be thrown: unsupported right operand types: int, bytes, str Sample code that throws the error: enc2 = EncryptionModule() policy = '(three)' attrs = ['ONE', 'TWO', 'THREE'] msg = "Hello world!" ct2 = enc2.encrypt(msg, policy) sk2 = enc2.generateUserKey(attrs) enc2.decrypt(sk2, ct2) ''' ciphertext = bytesToObject(serializedCiphertext, self.groupObj) c1, c2 = ciphertext['c1'], ciphertext['c2'] success = True try: key = self.cpabe.decrypt(self.public, sKey, c1) if (key == False): success = False except: success = False # Try to perform the decryption if we were able to recover the key plaintext = None if (success == True): cipher = AuthenticatedCryptoAbstraction(sha1(key)) plaintext = cipher.decrypt(c2) return (success, plaintext)
def MsgTestAESCBCSeperate(self, msg): groupObj = PairingGroup('SS512') ran = groupObj.random(GT) a = AuthenticatedCryptoAbstraction(sha1(ran)) ct = a.encrypt(msg) b = AuthenticatedCryptoAbstraction(sha1(ran)) dmsg = b.decrypt(ct) assert msg == dmsg, 'o: =>%s\nm: =>%s' % (msg, dmsg)
def decrypt(self, pk, sk, ct): c1, c2 = ct['c1'], ct['c2'] key = self.pkenc.decrypt(pk, sk, c1)[:self.key_len] if debug: print("Rec key =>", key, ", len =", len(key)) cipher = AuthenticatedCryptoAbstraction(key) msg = cipher.decrypt(c2) if debug: print("Rec msg =>", msg) return msg
def decrypt(self, pk, sk, ct): c1, c2 = ct['c1'], ct['c2'] key = self.pkenc.decrypt(pk, sk, c1)[:self.key_len] if debug: print("Rec key =>", key,", len =", len(key)) cipher = AuthenticatedCryptoAbstraction(key) msg = cipher.decrypt(c2) if debug: print("Rec msg =>", msg) return msg
def decAes(self, CT): if self.dek is None: raise Exception('DEK is null, cannot decrypt') a = AuthenticatedCryptoAbstraction(bytes(self.dek, "utf-8")) #CT_AES = a.encrypt(message) return a.decrypt(CT)
def decrypt(self, pk, sk, ct): c1, c2 = ct['c1'], ct['c2'] key = abenc.decrypt(pk, sk, c1) if key is False: print ("[WARNING] key failed to decrypt, message not intented for this device.") return None cipher = AuthenticatedCryptoAbstraction(sha2(key)) return cipher.decrypt(c2)
def MsgTestAESCBCSeperate(self,msg): groupObj = PairingGroup('SS512') ran = groupObj.random(GT) a = AuthenticatedCryptoAbstraction(sha1(ran)) ct = a.encrypt(msg) b = AuthenticatedCryptoAbstraction(sha1(ran)) dmsg = b.decrypt(ct); assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
def decryptAES(readFileCipherText, keyAES): pickleFileCipherText = pickleLoad(readFileCipherText) ct = byToOb(pickleFileCipherText) symcrypt = AuthenticatedCryptoAbstraction(extractor(keyAES)) recoveredMsg = symcrypt.decrypt(ct) print("Decrypted File using AES:") print(recoveredMsg.decode("utf-8")) return recoveredMsg
def parseList(self, packet, rpk): if packet.flag: symKey = hashlib.sha256(self.group.serialize(pair(self.sk, rpk))).digest() cipherRuner = AuthenticatedCryptoAbstraction(symKey) payload = cipherRuner.decrypt(packet.payload) L = json.loads(payload) return L else: raise "Banned!"
def decrypt(self, sKey, ciphertext): #return self.cpabe.decrypt(self.public, sKey, ciphertext) c1, c2 = ciphertext['c1'], ciphertext['c2'] success = True # TODO: we need to supress the print statement that comes out of this guy, to avoid unnecessary events try: key = self.cpabe.decrypt(self.public, sKey, c1) if (key == False): success = False except: success = False # Try to perform the encryption if we were able to recover the key plaintext = None if (success == True): cipher = AuthenticatedCryptoAbstraction(sha1(key)) plaintext = cipher.decrypt(c2) return (success, plaintext)
def decrypt(self, sKey, serializedCiphertext): ''' Decrypt the provided ciphertext sing the secret key. Decryption is only successful if the policy embedded in the secret key matches the ciphertext access policy. ''' ciphertext = bytesToObject(serializedCiphertext, PairingGroup('SS512')) c1, c2 = ciphertext['c1'], ciphertext['c2'] success = True try: key = self.cpabe.decrypt(self.public, sKey, c1) if (key == False): success = False except: success = False # Try to perform the decryption if we were able to recover the key plaintext = None if (success == True): cipher = AuthenticatedCryptoAbstraction(sha1(key)) plaintext = cipher.decrypt(c2) return (success, plaintext)
def i2v(self, CList, kunodes): Package = namedtuple('Package', 'destination payload flag') dataStream = [] candidate = [] pseuIDs = [] flags = [] tempRunner = [] for ct in CList: U = ct[0] V = ct[1] pseuID = self.group.deserialize( int2Bytes( V ^ integer(self.group.serialize(pair(U, self.sk))))) symKey = hashlib.sha256(self.group.serialize(pair(pseuID, self.sk))).digest() cipherRuner = AuthenticatedCryptoAbstraction(symKey) tempRunner.append(cipherRuner) pathList = json.loads(cipherRuner.decrypt(ct[2])) pathValue = [int(x) for x in pathList] tempFlag = set(pathValue) & kunodes flags.append(bool(tempFlag)) pseuIDs.append(pseuID) if tempFlag: candidate.append(str(self.group.serialize(pseuID), encoding = 'utf-8')) # dataStream.append(Package(pseuID, '', set(pathValue) & kunodes)) for index, pid in enumerate(pseuIDs): if flags[index]: payload = tempRunner[index].encrypt(json.dumps(candidate)) dataStream.append(Package(pid, payload, flags[index])) else: dataStream.append(Package(pid, 'You are blocked', flags[index])) return dataStream
def decrypt(self, gp, sk, ct): c1, c2 = ct['c1'], ct['c2'] key = abencma.decrypt(gp, sk, c1) cipher = AuthenticatedCryptoAbstraction(sha1(key)) return cipher.decrypt(c2)
def decrypt(self, ct, sk): c1, c2 = ct['c1'], ct['c2'] key = abenc.decrypt(c1, sk) cipher = AuthenticatedCryptoAbstraction(extract_key(key)) return cipher.decrypt(c2)
def decrypt(self, pk, ID, ct): c1, c2 = ct['c1'], ct['c2'] key = ibenc.decrypt(pk, ID, c1) cipher = AuthenticatedCryptoAbstraction(sha2(key)) return cipher.decrypt(c2)
def decrypt(self, pk, ID, ct): c1, c2 = ct['c1'], ct['c2'] key = ibenc.decrypt(pk, ID, c1) cipher = AuthenticatedCryptoAbstraction(sha1(key)) return cipher.decrypt(c2)
def main(): groupObj = PairingGroup('SS512') maabe = MAABE.MaabeRW15(groupObj) attrs1 = ['ONE', 'TWO'] attrs2 = ['THREE', 'FOUR'] access_policy = '((STUDENT@UT or PROFESSOR@OU) and (STUDENT@UT or MASTERS@OU))' if debug: print("attrs1 =>", attrs1) print("attrs2 =>", attrs2) print("Policy =>", access_policy) # setup public_parameters = maabe.setup() # authsetup 2AA (pk1, sk1) = maabe.authsetup(public_parameters, 'UT') (pk2, sk2) = maabe.authsetup(public_parameters, 'OU') maabepk = {'UT': pk1, 'OU': pk2} # keygen chamHash = chamwithemp.Chamwithemp() (pk, sk) = chamHash.keygen(1024) # keygen Bob gid = "bob" user_attr1 = ['STUDENT@UT'] user_attr2 = ['STUDENT@OU'] user_sk1 = maabe.multiple_attributes_keygen(public_parameters, sk1, gid, user_attr1) user_sk2 = maabe.multiple_attributes_keygen(public_parameters, sk2, gid, user_attr2) user_sk = {'GID': gid, 'keys': merge_dicts(user_sk1, user_sk2)} # hash msg = "Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for t" xi = chamHash.hash(pk, msg) etd = [xi['p1'], xi['q1']] if debug: print("Hash...") if debug: print("hash result =>", xi) # encrypt rand_key = groupObj.random(GT) if debug: print("msg =>", rand_key) #encrypt rand_key maabect = maabe.encrypt(public_parameters, maabepk, rand_key, access_policy) #rand_key->symkey AE symcrypt = AuthenticatedCryptoAbstraction(extractor(rand_key)) #symcrypt msg(etd=(p1,q1)) etdtostr = [str(i) for i in etd] etdsumstr = etdtostr[0] + etdtostr[1] symct = symcrypt.encrypt(etdsumstr) ct = {'rkc': maabect, 'ec': symct} if debug: print("\n\nCiphertext...\n") groupObj.debug(ct) print("ciphertext:=>", ct) # decrypt #decrypt rand_key rec_key = maabe.decrypt(public_parameters, user_sk, maabect) assert rand_key == rec_key, "FAILED Decryption: random key is incorrect" #rec_key->symkey AE rec_symcrypt = AuthenticatedCryptoAbstraction(extractor(rec_key)) #symdecrypt rec_etdsumstr rec_etdsumbytes = rec_symcrypt.decrypt(ct['ec']) rec_etdsumstr = str(rec_etdsumbytes, encoding="utf8") print("etdsumstr type=>", type(rec_etdsumstr)) #sumstr->etd str list rec_etdtolist = cut_text(rec_etdsumstr, len(etdtostr[0])) print("rec_etdtolist=>", rec_etdtolist) #etd str list->etd integer list rec_etdint = [ integer(int(rec_etdtolist[0])), integer(int(rec_etdtolist[1])) ] print("rec_etdint=>", rec_etdint) if debug: print("\n\nDecrypt...\n") if debug: print("Successful Decryption!!!") # collision msg1 = "Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in the embed code for p" assert xi['p1'] == rec_etdint[ 0], "FAILED Decryption: etd key p1 is incorrect" assert xi['q1'] == rec_etdint[ 1], "FAILED Decryption: etd key q1 is incorrect" r1 = chamHash.collision(msg, msg1, xi, sk, pk) if debug: print("new randomness =>", r1) if debug: print("collision generated correctly!!!")
params = bytesToObject(bin_data, group) ''' End set up''' ''' Load user key''' f = open('./userkeys/' + ID + '.key', 'r') bin_data = f.read() id_secret_key = bytesToObject(bin_data, group) f.close() ''' End loading user key ''' ''' Load ciphertexts ''' f = open('./ciphertexts/' + ID + file_name + '.enc', 'r') bin_data = f.readline() ct = json.loads(bin_data) A = bytesToObject(ct['CA'], group) B = bytesToObject(ct['CB'], group) C = bytesToObject(ct['CC'], IntegerGroup()) S = bytesToObject(ct['S'], group) C_ = {'A': A, 'B': B, 'C': C} encr_key = {'S': S, 'C': C_} ciphertext = ct['Data'] f.close() ''' End loading ciphertexts ''' '''Symmetric encryption key and algorithm set up''' sym_key = ibp.decryptFirstLevel(params, id_secret_key, encr_key, ID) sym_cipher = AuthenticatedCryptoAbstraction(sym_key) '''End set up''' ''' File decyption process ''' file_decrypted = sym_cipher.decrypt(ciphertext) f = open('./plaintexts/' + file_name, 'w') f.write(file_decrypted) f.close()
def MsgtestAESCBC(self,msg): groupObj = PairingGroup('SS512') a = AuthenticatedCryptoAbstraction(sha1(groupObj.random(GT))) ct = a.encrypt(msg) dmsg = a.decrypt(ct); assert msg == dmsg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
def MsgtestAESCBC(self, msg): groupObj = PairingGroup('SS512') a = AuthenticatedCryptoAbstraction(sha1(groupObj.random(GT))) ct = a.encrypt(msg) dmsg = a.decrypt(ct) assert msg == dmsg, 'o: =>%s\nm: =>%s' % (msg, dmsg)
def decrypt_jet(self, params, skid, cid): c1, c2 = cid['c1'], cid['c2'] key = self.decrypt(params, skid, c1) cipher = AuthenticatedCryptoAbstraction(sha1(key)) return cipher.decrypt(c2)
def decrypt(self, ct, sk): c1, c2 = ct['c1'], ct['c2'] key = abenc.decrypt(c1, sk) cipher = AuthenticatedCryptoAbstraction(sha1(key)) return cipher.decrypt(c2)
(serPKr, serMKr, conversionFactor) = W.refresh(serPK, serMK) #refresh one more time :) print(conversionFactor) (serPKr, serMKr, conversionFactor) = W.refresh(serPK, serMK) print(conversionFactor) (serPKr, serMKr, conversionFactor) = W.refresh(serPK, serMK) print(conversionFactor) (serPKr, serMKr, conversionFactor) = W.refresh(serPK, serMK) print(conversionFactor) serSKr = W.refreshSK(serSK, conversionFactor) newCT = W.encrypt(serPKr, message, access_policy) print("ratchat applied and the message is encrypted by conversed SK") try: mdec = W.decrypt(serPK, serSK, newCT) except ValueError as e: print("good: got ValueError, when trying to decrypt with the old key: " + repr(e)) newMdec = W.decrypt(serPKr, serSKr, newCT) assert mdec == message, "Failed Decryption!!!" print("done: ABE - ratchet") #-------- DEK = W.getDEK() from charm.toolbox.symcrypto import AuthenticatedCryptoAbstraction a = AuthenticatedCryptoAbstraction(DEK) CT_AES = a.encrypt(message) mdec = a.decrypt(CT_AES) assert mdec == message, "Failed Decryption!!!" print("done: AES")
pairing_cipher = cpabe.encrypt(master_public_key, pairing, policy) # send data (msg_cipher and pairing_cipher) to client # retrieve pairing used to encrypt byte message decrypted_pairing = cpabe.decrypt( master_public_key, secret_key, pairing_cipher) # generate decoder with decrypted_pairing symcrypt_resciever = AuthenticatedCryptoAbstraction( extractor(decrypted_pairing)) # decryption recoveredMsg = symcrypt_resciever.decrypt(msg_cipher) print(recoveredMsg) ''' >>> cpabe = CPabe09(group) >>> msg = group.random(GT) >>> (master_secret_key, master_public_key) = cpabe.setup() >>> policy = '((ONE or THREE) and (TWO or FOUR))' >>> attr_list = ['THREE', 'ONE', 'TWO'] >>> secret_key = cpabe.keygen(master_public_key, master_secret_key, attr_list) >>> cipher_text = cpabe.encrypt(master_public_key, msg, policy) >>> decrypted_msg = cpabe.decrypt(master_public_key, secret_key, cipher_text) >>> decrypted_msg == msg '''