本文整理汇总了Python中Crypto.Cipher.DES3.MODE_CBC属性的典型用法代码示例。如果您正苦于以下问题:Python DES3.MODE_CBC属性的具体用法?Python DES3.MODE_CBC怎么用?Python DES3.MODE_CBC使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类Crypto.Cipher.DES3
的用法示例。
在下文中一共展示了DES3.MODE_CBC属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getPrivateKeyObject_openssh
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def getPrivateKeyObject_openssh(data, passphrase):
kind = data[0][11: 14]
if data[1].startswith('Proc-Type: 4,ENCRYPTED'): # encrypted key
ivdata = data[2].split(',')[1][:-1]
iv = ''.join([chr(int(ivdata[i:i+2],16)) for i in range(0, len(ivdata), 2)])
if not passphrase:
raise BadKeyError, 'encrypted key with no passphrase'
ba = md5.new(passphrase + iv).digest()
bb = md5.new(ba + passphrase + iv).digest()
decKey = (ba + bb)[:24]
b64Data = base64.decodestring(''.join(data[4:-1]))
keyData = DES3.new(decKey, DES3.MODE_CBC, iv).decrypt(b64Data)
removeLen = ord(keyData[-1])
keyData = keyData[:-removeLen]
else:
keyData = base64.decodestring(''.join(data[1:-1]))
try:
decodedKey = asn1.parse(keyData)
except Exception, e:
raise BadKeyError, 'something wrong with decode'
示例2: test_aes_128
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def test_aes_128(self):
key = '2b7e151628aed2a6abf7158809cf4f3c'
iv = '000102030405060708090a0b0c0d0e0f'
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
'ae2d8a571e03ac9c9eb76fac45af8e51' +\
'30c81c46a35ce411e5fbc1191a0a52ef' +\
'f69f2445df4f9b17ad2b417be66c3710'
ciphertext = '7649abac8119b246cee98e9b12e9197d' +\
'5086cb9b507219ee95db113a917678b2' +\
'73bed6b8e3c1743b7116e69e22229516' +\
'3ff1caa1681fac09120eca307586e1a7'
key = unhexlify(key)
iv = unhexlify(iv)
plaintext = unhexlify(plaintext)
ciphertext = unhexlify(ciphertext)
cipher = AES.new(key, AES.MODE_CBC, iv)
self.assertEqual(cipher.encrypt(plaintext), ciphertext)
cipher = AES.new(key, AES.MODE_CBC, iv)
self.assertEqual(cipher.decrypt(ciphertext), plaintext)
示例3: test_aes_192
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def test_aes_192(self):
key = '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'
iv = '000102030405060708090a0b0c0d0e0f'
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
'ae2d8a571e03ac9c9eb76fac45af8e51' +\
'30c81c46a35ce411e5fbc1191a0a52ef' +\
'f69f2445df4f9b17ad2b417be66c3710'
ciphertext = '4f021db243bc633d7178183a9fa071e8' +\
'b4d9ada9ad7dedf4e5e738763f69145a' +\
'571b242012fb7ae07fa9baac3df102e0' +\
'08b0e27988598881d920a9e64f5615cd'
key = unhexlify(key)
iv = unhexlify(iv)
plaintext = unhexlify(plaintext)
ciphertext = unhexlify(ciphertext)
cipher = AES.new(key, AES.MODE_CBC, iv)
self.assertEqual(cipher.encrypt(plaintext), ciphertext)
cipher = AES.new(key, AES.MODE_CBC, iv)
self.assertEqual(cipher.decrypt(ciphertext), plaintext)
示例4: test_aes_256
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def test_aes_256(self):
key = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'
iv = '000102030405060708090a0b0c0d0e0f'
plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
'ae2d8a571e03ac9c9eb76fac45af8e51' +\
'30c81c46a35ce411e5fbc1191a0a52ef' +\
'f69f2445df4f9b17ad2b417be66c3710'
ciphertext = 'f58c4c04d6e5f1ba779eabfb5f7bfbd6' +\
'9cfc4e967edb808d679f777bc6702c7d' +\
'39f23369a9d9bacfa530e26304231461' +\
'b2eb05e2c39be9fcda6c19078c6a9d1b'
key = unhexlify(key)
iv = unhexlify(iv)
plaintext = unhexlify(plaintext)
ciphertext = unhexlify(ciphertext)
cipher = AES.new(key, AES.MODE_CBC, iv)
self.assertEqual(cipher.encrypt(plaintext), ciphertext)
cipher = AES.new(key, AES.MODE_CBC, iv)
self.assertEqual(cipher.decrypt(ciphertext), plaintext)
示例5: decrypt3DES
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def decrypt3DES(self, globalSalt, masterPassword, entrySalt, encryptedData):
hp = sha1(globalSalt + masterPassword).digest()
pes = entrySalt + '\x00' * (20 - len(entrySalt))
chp = sha1(hp + entrySalt).digest()
k1 = hmac.new(chp, pes + entrySalt, sha1).digest()
tk = hmac.new(chp, pes, sha1).digest()
k2 = hmac.new(chp, tk + entrySalt, sha1).digest()
k = k1 + k2
iv = k[-8:]
key = k[:24]
return DES3.new(key, DES3.MODE_CBC, iv).decrypt(encryptedData)
示例6: init_crypto_nt6
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def init_crypto_nt6(self):
self.iv = self.get_constant_object(
'InitializationVector', 'String', length=16, term=None).v()
aes_handle = self.get_constant_object(
'hAesKey', target='Pointer',
target_args=dict(target='_KIWI_BCRYPT_HANDLE_KEY'))
self.aes_key = aes_handle.key.hardkey.data.v()
des_handle = self.get_constant_object(
'h3DesKey', target='Pointer',
target_args=dict(target='_KIWI_BCRYPT_HANDLE_KEY'))
self.des_key = des_handle.key.hardkey.data.v()
try:
cipher = AES.new(self.aes_key, AES.MODE_CFB, self.iv)
cipher = DES3.new(self.des_key, DES3.MODE_CBC, self.iv[:8])
cipher = None
decryption_enabled = True
except ValueError as e_ve:
decryption_enabled = False
logging.warning('init_crypto_nt6 exception {}'.format(e_ve))
finally:
return decryption_enabled
示例7: decrypt_nt6
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def decrypt_nt6(self, encrypted):
if not self.decryption_enabled:
return obj.NoneObject()
cipher = None
if self.iv:
if len(encrypted) % 8:
cipher = AES.new(self.aes_key, AES.MODE_CFB, self.iv)
else:
if self.des_key:
cipher = DES3.new(self.des_key, DES3.MODE_CBC, self.iv[:8])
if cipher and encrypted:
return cipher.decrypt(encrypted)
return obj.NoneObject()
示例8: decrypt3DES
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def decrypt3DES(self, globalSalt, master_password, entrySalt, encryptedData):
"""
User master key is also encrypted (if provided, the master_password could be used to encrypt it)
"""
# See http://www.drh-consultancy.demon.co.uk/key3.html
hp = sha1(globalSalt + master_password).digest()
pes = entrySalt + '\x00' * (20 - len(entrySalt))
chp = sha1(hp + entrySalt).digest()
k1 = hmac.new(chp, pes + entrySalt, sha1).digest()
tk = hmac.new(chp, pes, sha1).digest()
k2 = hmac.new(chp, tk + entrySalt, sha1).digest()
k = k1 + k2
iv = k[-8:]
key = k[:24]
return DES3.new(key, DES3.MODE_CBC, iv).decrypt(encryptedData)
示例9: decrypt
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def decrypt(self, key, iv, ciphertext):
"""
Decrypt ciphered data (user / password) using the key previously found
"""
data = DES3.new(key, DES3.MODE_CBC, iv).decrypt(ciphertext)
return self.remove_padding(data)
示例10: encryptData
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def encryptData(self, encryptKey, privParameters, dataToEncrypt):
if DES3 is None:
raise error.StatusInformation(
errorIndication=errind.encryptionError
)
snmpEngineBoots, snmpEngineTime, salt = privParameters
des3Key, salt, iv = self.__getEncryptionKey(
encryptKey, snmpEngineBoots
)
des3Obj = DES3.new(des3Key, DES3.MODE_CBC, iv)
privParameters = univ.OctetString(salt)
plaintext = dataToEncrypt + univ.OctetString((0,) * (8 - len(dataToEncrypt) % 8)).asOctets()
cipherblock = iv
ciphertext = null
while plaintext:
cipherblock = des3Obj.encrypt(
univ.OctetString(map(lambda x,y:x^y, univ.OctetString(cipherblock).asNumbers(), univ.OctetString(plaintext[:8]).asNumbers())).asOctets()
)
ciphertext = ciphertext + cipherblock
plaintext = plaintext[8:]
return univ.OctetString(ciphertext), privParameters
# 5.1.1.3
示例11: decryptData
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def decryptData(self, decryptKey, privParameters, encryptedData):
if DES3 is None:
raise error.StatusInformation(
errorIndication=errind.decryptionError
)
snmpEngineBoots, snmpEngineTime, salt = privParameters
if len(salt) != 8:
raise error.StatusInformation(
errorIndication=errind.decryptionError
)
des3Key, iv = self.__getDecryptionKey(decryptKey, salt)
if len(encryptedData) % 8 != 0:
raise error.StatusInformation(
errorIndication=errind.decryptionError
)
des3Obj = DES3.new(des3Key, DES3.MODE_CBC, iv)
plaintext = null
ciphertext = encryptedData.asOctets()
cipherblock = iv
while ciphertext:
plaintext = plaintext + univ.OctetString(map(lambda x,y: x^y, univ.OctetString(cipherblock).asNumbers(), univ.OctetString(des3Obj.decrypt(ciphertext[:8])).asNumbers())).asOctets()
cipherblock = ciphertext[:8]
ciphertext = ciphertext[8:]
return plaintext
示例12: setup_cipher
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def setup_cipher(self):
if self.mode == cipherMODE.ECB:
self._cipher = _pyCryptoDES3.new(self.key, _pyCryptoDES3.MODE_ECB)
elif self.mode == cipherMODE.CBC:
self._cipher = _pyCryptoDES3.new(self.key, _pyCryptoDES3.MODE_CBC, self.IV)
else:
raise Exception('Unknown cipher mode!')
示例13: _make_des3_decryptor
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def _make_des3_decryptor(key, iv):
if debug:
print("Before getting decryptor")
decryptor = DES3.new(key, DES3.MODE_CBC, iv)
if debug:
print("Decryptor returned")
return decryptor
示例14: makePrivateKeyString_openssh
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def makePrivateKeyString_openssh(obj, passphrase):
keyType = objectType(obj)
if keyType == 'ssh-rsa':
keyData = '-----BEGIN RSA PRIVATE KEY-----\n'
p,q=obj.p,obj.q
if p > q:
(p,q) = (q,p)
# p is less than q
objData = [0, obj.n, obj.e, obj.d, q, p, obj.d%(q-1), obj.d%(p-1),Util.number.inverse(p, q)]
elif keyType == 'ssh-dss':
keyData = '-----BEGIN DSA PRIVATE KEY-----\n'
objData = [0, obj.p, obj.q, obj.g, obj.y, obj.x]
else:
raise BadKeyError('unknown key type %s' % keyType)
if passphrase:
iv = common.entropy.get_bytes(8)
hexiv = ''.join(['%02X' % ord(x) for x in iv])
keyData += 'Proc-Type: 4,ENCRYPTED\n'
keyData += 'DEK-Info: DES-EDE3-CBC,%s\n\n' % hexiv
ba = md5.new(passphrase + iv).digest()
bb = md5.new(ba + passphrase + iv).digest()
encKey = (ba + bb)[:24]
asn1Data = asn1.pack([objData])
if passphrase:
padLen = 8 - (len(asn1Data) % 8)
asn1Data += (chr(padLen) * padLen)
asn1Data = DES3.new(encKey, DES3.MODE_CBC, iv).encrypt(asn1Data)
b64Data = base64.encodestring(asn1Data).replace('\n','')
b64Data = '\n'.join([b64Data[i:i+64] for i in range(0,len(b64Data),64)])
keyData += b64Data + '\n'
if keyType == 'ssh-rsa':
keyData += '-----END RSA PRIVATE KEY-----'
elif keyType == 'ssh-dss':
keyData += '-----END DSA PRIVATE KEY-----'
return keyData
示例15: show_remmina_accounts
# 需要导入模块: from Crypto.Cipher import DES3 [as 别名]
# 或者: from Crypto.Cipher.DES3 import MODE_CBC [as 别名]
def show_remmina_accounts(debug=False):
diz = {}
res = []
fs = open(REMMINA_FOLDER+REMMINA_PREF)
fso = fs.readlines()
fs.close()
for i in fso:
if re.findall(r'secret=', i):
r_secret = i[len(r'secret='):][:-1]
if debug: print('**secret found {}'.format(r_secret))
for f in os.listdir(REMMINA_FOLDER):
if re.findall(REGEXP_ACCOUNTS, f):
fo = open( REMMINA_FOLDER+f, 'r')
for i in fo.readlines():
if re.findall(r'^password=', i):
r_password = i[len(r'password='):][:-1]
if re.findall(r'^name=', i):
r_name = i.split('=')[1][:-1]
if re.findall(r'username=', i):
r_username = i.split('=')[1][:-1]
if debug: print(fo, 'found', f)
password = base64.b64decode(r_password)
secret = base64.b64decode(r_secret)
diz[r_name] = DES3.new(secret[:24], DES3.MODE_CBC, secret[24:]).decrypt(password)
if sys.version_info.major == 3:
pval = diz[r_name].decode(CHARSET)
else:
pval = diz[r_name]
r = (r_name, r_username, pval, diz[r_name])
res.append(r)
print('{} {} {} [raw:{}]'.format(*r))
fo.close()
return res