Home python How to decrypt AES SHA-256 CBC?

How to decrypt AES SHA-256 CBC?

Author

Date

Category

There is a string that is encrypted with this algorithm.

b '{"Merchantid": "xxxxxxxxxx", "tokenid": "yyyyyyyyyyyyyyyyy"}'

at the exit I see a payload
25BE197FB04DAC542243AC124C3799031007417C2E214C833318C469D7D328DB889B36CDAACBB67D6F528911F3B31F7E3E4D44E3016A5A29D179998665FB4B590F42F4FEFA6771B5302AE1C067D9ACE0F8477D11BA82217B9A9F0E4941545161

In debug mode, I see the trail. settings

Trying the Decrypt method to decline the line – gives

from base64 import b16encode, b16decode
from os import urandom
From Cryptography Import X509
from cryptography.hazmat.Backends import Default_Backend
from cryptography.hazmat.primitives.hashes Import Sha1
from cryptography.hazmat.primitives.ciphers Import Cipher, Algorithms, Modes
From Gateway.Exceptions Import ConfigurationError
Class Crypt:
  "" "AES-256-CBC CRYPTO Class" ""
  IV_LENGTH = 16.
  token = ""
  CERT = ""
  Def __init __ (Self, Conf_Token, Conf_Cert):
    DEF Get_repr_fp (CERT):
      Fingerprint = X509.Load_pem_x509_Certificate (
          CERT, DEFAULT_BACKEND ()). FingerPrint (SHA1 ())
      FingerPrint = B16ENCODE (FingerPrint) .Decode ()
      RETURN ":". JOIN (STR (Fingerprint [i: I + 2]). Upper () for i in Range (
        0, len (fingerprint), 2))
    DEF GEN_KEY (FP):
      token = conf_token.
      Return (".join (
          CHR (ORD (X) | ORD (Y))
          For x, y in zip (FP, Token)) [: len (token)]). Encode ()
    Try:
      With Open (Conf_Cert, 'Rb') AS F:
        Pub = F.Read ()
        F.Close ()
      fingerprint = get_repr_fp (pub)
      EncryptKey = Gen_Key (FingerPrint)
    Except Typeerror AS E:
      Raise ConfigurationError () from E
    Aes = Algorithms.aes (EncryptKey)
    self.block_size = aes.block_size
    Self.Key_Length = int (AES.KEY_SIZE / 8)
    Self .__ DecryptKey = Fingerprint [: Self.key_LengTh] .encode ()
    Self .__ EncryptKey = EncryptKey
  Def __cipher (Self, Key, IV):
    AES = Algorithms.aes (Key)
    Return Cipher (AES, MODES.CBC (IV), Backend = Default_Backend ())
  Def Encryptor (Self, IV):
    "" "GET ENCRYPTOR CONTEXT" ""
    RETURN SELF .__ CIPHER (Self .__ EncryptKey, IV) .Nencryptor ()
  Def Decryptor (Self, IV):
    Return Self .__ Cipher (Self .__ DecryptKey, IV) .decryptor ()
  Def __Pad_Data (Self, Data):
    "" "Add Null Padding to Data" ""
    block_bytes = int (self.block_size / 8)
    Data = Data + Bytes (Block_bytes - Len (Data)% block_bytes))
    Return Data.
  Def __unpad_data (Self, Data):
    "" "REMOVE PADDING FROM DATA" ""
    Return Data.Decode (). Strip ()
  Def Encrypt (Self, Data):
    "" "ENCRYPT CONTENT" ""
    IV = urandom (Self.IV_LENGTH)
    Encryptor = Self.encryptor (IV)
    RETURN B16ENCODE (IV + (Self .__ Pad_Data (
      DATA)) + encryptor.finalize ()))
  Def Decrypt (Self, Data):
    "" "Decrypt Content" ""
    Data = B16Decode (data.upper ())
    decryptor = self.decryptor (Data [: Self.IV_LENGTH])
    RETURN SELF .__ UNPAD_DATA (
      decryptor.update (Data [Self.IV_LENGTH:]) + decryptor.finalize ())
C = Crypt (conf_token = "xxxxxxxxxxxx +", conf_cert = "yyyy.pem")
Rawtext = "ZZZZ"
a = rawtext.encode ()
z = c.encrypt (a)
Print ("Encrypted -:", z)
f = c.decrypt (z)
Print (F)

Answer 1, Authority 100%

AES is a symmetrical cipher. This means that the data is encrypted and decrypted the same key

In addition, the “closed key” you form as some kind of incomprehensible string with colon.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions