Ruby OpenSSL AES-128-CTR

Daniel

I can't figure out what I am doing wrong here trying to decrypt a string of hex values with a given key using ruby's OpenSSL cipher AES-128-CTR.

I am using the gem hex_string to convert my hex to bytes

ctrkey = "36f18357be4dbd77f050515c73fcf9f2"
ciphertext3 = "69dda8455c7dd4254bf353b773304eec0ec7702330098ce7f7520d1cbbb20fc3\
88d1b0adb5054dbd7370849dbf0b88d393f252e764f1f5f7ad97ef79d59ce29f5f51eeca32eabedd9afa9329"

cipher2 = OpenSSL::Cipher.new('AES-128-CTR')
cipher2.decrypt

ctrkey = ctrkey.to_byte_string
cipher2.key = ctrkey

iv = cipher2.random_iv
cipher2.iv = iv

ciphertext3 = ciphertext3.to_byte_string
plain = cipher2.update(ciphertext3) + cipher2.final

puts "plaintext of Q3: #{plain}"

I know I am missing something small because I have similar code implementing AES-128-CBC. Do I need to have a counter that increments the IV for each block of 128 bytes in the ciphertext?

Maarten Bodewes

No, you're not missing something small, you are missing something huge.

Instead of using the same IV as used for encryption, you are generating a new one. For CTR, if the IV is random then each counter value is different, resulting in random looking output.

Often the IV (or nonce in the case of CTR) is prefixed to the ciphertext. For CTR that may be fewer bytes than 16 - although that is still the most probable size to try.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AES_128_CTR encryption by openssl and PyCrypto

From Dev

Parameter details of OpenSSL's AES_ctr128_encrypt()

From Dev

Unexpected output of OpenSSL AES_ctr128_encrypt() as pseudo-random function for key derivation in RFC3711 (SRTP)

From Dev

Intel® TinyCrypt: how to use AES-128/CTR

From Dev

AES 128 CTR Mode Bit Shifting to Create Counter

From Dev

AES 256 CTR Encryption/Decryption using Visual Studio and Openssl

From Dev

AES 256 CTR Encryption/Decryption using Visual Studio and Openssl

From Dev

Java and openssl C different AES CTR encryption result

From Dev

Java AES 128 encrypting differently to openssl

From Dev

Test Vectors for AES-128-ECB and OpenSSL

From Dev

AES_ctr128_encrypt on Solaris doesnt decrypt same as other OS's

From Dev

Encrypt and decrypt a random NSString with AES128 CTR in iOS with a given key

From Dev

OpenSSL AES_cfb128_encrypt C++

From Dev

OpenSSL EVP_aes_128_gcm with BIO API

From Dev

sync AES ecnryption between cryptoJS and openSSL (ruby)

From Dev

AES CTR encryption and decryption

From Dev

AES CTR implementation

From Dev

Python AES CTR to PHP

From Dev

AES (aes-ige-128, aes-ige-192, aes-ige-256) encryption/decryption with openssl C

From Dev

How to decode a string encoded with openssl aes-128-cbc using java?

From Dev

How to generate a PEM file with Openssl using ECDHE-RSA-AES128-GCM-SHA256 cipher

From Dev

Decrypt aes-128-gcm encoded content with JAVA Cipher using PHP OpenSSL

From Dev

openssl command encrypt data does not match EVP_aes_128_cbc C code

From Dev

TPLB 3 OpenSSL Decrypt AES-256-CBC Encrypted with Ruby 2.0.0 OpenSSL::Cipher

From Dev

AES 128 decryption fails

From Dev

AES 128 with CBC

From Dev

AES 128 in Python

From Dev

AES 128 with CBC

From Dev

Determinisric AES-CTR in Java BouncyCastle?

Related Related

  1. 1

    AES_128_CTR encryption by openssl and PyCrypto

  2. 2

    Parameter details of OpenSSL's AES_ctr128_encrypt()

  3. 3

    Unexpected output of OpenSSL AES_ctr128_encrypt() as pseudo-random function for key derivation in RFC3711 (SRTP)

  4. 4

    Intel® TinyCrypt: how to use AES-128/CTR

  5. 5

    AES 128 CTR Mode Bit Shifting to Create Counter

  6. 6

    AES 256 CTR Encryption/Decryption using Visual Studio and Openssl

  7. 7

    AES 256 CTR Encryption/Decryption using Visual Studio and Openssl

  8. 8

    Java and openssl C different AES CTR encryption result

  9. 9

    Java AES 128 encrypting differently to openssl

  10. 10

    Test Vectors for AES-128-ECB and OpenSSL

  11. 11

    AES_ctr128_encrypt on Solaris doesnt decrypt same as other OS's

  12. 12

    Encrypt and decrypt a random NSString with AES128 CTR in iOS with a given key

  13. 13

    OpenSSL AES_cfb128_encrypt C++

  14. 14

    OpenSSL EVP_aes_128_gcm with BIO API

  15. 15

    sync AES ecnryption between cryptoJS and openSSL (ruby)

  16. 16

    AES CTR encryption and decryption

  17. 17

    AES CTR implementation

  18. 18

    Python AES CTR to PHP

  19. 19

    AES (aes-ige-128, aes-ige-192, aes-ige-256) encryption/decryption with openssl C

  20. 20

    How to decode a string encoded with openssl aes-128-cbc using java?

  21. 21

    How to generate a PEM file with Openssl using ECDHE-RSA-AES128-GCM-SHA256 cipher

  22. 22

    Decrypt aes-128-gcm encoded content with JAVA Cipher using PHP OpenSSL

  23. 23

    openssl command encrypt data does not match EVP_aes_128_cbc C code

  24. 24

    TPLB 3 OpenSSL Decrypt AES-256-CBC Encrypted with Ruby 2.0.0 OpenSSL::Cipher

  25. 25

    AES 128 decryption fails

  26. 26

    AES 128 with CBC

  27. 27

    AES 128 in Python

  28. 28

    AES 128 with CBC

  29. 29

    Determinisric AES-CTR in Java BouncyCastle?

HotTag

Archive