AES 256 CTR Encryption/Decryption using Visual Studio and Openssl

Tanatos Daniel

Good day, gentleman! I am trying to encrypt and decrypt a string using aes ctr with a 256-bit key. Below I posted the code. I'm missing something, but I can't figure out what. Checktext resulted from decryption is not the same as the plaintext. Thanks in advance!

struct ctr_state 
{ 
    unsigned char ivec[16]; 
    unsigned int num; 
    unsigned char ecount[16]; 
}; 

int init_ctr(struct ctr_state *state, const unsigned char iv[16])
{ 
    state->num = 0; 

    memset(state->ecount,0,16);
    memset(state->ivec + 8, 0, 8);   /* Copy IV into 'ivec' */ 
    memcpy(state->ivec, iv, 8); 

    return 0;
} 

struct ctr_state state;

void ctr_encrypt(const size_t encslength, AES_KEY key, int length) 
    {
        init_ctr(&state, iv);
        unsigned char my_data[16], output[16];

        AES_set_encrypt_key((unsigned char*)rkey, 256, &key);

        for (int i=1; i<encslength/16+1; i++)
        {
            memset(my_data,0,16);
            memcpy(my_data,plaintext+((i-1)*16),16);
            AES_ctr128_encrypt((unsigned char*)my_data, (unsigned char*)output, 16, &key, state.ivec, state.ecount, &state.num);
            memcpy(ciphertext+((i-1)*16),output,16);
        }

        hexdump(stdout, "ciphertext", (unsigned char*)ciphertext, length);
    }


    void ctr_decrypt(const size_t encslength, AES_KEY key, int length)
    {
        init_ctr(&state, iv);
        unsigned char my_data[16], output[16];

        AES_set_decrypt_key((unsigned char*)rkey, 256, &key);

        for (int i=1; i<encslength/16+1; i++)
        {
            memset(my_data,0,16);
            memcpy(my_data,ciphertext+((i-1)*16),16);
            AES_ctr128_encrypt((unsigned char*)my_data, (unsigned char*)output, 16, &key, state.ivec, state.ecount, &state.num);
            memcpy(checktext+((i-1)*16),output,16);
        }

        hexdump(stdout, "checktext", (unsigned char*)checktext, length);
    }
Chiara Hsieh

During decryption, replace

AES_set_decrypt_key((unsigned char*)rkey, 256, &key);

with

AES_set_encrypt_key((unsigned char*)rkey, 256, &key);

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 256 CTR Encryption/Decryption using Visual Studio and Openssl

From Dev

AES_128_CTR encryption by openssl and PyCrypto

From Dev

Ruby OpenSSL AES-128-CTR

From Dev

Link error when using AES256 example with OpenSSL

From Dev

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

From Dev

AES-256 Encryption with OpenSSL library using ECB mode of operation

From Dev

Encrypt using AES-256 like OpenSSL with PyCrypto

From Dev

How to encrypt file using OpenSSL and AES-256 with SHA-256?

From Dev

AES-256-CTR Encryption in node JS and decryption in Java

From Dev

Parameter details of OpenSSL's AES_ctr128_encrypt()

From Dev

Java and openssl C different AES CTR encryption result

From Dev

Decrypting a string encrypted by PHP with AES256-GCM using OpenSSL in C# using BouncyCastle

From Dev

C - OpenSSL with AES-256 and CFB mode

From Dev

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

From Dev

Using the nonce and counter correctly for AES-CTR mode

From Dev

How do I correctly encrypt a string using aes-256-ecb in NodeJS and decrypt with OpenSSL's command line?

From Dev

How do I correctly encrypt a string using aes-256-ecb in NodeJS and decrypt with OpenSSL's command line?

From Dev

OpenSSL AES 256 CBC via EVP api in C

From Dev

Whats is the Java name for openssl's "aes-256-cfb"?

From Dev

AES-256 from OpenSSL produces a different ciphertext each time

From Dev

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

From Dev

Different AES256 result between Java and OpenSSL on iOS

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 include OpenSSL in Visual Studio

From Dev

Decrypting aes-256-cbc using bouncycastle

From Dev

Decrypting aes-256-cbc using bouncycastle

Related Related

  1. 1

    AES 256 CTR Encryption/Decryption using Visual Studio and Openssl

  2. 2

    AES_128_CTR encryption by openssl and PyCrypto

  3. 3

    Ruby OpenSSL AES-128-CTR

  4. 4

    Link error when using AES256 example with OpenSSL

  5. 5

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

  6. 6

    AES-256 Encryption with OpenSSL library using ECB mode of operation

  7. 7

    Encrypt using AES-256 like OpenSSL with PyCrypto

  8. 8

    How to encrypt file using OpenSSL and AES-256 with SHA-256?

  9. 9

    AES-256-CTR Encryption in node JS and decryption in Java

  10. 10

    Parameter details of OpenSSL's AES_ctr128_encrypt()

  11. 11

    Java and openssl C different AES CTR encryption result

  12. 12

    Decrypting a string encrypted by PHP with AES256-GCM using OpenSSL in C# using BouncyCastle

  13. 13

    C - OpenSSL with AES-256 and CFB mode

  14. 14

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

  15. 15

    Using the nonce and counter correctly for AES-CTR mode

  16. 16

    How do I correctly encrypt a string using aes-256-ecb in NodeJS and decrypt with OpenSSL's command line?

  17. 17

    How do I correctly encrypt a string using aes-256-ecb in NodeJS and decrypt with OpenSSL's command line?

  18. 18

    OpenSSL AES 256 CBC via EVP api in C

  19. 19

    Whats is the Java name for openssl's "aes-256-cfb"?

  20. 20

    AES-256 from OpenSSL produces a different ciphertext each time

  21. 21

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

  22. 22

    Different AES256 result between Java and OpenSSL on iOS

  23. 23

    AES CTR encryption and decryption

  24. 24

    AES CTR implementation

  25. 25

    Python AES CTR to PHP

  26. 26

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

  27. 27

    How to include OpenSSL in Visual Studio

  28. 28

    Decrypting aes-256-cbc using bouncycastle

  29. 29

    Decrypting aes-256-cbc using bouncycastle

HotTag

Archive