def encrypt(self, pk, M, object): key = self.group.random(GT) c1 = abenc.encrypt(pk, key, object) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return { 'c1':c1, 'c2':c2 }
def encrypt(self, pk, gp, M, policy_str): if type(M) != bytes and type(policy_str) != str: raise "message and policy not right type!" key = group.random(GT) c1 = abencma.encrypt(pk, gp, key, policy_str) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return { 'c1':c1, 'c2':c2 }
def encrypt(self, pk, ID, M): if type(M) != bytes: raise "message not right type!" key = group.random(GT) c1 = ibenc.encrypt(pk, ID, key) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(M) return { 'c1':c1, 'c2':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 encrypt(self, plaintext, policy): #return self.cpabe.encrypt(self.public, plaintext, policy) key = self.groupObj.random(GT) c1 = self.cpabe.encrypt(self.public, key, policy) # instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(plaintext) return { 'c1':c1, 'c2':c2 }
def encAES(self, m): if self.dek is None: raise Exception('DEK is null, cannot encrypt') a = AuthenticatedCryptoAbstraction(bytes(self.dek, "utf-8")) CT_AES = a.encrypt(m) groupObj = PairingGroup('SS512') return objectToBytes(CT_AES, groupObj)
def encrypt(self, plaintext, policy): ''' Encrypt a block of plaintext using the provided polcy structure. The ciphertext is stored as a dictionary, for now. ''' key = self.groupObj.random(GT) c1 = self.cpabe.encrypt(self.public, key, policy) # Instantiate a symmetric enc scheme from this key cipher = AuthenticatedCryptoAbstraction(sha1(key)) c2 = cipher.encrypt(plaintext) return objectToBytes({ 'c1':c1, 'c2':c2 }, self.groupObj)
def encrypt(self, pk, M): # generate a short session key, K and encrypt using pkenc key = OpenSSLRand().getRandomBytes(self.key_len) # urandom(self.key_len) # encrypt session key using PKEnc c1 = self.pkenc.encrypt(pk, key) # use symmetric key encryption to enc actual message cipher = AuthenticatedCryptoAbstraction(key) c2 = cipher.encrypt(M) if debug: print("Ciphertext 2...") if debug: print(c2) return { 'c1':c1, 'c2':c2 }
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)
'''Group set up''' group = PairingGroup('SS512', secparam=1024) ibp = PreGA(group) f = open('./setup/master.param', 'r') bin_data = f.read() params = bytesToObject(bin_data, group) f.close() ''' End set up''' '''Symmetric encryption key and algorithm set up''' sym_key = OpenSSLRand().getRandomBytes(16) sym_cipher = AuthenticatedCryptoAbstraction(sym_key) '''End set up''' '''Encryption proccess''' sym_key_ciphertext = ibp.encrypt(params, ID, sym_key) # encrypt the symmetric encryption key f = open(file_name, 'r') file_data = f.read() file_ciphertext = sym_cipher.encrypt(file_data) '''End of encryption process''' ct = { 'CA': objectToBytes(sym_key_ciphertext['C']['A'], group), 'CB': objectToBytes(sym_key_ciphertext['C']['B'], group), 'CC': objectToBytes(sym_key_ciphertext['C']['C'], IntegerGroup()), 'S': objectToBytes(sym_key_ciphertext['S'], group), 'Data': file_ciphertext } f = open('./ciphertexts/' + ID + file_name + '.enc', 'w') f.write(json.dumps(ct)) 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 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!!!")
alpha, a = group.random(), group.random() cpabe = CPabe09(group) (master_secret_key, master_public_key) = cpabe.setup(g1, g2, alpha, a) # set up the policy for abe policy = '((ONE or THREE) and (TWO or FOUR))' # define the attributes for abe attr_list = ['ONE', 'TWO', 'THREE'] # get the secret key for abe secret_key = cpabe.keygen(master_public_key, master_secret_key, attr_list) # encryption of byte message with the pairing msg_cipher = symcrypt_sender.encrypt(msg) # encrypt the pairing 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))
(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")