AES 256 CTR encryption/decryption with Visual Studio and Openssl


Tanatos Daniel

Hello, gentlemen! I am trying to encrypt and decrypt a string using aes ctr with a 256 bit key. I have posted the code below. I'm missing something, but I don't know what. The Checktext produced by decryption is different from 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);
    }
Xie Jia ara

During decryption, replace

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

and

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

Related


AES 256 CTR encryption/decryption with Visual Studio and Openssl

Tanatos Daniel Hello everyone! I am trying to encrypt and decrypt a string using aes ctr with a 256 bit key. I have posted the code below. I'm missing something, but I don't know what. The Checktext produced by decryption is different from the plaintext. Thank

AES 256 CTR encryption/decryption with Visual Studio and Openssl

Tanatos Daniel Hello, gentlemen! I am trying to encrypt and decrypt a string using aes ctr with a 256 bit key. I have posted the code below. I'm missing something, but I don't know what. The Checktext produced by decryption is different from the plaintext. Tha

What counter increment function does OpenSSL EVP_aes_256_ctr use

Chicken 123 I am using openssl v1.0.2 through the evp.h interface. I'm using his EVP_aes_256_ctr()function and need to manage my counters and make sure they don't get reused. I am using EVP interface in FIPS mode. What incremental function does openssl ctr mod

What counter increment function does OpenSSL EVP_aes_256_ctr use

Chicken 123 I am using openssl v1.0.2 through the evp.h interface. I'm using his EVP_aes_256_ctr()function and need to manage my counters and make sure they don't get reused. I am using EVP interface in FIPS mode. What incremental function does openssl ctr mod

AES_128_CTR encryption with openssl and PyCrypto

Guan Deli Wondering the correct way to convert AES_128_CTR encryption to PyCrypto via openssl. First, I encrypted via openssl as follows: openssl enc -aes-128-ctr -in input.mp4 -out output.openssl.mp4 -K 7842f0a1ebc38f44e3e0c81943f68582 -iv d01f40dfc8ec8cd9 T

AES_128_CTR encryption with openssl and PyCrypto

Guan Deli Wondering the correct way to convert AES_128_CTR encryption to PyCrypto via openssl. First, I encrypted via openssl as follows: openssl enc -aes-128-ctr -in input.mp4 -out output.openssl.mp4 -K 7842f0a1ebc38f44e3e0c81943f68582 -iv d01f40dfc8ec8cd9 T

AES_128_CTR encryption with openssl and PyCrypto

Guan Deli Wondering the correct way to convert AES_128_CTR encryption to PyCrypto via openssl. First, I encrypted via openssl as follows: openssl enc -aes-128-ctr -in input.mp4 -out output.openssl.mp4 -K 7842f0a1ebc38f44e3e0c81943f68582 -iv d01f40dfc8ec8cd9 T

Ruby OpenSSL AES-128-CTR

Daniel I can't figure out what I'm doing wrong when decrypting a string of hex values with a given key using ruby's OpenSSL cipher AES-128-CTR. I am using gem hex_string to convert hex to bytes ctrkey = "36f18357be4dbd77f050515c73fcf9f2" ciphertext3 = "69dda84

AES_128_CTR encryption with openssl and PyCrypto

Guan Deli Wondering the correct way to convert AES_128_CTR encryption to PyCrypto via openssl. First, I encrypted via openssl as follows: openssl enc -aes-128-ctr -in input.mp4 -out output.openssl.mp4 -K 7842f0a1ebc38f44e3e0c81943f68582 -iv d01f40dfc8ec8cd9 T

Ruby OpenSSL AES-128-CTR

Daniel I can't figure out what I'm doing wrong when decrypting a string of hex values with a given key using ruby's OpenSSL cipher AES-128-CTR. I am using gem hex_string to convert hex to bytes ctrkey = "36f18357be4dbd77f050515c73fcf9f2" ciphertext3 = "69dda84

Encrypt in Node JS and decrypt in Golang using AES 256 CTR

Chacha: I have encrypted some JSON text using the following Node JS code:- var algorithm = 'aes-256-ctr'; var crypto = require('crypto'); var password = "6A80FD8D38D579D1090F6CDB62C729311781E4BA31CD7D804BD7BF5AEC3BFC2D" var typedRequest = {"abc":"cde"} var

Does Android support AES256/CTR/NoPadding?

Hongkong Cipher cipher = Cipher.getInstance("AES256/CTR/NoPadding") The above code throws the following exception: Stack Trace: java.security.NoSuchAlgorithmException: AES256/CTR/NoPadding at javax.crypto.Cipher.getInstance(Cipher.java:174) at javax.crypto.Ci

Encrypt in Node JS and decrypt in Golang using AES 256 CTR

