0

I need to decrypt an FTP password encrypted by pycrypto in AES CTR mode ,and for some reason I have to use cryptography instead,so how to do the decryption?

I want to know what is the nonce of cryptography should be?

this is the encrypt code by pycrypto

from Crypto.Util import Counter
from Crypto.Cipher import AES

def encrypt(text):
    ctr = Counter.new(128, initial_value=1)
    encrypto = AES.new(key, AES.MODE_CTR, counter=ctr)
    encrypted = encrypto.encrypt(text)
    return base64.b64encode(encrypted)

and the decrypt code by cryptography

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

def decrypt(text)
    cipher = Cipher(algorithms.AES(key), None, backend=default_backend())
    nonce = ?
    cipher.mode = modes.CTR(nonce)
    decryptor = cipher.decryptor()
    decrypted = decryptor.update(encrypted.decode("base64"))
    return decrypted
Ragnarok
  • 1
  • 1
  • Because they are in different systems, the system decryption belongs to judges pycrypto having some problem in business, so we have to change to another one – Ragnarok Sep 20 '19 at 09:11
  • CTR mode uses a nonce, in general, the nonce is prepended to the ciphertext, and, during the decryption, it is resolved. – kelalaka Sep 20 '19 at 09:13
  • 1
    For encryption (PyCrypto-code) always the _same_ nonce (=`00000000000000000000000000000001`) is taken. Thus, for decryption (Cryptography-code), `nonce = bytes.fromhex('00000000000000000000000000000001')` applies. Note however, the use of a constant nonce is rather unusual and if not a _new_ key is taken for _every_ message, [security](https://crypto.stackexchange.com/questions/2991/why-must-iv-key-pairs-not-be-reused-in-ctr-mode) will be _lost_ for AES-CTR! – Topaco Sep 20 '19 at 21:07

0 Answers0