Ejemplo n.º 1
0
    def decrypt(self, ciphertext, iv, key, algo="aes-256-cbc"):
        cipher_type = self._get_algo_cipher_type(algo)

        if cipher_type == "cbc":
            cipher = pyaes.AESModeOfOperationCBC(key, iv=iv)
        elif cipher_type == "ctr":
            # The IV is actually a counter, not an IV but it does almost the
            # same. Notice: pyaes always uses 1 as initial counter! Make sure
            # not to call pyaes directly.

            # We kinda do two conversions here: from byte array to int here, and
            # from int to byte array in pyaes internals. It's possible to fix that
            # but I didn't notice any performance changes so I'm keeping clean code.
            iv_int = 0
            for byte in iv:
                iv_int = (iv_int * 256) + byte
            counter = pyaes.Counter(iv_int)
            cipher = pyaes.AESModeOfOperationCTR(key, counter=counter)
        elif cipher_type == "cfb":
            # Change segment size from default 8 bytes to 16 bytes for OpenSSL
            # compatibility
            cipher = pyaes.AESModeOfOperationCFB(key, iv, segment_size=16)
        elif cipher_type == "ofb":
            cipher = pyaes.AESModeOfOperationOFB(key, iv)

        decrypter = pyaes.Decrypter(cipher)
        data = decrypter.feed(ciphertext)
        data += decrypter.feed()
        return data
Ejemplo n.º 2
0
def secure_dumps(data, encryption_key, hash_key=None, compression_level=None):
    if not hash_key:
        hash_key = hashlib.sha1(encryption_key).hexdigest()
    dump = pickle.dumps(data)
    if compression_level:
        dump = zlib.compress(dump, compression_level)
    key = _pad(encryption_key[:32])
    aes = pyaes.AESModeOfOperationCFB(key, iv=key[:16], segment_size=8)
    encrypted_data = base64.urlsafe_b64encode(aes.encrypt(_pad(dump)))
    signature = hmac.new(hash_key, encrypted_data).hexdigest()
    return signature + ':' + encrypted_data
Ejemplo n.º 3
0
def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
    if not ':' in data:
        return None
    if not hash_key:
        hash_key = hashlib.sha1(encryption_key).hexdigest()
    signature, encrypted_data = data.split(':', 1)
    actual_signature = hmac.new(hash_key, encrypted_data).hexdigest()
    if signature != actual_signature:
        return None
    key = _pad(encryption_key[:32])
    aes = pyaes.AESModeOfOperationCFB(key, iv=key[:16], segment_size=8)
    try:
        data = aes.decrypt(base64.urlsafe_b64decode(encrypted_data))
        data = data.rstrip(' ')
        if compression_level:
            data = zlib.decompress(data)
        return pickle.loads(data)
    except (TypeError, pickle.UnpicklingError):
        return None
Ejemplo n.º 4
0
def de(c,key,iv):   
   aes = pyaes.AESModeOfOperationCFB(key, iv = iv, segment_size = 16)
   m = aes.decrypt(c)
   return zlib.decompress(m)
Ejemplo n.º 5
0
import base64
import hashlib

salt = base64.b64decode('ml2XwdbswuGmGLLfdd+8Zw==')

pub = 'MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEGUO0ZrWHa8kOSIjrrI8Rw8RhYXyM2adkeAyFLNenNEUK16CdJgMjyjxi+zG5hCXZJHmUqRqM4x927duzyzYER/Bdh9Uh9MbN9JX5BeL35IM5YV604nXVslQHXQ3YO1Fb'
sharedSecret = base64.b64decode(
    'MIGkAgEBBDCHVJkw18P0ExyI7gK/rGzYxNju+CuuqIVYahJBB54lH1iq7AxunA3C0a4jua+C0WygBwYFK4EEACKhZANiAAQZQ7RmtYdryQ5IiOusjxHDxGFhfIzZp2R4DIUs16c0RQrXoJ0mAyPKPGL7MbmEJdkkeZSpGozjH3bt27PLNgRH8F2H1SH0xs30lfkF4vfkgzlhXrTiddWyVAddDdg7UVs=')
hash = hashlib.sha256()
hash.update(salt)
hash.update(sharedSecret)


key_256 = hash.digest()
iv=hash.digest()[0:16]

print('公钥: {0}'.format(pub))
print('私钥: {0}'.format(base64.b64encode(key_256)))
print('盐值: {0}'.format(base64.b64encode(salt)))

plaintext = "Text may be any length you wish, no padding is required"
ciphertext = b'\xf8\x1ci\x88#\xd8\x19\x9a(#V\x15\x0f\x97\x12p\x90\xaa\x8e\xc0\x11\xe5z\x8f\x90;\xadrT}g8\x16\xcf\x93\x17\x00~b\xd9\xa7|D\x16\x19\xc7\x9b\xd034+\x8e\x0b\x88\x9f7\x84\x8d\xf7\x80\x11A\x95\x9a\xea\xbf\xfcw\x87\xbd\xea\xfd\xc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x87'

print(ciphertext)

aes = pyaes.AESModeOfOperationCFB(key_256, iv)
decrypted = aes.decrypt(ciphertext)
print(decrypted)

decrypted = aes.decrypt(ciphertext)
print(decrypted)
Ejemplo n.º 6
0
msg = b'b'  #binary
msg += b'\x08'  #filename len
msg += b'_CONSOLE'  #Special 'your eyes only'
msg += b'\x00' * 4  #No date
msg += sys.stdin.read().encode('ascii')

p += packet_len(msg)
p += msg

pre = random(16)  #We effectivly use an encryptied IV
pre += bytes([pre[-2]])
pre += bytes([pre[-2]])

aes = pyaes.AESModeOfOperationCFB(session_key,
                                  iv=b'\x00' * 16,
                                  segment_size=16)
buf = aes.encrypt(pre + b'\00' * 14)[:18]  #First BS+2 of random

aes = pyaes.AESModeOfOperationCFB(session_key, iv=buf[2:],
                                  segment_size=16)  #resync
buf += aes.encrypt(p + b'\00' * (16 - len(p) % 16))

out += packet_len(p)
out += buf[:len(p) + 18]

print("-----BEGIN PGP MESSAGE-----")
print("Version: " + sys.argv[0] + " 0.0.1")
print()
print(base64.b64encode(out).decode('ascii') + "=")
print()
Ejemplo n.º 7
0
 def __init__(self, key, iv):
     super(AESCipher, self).__init__(key, iv)
     self.aes_ctx = pyaes.AESModeOfOperationCFB(key, iv)