Aes256-cbc encoding/decrypting error

jayboy

I have been using gibberish.aes javascript code to encrypt some strings but when using a password such as £ or á, openssl command line does not want to decrypt it and spits out randomness but it works fine in the javascript itself, does anybody know whats going on here?

Example:

encrypted file: U2FsdGVkX18EWZNx70TPi0dYuiQG+7Zpg5RiGa2/mQsWU4A6JhWMwt3+mP1y6+xIQYN45t65oB+VntZfEd6EArB0X4nPmCJ18jBfO57a1jE=

password: password£

user1686

There are multiple ways to represent such characters as bytes – e.g. the letter "š" becomes {c5 a1} when encoded using UTF-8 (which JavaScript uses), but it could also be {f0} in ISO-8859-13 or Windows-1257, or {61 01} in UTF-16LE.

So you need to make sure you're always using the same text encoding for passphrases (ideally UTF-8). How to do it depends on the programming language as well as the encryption library. Some APIs require the passphrase to be supplied as a byte-array for exactly this reason – to force the developer to select a specific encoding.

When specifying passphrases with accented characters directly inside source code files (.py, etc), they're encoded into bytes by your text editor – make sure you know what encoding it uses, and try to use UTF-8 whenever possible. If that's not possible, write the accented characters using \x or \u escapes instead. For example (Py2/Py3):

passphrase = u"password£".encode("utf-8")

passphrase = u"password\u00A3".encode("utf-8")

passphrase = b"password\xC2\xA3"        # byte array – already encoded

