def encrypto(self, payload, key, iv): aesKey = SecretKeySpec(base64.b64decode(key), "AES") aesIV = IvParameterSpec(base64.b64decode(iv)) cipher = Cipher.getInstance("AES/CBC/PKCS7Padding") cipher.init(Cipher.ENCRYPT_MODE, aesKey, aesIV) encrypted = cipher.doFinal(payload) return Base64.getEncoder().encode(encrypted)
def decryptJython(payload, key, iv): decoded = Base64.getDecoder().decode(payload) aesKey = SecretKeySpec(key, "AES") aesIV = IvParameterSpec(iv) cipher = Cipher.getInstance("AES/CFB/NOPADDING") cipher.init(Cipher.DECRYPT_MODE, aesKey, aesIV) return cipher.doFinal(decoded)
def encryptJython(payload, key, iv): aesKey = SecretKeySpec(key, "AES") aesIV = IvParameterSpec(iv) cipher = Cipher.getInstance("AES/CFB/NOPADDING") cipher.init(Cipher.ENCRYPT_MODE, aesKey, aesIV) encrypted = cipher.doFinal(payload) return Base64.getEncoder().encode(encrypted)
def test_jce(): if 1: cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, 'AES'), IvParameterSpec(iv)) else: cipher = Cipher.getInstance("AES/ECB/PKCS5Padding") cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(key, 'AES')) p1 = "this is a test of part one" p2 = "now part two" p3 = "last, part three aaaaaaa" #s1 = cipher.update(p1) #s2 = cipher.update(p2) #s3 = cipher.update(p3) #secret = cipher.doFinal() secret = cipher.doFinal(plaintext) print binascii.b2a_hex(secret.tostring())
def AESCipherdecrypt(self, key, enc): enc, iv = enc.split(':') cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING") cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(key.decode("hex"), "AES"), IvParameterSpec(iv.decode("hex"))) decrypted_password = cipher.doFinal(enc.decode("hex")) return decrypted_password.tostring()
def decryptPassword(encryptedpassword, key): secretKey = Base64.decodeBase64(key) seckeySpec = SecretKeySpec(secretKey, "AES") if encryptedpassword is not None and encryptedpassword is not ' ': encData = Base64.decodeBase64(encryptedpassword) cipher.init(Cipher.DECRYPT_MODE, seckeySpec) decrypted = cipher.doFinal(encData) originalString = decrypted.tostring() return originalString
def decrypto(self, payload, key, iv): decoded = base64.b64decode(payload) aesKey = SecretKeySpec(base64.b64decode(key), "AES") aesIV = IvParameterSpec(base64.b64decode(iv)) cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC") cipher.init(Cipher.DECRYPT_MODE, aesKey, aesIV) return String(cipher.doFinal(decoded))
def encrypt(payload, key): salt = bytearray([1,2,3,4,5,6,7,8]) k2, iv2 = derive_key_and_iv(key, salt, 32, 16) aesKey = SecretKeySpec(k2, "AES") aesIV = IvParameterSpec(iv2) # print key, binascii.hexlify(salt), binascii.hexlify(k2), binascii.hexlify(iv2), payload, "TESTING - ENCRYPT" cipher = Cipher.getInstance("AES/CBC/PKCS7Padding") cipher.init(Cipher.ENCRYPT_MODE, aesKey, aesIV) encrypted = cipher.doFinal(payload) out = Base64.getEncoder().encode(b'Salted__' + salt + encrypted) # print("DEBUG enc:", out.tostring(), binascii.hexlify(salt), binascii.hexlify(k2), binascii.hexlify(iv2), key) return out
def drm_cipher_file(transform, opmode, key, iv, name_in, name_out): fin = open(name_in, "rb") fout = open(name_out, "wb") cipher = Cipher.getInstance(transform) cipher.init(opmode, SecretKeySpec(key, 'AES'), IvParameterSpec(iv)) cache_len = 64 while True: buf = fin.read(cache_len) if not buf: fout.write(cipher.doFinal()) break buf2 = cipher.update(buf) fout.write(buf2) fin.close() fout.close()
def decryptAES(self, key, encryptedStr): # make sure key length is 16 bytes (128 bits) if ( len(key) != 16 ): return None # split the encrypted string into IV and ciphertext iv, encrypted = encryptedStr[:16], encryptedStr[16:] # configure IV and key specification skeySpec = SecretKeySpec(key, "AES") ivspec = IvParameterSpec(iv) # setup cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec) # decrypt the plaintext encodedBytes = base64.b64decode( b'' + encrypted ) decodedBytes = cipher.doFinal( encodedBytes ) plaintext = ''.join(chr(i) for i in decodedBytes) return plaintext
def encryptAES(self, key, toEncrypt): # make sure key length is 16 bytes (128 bits) if ( len(key) != 16 ): return None # generate a random IV randomSource = string.ascii_letters + string.digits iv = ''.join(random.SystemRandom().choice(randomSource) for i in range(16)) # configure IV and key specification skeySpec = SecretKeySpec(key, "AES") ivspec = IvParameterSpec(iv) # setup cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec) # encrypt the plaintext encryptedBytes = cipher.doFinal( toEncrypt.encode('utf-8') ) encryptedValue = base64.b64encode( encryptedBytes ) return iv.encode("ascii") + encryptedValue
def __init__(self, alg, mode, key, iv=None): modestr = _modes.get(mode, "/ECB/NoPadding") keyspec = SecretKeySpec(key, alg) self.__keysize = len(key) self.IV = iv if iv is None or mode == blockalgo.MODE_ECB: if mode != blockalgo.MODE_ECB: raise ValueError('mode requires an IV') self.__cryptor = JCECipher.getInstance(alg + modestr) self.__cryptor.init(JCECipher.ENCRYPT_MODE, keyspec) self.__decryptor = JCECipher.getInstance(alg + modestr) self.__decryptor.init(JCECipher.DECRYPT_MODE, keyspec) else: ivspec = IvParameterSpec(iv) self.__cryptor = JCECipher.getInstance(alg + modestr) self.__cryptor.init(JCECipher.ENCRYPT_MODE, keyspec, ivspec) self.__decryptor = JCECipher.getInstance(alg + modestr) self.__decryptor.init(JCECipher.DECRYPT_MODE, keyspec, ivspec)
def decrypt(payload, key): decoded = Base64.getDecoder().decode(payload) # print("Lol - decoded: ", decoded) if decoded.tostring()[:8] != "Salted__": print decoded.tostring()[:8] return False decoded = decoded[8:] salt = decoded[:8] k2, iv2 = derive_key_and_iv(key, salt, 32, 16) # print key, binascii.hexlify(salt), binascii.hexlify(k2), binascii.hexlify(iv2), payload, "TESTING - DECRYPT" aesKey = SecretKeySpec(k2, "AES") aesIV = IvParameterSpec(iv2) cipher = Cipher.getInstance("AES/CBC/PKCS7Padding") cipher.init(Cipher.DECRYPT_MODE, aesKey, aesIV) return cipher.doFinal(decoded[8:])
def __init__(self, gateway_host, gateway_password, private_password): self.host = gateway_host key1 = hashlib.md5() key1.update(gateway_password) key1.update(self.SALT) key2 = hashlib.md5() key2.update(self.SALT) key2.update(private_password) self.key = key1.digest() + key2.digest() self.opener = urllib2.build_opener() self.opener.addheaders = [('User-agent', 'TeleHeater/2.2.3'), ('Accept', 'application/json')] self.secretKey = SecretKeySpec(self.key, "AES") self.cipher = Cipher.getInstance("AES/ECB/NoPadding")
def aesEncrypt(data, key): cipher = Cipher.getInstance('AES/CTR/NoPadding') skeySpec = SecretKeySpec(key, 'AES') cipher.init(Cipher.ENCRYPT_MODE, skeySpec, _iv) return cipher.doFinal(data).tostring()
from java.util import Properties from java.math import BigInteger from java.security import * from javax.crypto import KeyGenerator, SecretKey, Cipher from javax.crypto.spec import SecretKeySpec from org.apache.log4j import Logger from org.apache.commons.codec.binary import Base64 log = Logger.getLogger('password_encrypter') kgen = KeyGenerator.getInstance("AES") kgen.init(128) skey = kgen.generateKey() raw = skey.getEncoded() rawkey = Base64.encodeBase64(raw).tostring() skeySpec = SecretKeySpec(raw, "AES") cipher = Cipher.getInstance("AES") def encryptPassword(password): if password is not None and password is not ' ': cipher.init(Cipher.ENCRYPT_MODE, skeySpec) encrypted = cipher.doFinal(password) encodeTxt = Base64.encodeBase64(encrypted) return encodeTxt.tostring() def encryptAllPasswords(configfile): log.info("Encrypting Passwords in config file") if (configfile is not None and not ''): if not configfile.endswith('.properties'):
def decrypt(key, ciphertext): secretkey = SecretKeySpec(key, 'AES') cipher = Cipher.getInstance('AES/ECB/NoPadding') cipher.init(Cipher.DECRYPT_MODE, secretkey) decrypted = cipher.doFinal(ciphertext) return decrypted.tostring()