我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用Crypto.Cipher.DES.MODE_ECB。
def decrypt_secret(secret, key): """Python implementation of SystemFunction005. Decrypts a block of data with DES using given key. Note that key can be longer than 7 bytes.""" decrypted_data = '' j = 0 # key index for i in range(0,len(secret),8): enc_block = secret[i:i+8] block_key = key[j:j+7] des_key = str_to_key(block_key) des = DES.new(des_key, DES.MODE_ECB) decrypted_data += des.decrypt(enc_block) j += 7 if len(key[j:j+7]) < 7: j = len(key[j:j+7]) (dec_data_len,) = unpack("<L", decrypted_data[:4]) return decrypted_data[8:8+dec_data_len]
def encrypt(self, data): """Encrypt sensitive data by KIK. For (3)DES, data must be padded to BS. For AES, if data not BS-alligned, they are padded by '80..00'""" l = len(data) if self.zAES: l %= 16 if l > 0: data += '\x80' + '\0'*(15-l) key = AES.new(self.keyValue, AES.MODE_CBC, IV='\0'*16) else: # suppose 8B aligned data assert l % 8 == 0 # for (3)DES KIK, ECB is used # KeyType.DES_IMPLICIT is supposed to be 3DES ECB if self.keyType in (KeyType.TDES_CBC, KeyType.DES_IMPLICIT): key = DES3.new(self.keyValue, DES.MODE_ECB) elif self.keyType in (KeyType.DES_ECB, KeyType.DES_CBC): key = DES.new(self.keyValue, DES.MODE_ECB) else: raise ValueError("Unknown key type %02X" % self.keyType) return key.encrypt(data)
def runTest(self): from Crypto.Cipher import DES from binascii import b2a_hex X = [] X[0:] = [b('\x94\x74\xB8\xE8\xC7\x3B\xCA\x7D')] for i in range(16): c = DES.new(X[i],DES.MODE_ECB) if not (i&1): # (num&1) returns 1 for odd numbers X[i+1:] = [c.encrypt(X[i])] # even else: X[i+1:] = [c.decrypt(X[i])] # odd self.assertEqual(b2a_hex(X[16]), b2a_hex(b('\x1B\x1A\x2D\xDB\x4C\x64\x24\x38')))
def decryptSecret(key, value): # [MS-LSAD] Section 5.1.2 plainText = '' key0 = key for i in range(0, len(value), 8): cipherText = value[:8] tmpStrKey = key0[:7] tmpKey = transformKey(tmpStrKey) Crypt1 = DES.new(tmpKey, DES.MODE_ECB) plainText += Crypt1.decrypt(cipherText) cipherText = cipherText[8:] key0 = key0[7:] value = value[8:] # AdvanceKey if len(key0) < 7: key0 = key[len(key0):] secret = LSA_SECRET_XP(plainText) return (secret['Secret'])
def encryptSecret(key, value): # [MS-LSAD] Section 5.1.2 plainText = '' cipherText = '' key0 = key value0 = pack('<LL', len(value), 1) + value for i in range(0, len(value0), 8): if len(value0) < 8: value0 = value0 + '\x00'*(8-len(value0)) plainText = value0[:8] tmpStrKey = key0[:7] tmpKey = transformKey(tmpStrKey) Crypt1 = DES.new(tmpKey, DES.MODE_ECB) cipherText += Crypt1.encrypt(plainText) plainText = plainText[8:] key0 = key0[7:] value0 = value0[8:] # AdvanceKey if len(key0) < 7: key0 = key[len(key0):] return cipherText
def SamDecryptNTLMHash(encryptedHash, key): # [MS-SAMR] Section 2.2.11.1.1 Block1 = encryptedHash[:8] Block2 = encryptedHash[8:] Key1 = key[:7] Key1 = transformKey(Key1) Key2 = key[7:14] Key2 = transformKey(Key2) Crypt1 = DES.new(Key1, DES.MODE_ECB) Crypt2 = DES.new(Key2, DES.MODE_ECB) plain1 = Crypt1.decrypt(Block1) plain2 = Crypt2.decrypt(Block2) return plain1 + plain2
def calcKCV(keyValue, zAES=False): """Calculate KCV for symmetric key. keyValue - key values as string (DES, 3DES2k, 3DES3k, AES) zAES - True if key is AES (i.e. encrypt block of '01' instead of '00') Return 3B-long string.""" if zAES: assert len(keyValue) in (16, 24, 32), "Wrong length of AES key" block = '\x01'*16 tkey = AES.new(keyValue, AES.MODE_ECB) else: assert len(keyValue) in (8, 16, 24), "Wrong length of (3)DES key" block = '\x00'*8 if len(keyValue) == 8: tkey = DES.new(keyValue, DES.MODE_ECB) else: tkey = DES3.new(keyValue, DES.MODE_ECB) return tkey.encrypt(block)[:3]
def decrypt_secret(secret, key): """Python implementation of SystemFunction005. Decrypts a block of data with DES using given key. Note that key can be longer than 7 bytes.""" decrypted_data = '' j = 0 # key index for i in range(0, len(secret), 8): enc_block = secret[i:i + 8] block_key = key[j:j + 7] des_key = hashdump.str_to_key(block_key) des = DES.new(des_key, DES.MODE_ECB) decrypted_data += des.decrypt(enc_block) j += 7 if len(key[j:j + 7]) < 7: j = len(key[j:j + 7]) (dec_data_len,) = struct.unpack("<L", decrypted_data[:4]) return decrypted_data[8:8 + dec_data_len]
def __decryptSecret(self, key, value): # [MS-LSAD] Section 5.1.2 plainText = '' encryptedSecretSize = unpack('<I', value[:4])[0] value = value[len(value)-encryptedSecretSize:] key0 = key for i in range(0, len(value), 8): cipherText = value[:8] tmpStrKey = key0[:7] tmpKey = self.__cryptoCommon.transformKey(tmpStrKey) Crypt1 = DES.new(tmpKey, DES.MODE_ECB) plainText += Crypt1.decrypt(cipherText) key0 = key0[7:] value = value[8:] # AdvanceKey if len(key0) < 7: key0 = key[len(key0):] secret = LSA_SECRET_XP(plainText) return secret['Secret']
def SamEncryptNTLMHash(encryptedHash, key): # [MS-SAMR] Section 2.2.11.1.1 Block1 = encryptedHash[:8] Block2 = encryptedHash[8:] Key1 = key[:7] Key1 = transformKey(Key1) Key2 = key[7:14] Key2 = transformKey(Key2) Crypt1 = DES.new(Key1, DES.MODE_ECB) Crypt2 = DES.new(Key2, DES.MODE_ECB) plain1 = Crypt1.encrypt(Block1) plain2 = Crypt2.encrypt(Block2) return plain1 + plain2
def des_base64_encrypt(cls, reqdata): """ ??DES?base64????? @:param reqdata ????????? """ key = cls.des_key length = len(reqdata) if length < cls.block_size: add = cls.block_size - length elif length > cls.block_size: add = cls.block_size - (length % cls.block_size) else: add = 8 reqdata = reqdata + (cls.pad_str[add-1] * add) des = DES.new(key, DES.MODE_ECB) encrypt_data = des.encrypt(reqdata) return base64.b64encode(encrypt_data)
def decrypt_secret(secret, key): """Python implementation of SystemFunction005. Decrypts a block of data with DES using given key. Note that key can be longer than 7 bytes.""" decrypted_data = '' j = 0 # key index for i in range(0, len(secret), 8): enc_block = secret[i:i + 8] block_key = key[j:j + 7] des_key = hashdump.str_to_key(block_key) des = DES.new(des_key, DES.MODE_ECB) enc_block = enc_block + "\x00" * int(abs(8 - len(enc_block)) % 8) decrypted_data += des.decrypt(enc_block) j += 7 if len(key[j:j + 7]) < 7: j = len(key[j:j + 7]) (dec_data_len,) = struct.unpack("<L", decrypted_data[:4]) return decrypted_data[8:8 + dec_data_len]
def decrypt_single_hash(rid, hbootkey, enc_hash, lmntstr): (des_k1,des_k2) = sid_to_key(rid) d1 = DES.new(des_k1, DES.MODE_ECB) d2 = DES.new(des_k2, DES.MODE_ECB) md5 = MD5.new() md5.update(hbootkey[:0x10] + pack("<L",rid) + lmntstr) rc4_key = md5.digest() rc4 = ARC4.new(rc4_key) obfkey = rc4.encrypt(enc_hash) hash = d1.decrypt(obfkey[:8]) + d2.decrypt(obfkey[8:]) return hash
def decrypt_config_bin(self, data): key = '\x47\x8D\xA5\x0B\xF9\xE3\xD2\xCF' crypto = DES.new(key, DES.MODE_ECB) data_decrypted = crypto.decrypt(data).rstrip('\0') authKey, cPskSecret, cUsrPIN = self.parse(data_decrypted) passwd = self.decrypt_authKey(authKey) return passwd, authKey, cPskSecret, cUsrPIN
def ComputeNetlogonCredential(inputData, Sk): k1 = Sk[:7] k3 = crypto.transformKey(k1) k2 = Sk[7:14] k4 = crypto.transformKey(k2) Crypt1 = DES.new(k3, DES.MODE_ECB) Crypt2 = DES.new(k4, DES.MODE_ECB) cipherText = Crypt1.encrypt(inputData) return Crypt2.encrypt(cipherText) # Section 3.1.4.4.1
def removeDESLayer(cryptedHash, rid): Key1,Key2 = deriveKey(rid) Crypt1 = DES.new(Key1, DES.MODE_ECB) Crypt2 = DES.new(Key2, DES.MODE_ECB) decryptedHash = Crypt1.decrypt(cryptedHash[:8]) + Crypt2.decrypt(cryptedHash[8:]) return decryptedHash
def __decryptHash(self, rid, cryptedHash, constant): # Section 2.2.11.1.1 Encrypting an NT or LM Hash Value with a Specified Key # plus hashedBootKey stuff Key1,Key2 = self.__cryptoCommon.deriveKey(rid) Crypt1 = DES.new(Key1, DES.MODE_ECB) Crypt2 = DES.new(Key2, DES.MODE_ECB) rc4Key = self.MD5( self.__hashedBootKey[:0x10] + pack("<L",rid) + constant ) rc4 = ARC4.new(rc4Key) key = rc4.encrypt(cryptedHash) decryptedHash = Crypt1.decrypt(key[:8]) + Crypt2.decrypt(key[8:]) return decryptedHash
def __removeDESLayer(self, cryptedHash, rid): Key1,Key2 = self.__cryptoCommon.deriveKey(int(rid)) Crypt1 = DES.new(Key1, DES.MODE_ECB) Crypt2 = DES.new(Key2, DES.MODE_ECB) decryptedHash = Crypt1.decrypt(cryptedHash[:8]) + Crypt2.decrypt(cryptedHash[8:]) return decryptedHash
def __DES_block(key, msg): if POW: cipher = POW.Symmetric(POW.DES_ECB) cipher.encryptInit(__expand_DES_key(key)) return cipher.update(msg) else: cipher = DES.new(__expand_DES_key(key),DES.MODE_ECB) return cipher.encrypt(msg)
def __init__(self, key): """ Constructor, key is 3DES2k.""" assert len(key) == 16, "3DES key must be 16B long" self.e = DES.new(key[:8], DES.MODE_ECB) self.d = DES.new(key[8:], DES.MODE_ECB)
def setValue(self, keyvalue): keyvalue = ''.join(keyvalue) if self.zAES: assert len(keyvalue) in (16, 24, 32) cipher = AES.new(keyvalue, AES.MODE_ECB) poly = cipher.encrypt('\x00'*self.BS) self.xorKey1 = self.polyMulX(poly) self.xorKey2 = self.polyMulX(self.xorKey1) else: if len(keyvalue) != self.keysize: raise ValueError("KIC/KID=%02X, value length received %d" % (self.iKICD, len(keyvalue))) self.keyval = keyvalue
def __init__(self, keyValue): assert len(keyValue) in (16, 24), "Wrong length of 3DES key" self.key = DES3.new(keyValue, DES.MODE_ECB) self.zAES = False