In some languages, the compiler/interpreter will again decode the source file, so make sure it knows what encoding was used by your editor (e.g. in Python add a # encoding: utf-8 line at the top).

When working directly on command line, the encoding from keypresses to bytes is done by your terminal app, so make sure it is in UTF-8 mode. The command-line shell (bash) should also have $LANG telling it to use UTF-8. (All programs running inside the terminal already receive series of bytes; they have no control over the encoding that the terminal used.)

If in doubt, try sending the passphrase to a "hexdump" tool like hd or xxd:

  • Good (UTF-8):

    $ echo -n password£á | hexdump -C
    00000000  70 61 73 73 77 6f 72 64 c2 a3 c3 a1              |password....|
    
  • Bad (ISO-8859-1):

    $ echo -n password£á | hexdump -C
    00000000  70 61 73 73 77 6f 72 64 a3 e1                    |password..|
    

I tested your input using:

echo "U2FsdGVkX18EWZNx70TPi0dYuiQG+7Zpg5RiGa2/mQsWU4A6JhWMwt3+mP1y6+xIQYN45t65oB+VntZfEd6EArB0X4nPmCJ18jBfO57a1jE=" \
  | base64 -d \
  | openssl enc -aes-256-cbc -d -md md5 -k "password£"

As well as:

#!/usr/bin/env python3

from base64 import b64decode
from Crypto.Hash import MD5
from Crypto.Cipher import AES

def OpenSSL_parse_enc_header(data):
    if data[0:8] != b"Salted__":
        raise ValueError("missing OpenSSL header")
    salt = data[8:16]
    data = data[16:]
    return salt, data

def OpenSSL_EVP_BytesToKey(passphrase, salt, key_size, iv_size):
    buf = b""
    hash = b""
    while len(buf) < key_size + iv_size:
        hash = MD5.new(hash + passphrase + salt).digest()
        buf += hash
    key = buf[0:key_size]
    iv = buf[key_size:key_size+iv_size]
    return key, iv

def PKCS7_remove_padding(data, block_size):
    if len(data) % block_size != 0:
        raise ValueError("data is not padded")
    pad_len = data[-1]
    if pad_len < 1 or pad_len > block_size:
        raise ValueError("PKCS#7 padding incorrect")
    if data[-pad_len:] != bytes([pad_len] * pad_len):
        raise ValueError("PKCS#7 padding incorrect")
    return data[:-pad_len]

enc_data = b64decode("U2FsdGVkX18EWZNx70TPi0dYuiQG+7Zpg5RiGa2/mQsWU4"
                     "A6JhWMwt3+mP1y6+xIQYN45t65oB+VntZfEd6EArB0X4nP"
                     "mCJ18jBfO57a1jE=")
kdf_salt, enc_data = OpenSSL_parse_enc_header(enc_data)

passphrase = "password£".encode("utf-8")
key, iv = OpenSSL_EVP_BytesToKey(passphrase,
                                 kdf_salt,
                                 key_size=256//8,
                                 iv_size=AES.block_size)

plain_data = AES.new(key, AES.MODE_CBC, iv=iv).decrypt(enc_data)
plain_data = PKCS7_remove_padding(plain_data, AES.block_size)
print(plain_data)

In both cases it returns this text (with valid PKCS#7 padding, therefore successful decryption):

L3scoV8yhgA9tqbXBA2SXTczghGUSGTDsWkakCwgK6jk13TAUfXi

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

teamcity aes256-cbc error when retrieving git repository

From Dev

Java Encrypt a file using aes256/ CBC/PKCS7Padding

From Dev

Link error when using AES256 example with OpenSSL

From Dev

AES256 Encryption/Decryption Error+ IOS SDK 7

From Dev

AES256 Encryption/Decryption Error+ IOS SDK 7

From Dev

Unknown Error in AES CBC encryption pycrypto 2.6

From Dev

OpenSSL AES-256-CBC Encryption Error, "wrong final block length" Ruby

From Dev

Python AES CBC error with string size when using Block size of 256

From Dev

Error when decrypt input data longer than 15 chars with AES256 and Node.js

From Dev

Decrypting aes-256-cbc using bouncycastle

From Dev

Generate a SSH pair with AES-256-CBC

From Dev

Decrypting aes-256-cbc using bouncycastle

From Dev

AES 128 with CBC

From Dev

AES 128 with CBC

From Dev

What is the default IV when encrypting with aes_256_cbc cipher?

From Dev

OpenSSL AES 256 CBC via EVP api in C

From Java

Need help in interpreting the aes-256-cbc encyption with oaepHash

From Dev

How to encrypt plaintext with AES-256 CBC in PHP using OpenSSL?

From Dev

Team City The cipher 'aes256-cbc' is required, but it is not available

From Dev

How to decrypt an AES-256-CBC encrypted string

From Dev

Length of Encrypted Text using AES-256-CBC

From Dev

AES-256-CBC encryption in PHP and decryption in Node.js

From Dev

CryptoJS AES CBC 256 decrypt adds additional byte in the middle of plaintext

From Dev

aes-256-cbc encryption/decryption keys don't work

From Dev

Implement Php open_ssl_decrypt AES 256 CBC as CryptoJS

From Dev

How can I encrypt / decrypt AES-256 CBC with OpenSSL?

From Dev

What is the default IV when encrypting with aes_256_cbc cipher?

From Dev

AES 256 bits CBC PKCS#5 encrypt / decrypt in Python

From Dev

AES-256-CBC encrypted with PHP and decrypt in Java

Related Related

  1. 1

    teamcity aes256-cbc error when retrieving git repository

  2. 2

    Java Encrypt a file using aes256/ CBC/PKCS7Padding

  3. 3

    Link error when using AES256 example with OpenSSL

  4. 4

    AES256 Encryption/Decryption Error+ IOS SDK 7

  5. 5

    AES256 Encryption/Decryption Error+ IOS SDK 7

  6. 6

    Unknown Error in AES CBC encryption pycrypto 2.6

  7. 7

    OpenSSL AES-256-CBC Encryption Error, "wrong final block length" Ruby

  8. 8

    Python AES CBC error with string size when using Block size of 256

  9. 9

    Error when decrypt input data longer than 15 chars with AES256 and Node.js

  10. 10

    Decrypting aes-256-cbc using bouncycastle

  11. 11

    Generate a SSH pair with AES-256-CBC

  12. 12

    Decrypting aes-256-cbc using bouncycastle

  13. 13

    AES 128 with CBC

  14. 14

    AES 128 with CBC

  15. 15

    What is the default IV when encrypting with aes_256_cbc cipher?

  16. 16

    OpenSSL AES 256 CBC via EVP api in C

  17. 17

    Need help in interpreting the aes-256-cbc encyption with oaepHash

  18. 18

    How to encrypt plaintext with AES-256 CBC in PHP using OpenSSL?

  19. 19

    Team City The cipher 'aes256-cbc' is required, but it is not available

  20. 20

    How to decrypt an AES-256-CBC encrypted string

  21. 21

    Length of Encrypted Text using AES-256-CBC

  22. 22

    AES-256-CBC encryption in PHP and decryption in Node.js

  23. 23

    CryptoJS AES CBC 256 decrypt adds additional byte in the middle of plaintext

  24. 24

    aes-256-cbc encryption/decryption keys don't work

  25. 25

    Implement Php open_ssl_decrypt AES 256 CBC as CryptoJS

  26. 26

    How can I encrypt / decrypt AES-256 CBC with OpenSSL?

  27. 27

    What is the default IV when encrypting with aes_256_cbc cipher?

  28. 28

    AES 256 bits CBC PKCS#5 encrypt / decrypt in Python

  29. 29

    AES-256-CBC encrypted with PHP and decrypt in Java

HotTag

Archive