Chacha: I have encrypted some JSON text using the following Node JS code:- var algorithm = 'aes-256-ctr'; var crypto = require('crypto'); var password = "6A80FD8D38D579D1090F6CDB62C729311781E4BA31CD7D804BD7BF5AEC3BFC2D" var typedRequest = {"abc":"cde"} var

Encrypt in Node JS and decrypt in Golang using AES 256 CTR

Chacha: I have encrypted some JSON text using the following Node JS code:- var algorithm = 'aes-256-ctr'; var crypto = require('crypto'); var password = "6A80FD8D38D579D1090F6CDB62C729311781E4BA31CD7D804BD7BF5AEC3BFC2D" var typedRequest = {"abc":"cde"} var

Does Android support AES256/CTR/NoPadding?

Hongkong Cipher cipher = Cipher.getInstance("AES256/CTR/NoPadding") The above code throws the following exception: Stack Trace: java.security.NoSuchAlgorithmException: AES256/CTR/NoPadding at javax.crypto.Cipher.getInstance(Cipher.java:174) at javax.crypto.Ci

Does Android support AES256/CTR/NoPadding?

Hongkong Cipher cipher = Cipher.getInstance("AES256/CTR/NoPadding") The above code throws the following exception: Stack Trace: java.security.NoSuchAlgorithmException: AES256/CTR/NoPadding at javax.crypto.Cipher.getInstance(Cipher.java:174) at javax.crypto.Ci

Decryption of AES-256-CTR encrypted Node JS and Java

Ashish Pandey: I tried to encode in NodeJS with decryption and it worked fine with NodeJS. But when I try to do decryption in Java using the same IV and secret, it doesn't behave as expected. Here is the code snippet: In encrypted NodeJS: var crypto = require(

Encrypt in Node JS and decrypt in Golang using AES 256 CTR

Chacha: I have encrypted some JSON text using the following Node JS code:- var algorithm = 'aes-256-ctr'; var crypto = require('crypto'); var password = "6A80FD8D38D579D1090F6CDB62C729311781E4BA31CD7D804BD7BF5AEC3BFC2D" var typedRequest = {"abc":"cde"} var

Encrypt in Node JS and decrypt in Golang using AES 256 CTR

Chacha: I have encrypted some JSON text using the following Node JS code:- var algorithm = 'aes-256-ctr'; var crypto = require('crypto'); var password = "6A80FD8D38D579D1090F6CDB62C729311781E4BA31CD7D804BD7BF5AEC3BFC2D" var typedRequest = {"abc":"cde"} var

Does Android support AES256/CTR/NoPadding?

Hongkong Cipher cipher = Cipher.getInstance("AES256/CTR/NoPadding") The above code throws the following exception: Stack Trace: java.security.NoSuchAlgorithmException: AES256/CTR/NoPadding at javax.crypto.Cipher.getInstance(Cipher.java:174) at javax.crypto.Ci

Does Android support AES256/CTR/NoPadding?

Hongkong Cipher cipher = Cipher.getInstance("AES256/CTR/NoPadding") The above code throws the following exception: Stack Trace: java.security.NoSuchAlgorithmException: AES256/CTR/NoPadding at javax.crypto.Cipher.getInstance(Cipher.java:174) at javax.crypto.Ci

Parameter details for OpenSSL's AES_ctr128_encrypt()

cupamanduka I am trying to understand the parameters of the following function in the openSSL encryption library. void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key, unsigned char ivec[AES_BLOCK_SIZE]

Java and openssl C different AES CTR encryption results

Ivan Romanov I need to port an application from Java to C++. I'm stuck with AES CTR 128. In Java and C it has different encryption results. So my Java code. import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKe

C - OpenSSL with AES-256 and CFB mode

ur3k I'm using Ubuntu 14.10 and trying to use C and OpenSSL I get the same output as using: openssl enc -aes-256-cfb8 -in test -out test.enc -K $key -iv $iv I've been working on it for two days and I'm not getting the encryption, it works but not what I shoul

C - OpenSSL with AES-256 and CFB mode

ur3k I'm using Ubuntu 14.10 and trying to use C and OpenSSL I get the same output as using: openssl enc -aes-256-cfb8 -in test -out test.enc -K $key -iv $iv I've been working on it for two days and I'm not getting the encryption, it works but not what I shoul

MCrypt rijndael-256 to OpenSSL AES-256-ECB conversion

Miracle of Mahesh Since Mcrypt is deprecated, I want to use OpenSSL in the code as we are already using php 7.2.4 in the server. I have used the following code for encryption/decryption. //encryption function encrypt($text, $salt='') { if ($text == "")

MCrypt rijndael-256 to OpenSSL AES-256-ECB conversion

Miracle of Mahesh Since Mcrypt is deprecated, I want to use OpenSSL in the code as we are already using php 7.2.4 in the server. I have used the following code for encryption/decryption. //encryption function encrypt($text, $salt='') { if ($text == "")