本文整理汇总了Python中Crypto.Util.strxor.strxor方法的典型用法代码示例。如果您正苦于以下问题:Python strxor.strxor方法的具体用法?Python strxor.strxor怎么用?Python strxor.strxor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Util.strxor
的用法示例。
在下文中一共展示了strxor.strxor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gcm_decrypt
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def gcm_decrypt(k, iv, encrypted, auth_data, tag):
aes = AES.new(k, AES.MODE_GCM)
h = aes.encrypt(chr(0) * aes.block_size)
if len(iv) == 12:
y0 = iv + "\x00\x00\x00\x01"
else:
y0 = ghash(h, '', iv)
decrypted = gctr(k, y0, encrypted)
s = ghash(h, auth_data, encrypted)
t = aes.encrypt(y0)
T = strxor.strxor(s, t)
if T != tag:
return '' # decrypted data is invalid
else:
return decrypted
示例2: encrypt
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def encrypt(self, plaintext):
"""CBC encryption."""
cipher = AES.new(key=self._key, mode=AES.MODE_ECB)
# The full URL is not necessary for this setup, so I am just encrypting
# the plaintext as it is. I don't even need to support padding.
prev_ct = self._iv
block_index = 0
ciphertext = b''
# The loop simulates encryption through AES in CBC mode.
while block_index < len(plaintext):
block = plaintext[block_index : block_index + AES.block_size]
final_block = strxor(block, prev_ct)
cipher_block = cipher.encrypt(final_block)
prev_ct = cipher_block
ciphertext += cipher_block
block_index += AES.block_size
return ciphertext
示例3: decrypt
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def decrypt(self, ciphertext):
"""CBC decryption."""
cipher = AES.new(key=self._key, mode=AES.MODE_ECB)
prev_ct = self._iv
block_index = 0
plaintext = b''
# The loop simulates decryption through AES in CBC mode.
while block_index < len(ciphertext):
block = ciphertext[block_index : block_index + AES.block_size]
prep_plaintext = cipher.decrypt(block)
plaintext += strxor(prev_ct, prep_plaintext)
prev_ct = block
block_index += AES.block_size
# Here we should check if this is all readable ASCII, and raise an
# exception if it's not. However that part is not really necessary,
# and converting from Exception object to byte string (instead of a
# usual string) does not look great so let's be lazy :)
return plaintext
示例4: _update
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def _update(self, data_block):
"""Update a block aligned to the block boundary"""
bs = self._block_size
assert len(data_block) % bs == 0
if len(data_block) == 0:
return
ct = self._cbc.encrypt(data_block)
if len(data_block) == bs:
second_last = self._last_ct
else:
second_last = ct[-bs*2:-bs]
self._last_ct = ct[-bs:]
self._last_pt = strxor(second_last, data_block[-bs:])
示例5: update
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def update(self, item):
"""Pass the next component of the vector.
The maximum number of components you can pass is equal to the block
length of the cipher (in bits) minus 1.
:Parameters:
item : byte string
The next component of the vector.
:Raise TypeError: when the limit on the number of components has been reached.
"""
if self._n_updates == 0:
raise TypeError("Too many components passed to S2V")
self._n_updates -= 1
mac = CMAC.new(self._key,
msg=self._last_string,
ciphermod=self._ciphermod,
cipher_params=self._cipher_params)
self._cache = strxor(self._double(self._cache), mac.digest())
self._last_string = _copy_bytes(None, None, item)
示例6: derive
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def derive(self):
""""Derive a secret from the vector of components.
:Return: a byte string, as long as the block length of the cipher.
"""
if len(self._last_string) >= 16:
# xorend
final = self._last_string[:-16] + strxor(self._last_string[-16:], self._cache)
else:
# zero-pad & xor
padded = (self._last_string + b'\x80' + b'\x00' * 15)[:16]
final = strxor(padded, self._double(self._cache))
mac = CMAC.new(self._key,
msg=final,
ciphermod=self._ciphermod,
cipher_params=self._cipher_params)
return mac.digest()
示例7: test_output_memoryview
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def test_output_memoryview(self):
"""Verify result can be stored in pre-allocated memory"""
term1 = unhexlify(b"ff339a83e5cd4cdf5649")
term2 = unhexlify(b"383d4ba020573314395b")
original_term1 = term1[:]
original_term2 = term2[:]
expected_xor = unhexlify(b"c70ed123c59a7fcb6f12")
output = memoryview(bytearray(len(term1)))
result = strxor(term1, term2, output=output)
self.assertEqual(result, None)
self.assertEqual(output, expected_xor)
self.assertEqual(term1, original_term1)
self.assertEqual(term2, original_term2)
示例8: digest
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def digest(self):
"""Compute the *binary* MAC tag.
The caller invokes this function at the very end.
This method returns the MAC that shall be sent to the receiver,
together with the ciphertext.
:Return: the MAC, as a byte string.
"""
if self.digest not in self._next:
raise TypeError("digest() cannot be called when decrypting"
" or validating a message")
self._next = [self.digest]
if not self._mac_tag:
tag = b'\x00' * self.block_size
for i in range(3):
tag = strxor(tag, self._omac[i].digest())
self._mac_tag = tag[:self._mac_len]
return self._mac_tag
示例9: pbkdf2_bin
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def pbkdf2_bin(data, salt, iterations=1000, keylen=24, hashfunc=None):
"""Returns a binary digest for the PBKDF2 hash algorithm of `data`
with the given `salt`. It iterates `iterations` time and produces a
key of `keylen` bytes. By default SHA-1 is used as hash function,
a different hashlib `hashfunc` can be provided.
"""
hashfunc = hashfunc or sha1
mac = hmac.new(data, None, hashfunc)
def _pseudorandom(x, mac=mac):
h = mac.copy()
h.update(x)
return h.digest()
buf = deque()
for block in xrange(1, -(-keylen // mac.digest_size) + 1):
rv = u = _pseudorandom(salt + _pack_int(block))
for i in xrange(iterations - 1):
u = _pseudorandom(u)
rv = strxor(rv, u)
buf.extend(rv)
return ''.join(buf)[:keylen]
示例10: _digest
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def _digest(self, last_data):
if len(last_data)==self._bs:
last_block = strxor(last_data, self._k1)
else:
last_block = strxor(last_data+bchr(128)+
bchr(0)*(self._bs-1-len(last_data)), self._k2)
tag = self._mac.encrypt(last_block)
return tag
示例11: PBKDF2
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def PBKDF2(password, salt, dkLen=16, count=1000, prf=None):
"""Derive one or more keys from a password (or passphrase).
This performs key derivation according to the PKCS#5 standard (v2.0),
by means of the ``PBKDF2`` algorithm.
:Parameters:
password : string
The secret password or pass phrase to generate the key from.
salt : string
A string to use for better protection from dictionary attacks.
This value does not need to be kept secret, but it should be randomly
chosen for each derivation. It is recommended to be at least 8 bytes long.
dkLen : integer
The cumulative length of the desired keys. Default is 16 bytes, suitable for instance for `Crypto.Cipher.AES`.
count : integer
The number of iterations to carry out. It's recommended to use at least 1000.
prf : callable
A pseudorandom function. It must be a function that returns a pseudorandom string
from two parameters: a secret and a salt. If not specified, HMAC-SHA1 is used.
:Return: A byte string of length `dkLen` that can be used as key material.
If you wanted multiple keys, just break up this string into segments of the desired length.
"""
password = tobytes(password)
if prf is None:
prf = lambda p,s: HMAC.new(p,s,SHA1).digest()
key = b('')
i = 1
while len(key)<dkLen:
U = previousU = prf(password,salt+struct.pack(">I", i))
for j in xrange(count-1):
previousU = t = prf(password,previousU)
U = strxor(U,t)
key += U
i = i + 1
return key[:dkLen]
示例12: sign
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def sign(key, message):
try:
ECB = AES.new(key, AES.MODE_ECB)
messageblocks = [message[i:i + 16] for i in range(0, len(message), 16)]
tag = ECB.encrypt(messageblocks[0])
for i in range(1,len(messageblocks)):
tag = ECB.encrypt(strxor(messageblocks[i], tag))
return hexlify(tag)
except:
print("\nYou can't sign that way! No padding done here boy!")
exit()
示例13: gctr
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def gctr(k, icb, plaintext):
y = ''
if len(plaintext) == 0:
return y
aes = AES.new(k)
cb = icb
for i in range(0, len(plaintext), aes.block_size):
cb = inc32(cb)
encrypted = aes.encrypt(cb)
plaintext_block = plaintext[i:i+aes.block_size]
y += strxor.strxor(plaintext_block, encrypted[:len(plaintext_block)])
return y
示例14: gcm_encrypt
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def gcm_encrypt(k, iv, plaintext, auth_data):
aes = AES.new(k, AES.MODE_GCM)
h = aes.encrypt(chr(0) * aes.block_size)
if len(iv) == 12:
y0 = iv + "\x00\x00\x00\x01"
else:
y0 = ghash(h, '', iv)
encrypted = gctr(k, y0, plaintext)
s = ghash(h, auth_data, encrypted)
t = aes.encrypt(y0)
T = strxor.strxor(s, t)
return (encrypted, T)
示例15: decrypt_data
# 需要导入模块: from Crypto.Util import strxor [as 别名]
# 或者: from Crypto.Util.strxor import strxor [as 别名]
def decrypt_data(ciphertext):
cipher = "JFK63wT1zksfFnACSd93c5WzN5PURZNH"
print '[!] Cipher:' + cipher
plaintext = strxor(b64decode(ciphertext), cipher)
return plaintext