openssl命令行解密aes ctr 128

时间:2012-02-24 16:23:27

标签: c encryption command-line openssl aes

我正在使用以下工作代码来解密文件:

#include <openssl/aes.h>
#include <stdio.h>
#include <string.h>

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

FILE *fp;
FILE *rp;
FILE *op;
size_t count;   
char * buffer; 
AES_KEY key; 

int bytes_read, bytes_written;   
unsigned char indata[AES_BLOCK_SIZE]; 
unsigned char outdata[AES_BLOCK_SIZE];  
unsigned char ckey[] =  "1234567890123456"; 
unsigned char iv[8] = {0};
struct ctr_state state;   

void init_ctr(struct ctr_state *state, const unsigned char iv[8]){     
    state->num = 0;
    memset(state->ecount, 0, 16);
    memset(state->ivec + 8, 0, 8);
    memcpy(state->ivec, iv, 8);
} 

void decrypt(){
  //Opening files where text cipher text is read and the plaintext recovered         
  rp=fopen("c:\\temp\\decrypted.mp3","wb");
  op=fopen("c:\\temp\\encrypted.mp3","rb");
  if (rp==NULL) {fputs ("File error",stderr); return;}   
  if (op==NULL) {fputs ("File error",stderr); return;} 

  //Initializing the encryption KEY
  AES_set_encrypt_key(ckey, 128, &key); 
    init_ctr(&state, iv);//Counter call

  //Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext   
  while (1) {     
    bytes_read = fread(indata, 1, AES_BLOCK_SIZE, op);  
    AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num); 
    bytes_written = fwrite(outdata, 1, bytes_read, rp); 
    if (bytes_read < AES_BLOCK_SIZE) 
    break; 
    }   
  fclose (rp); 
  fclose (op);
}

int main(int argc, char *argv[]){   
  decrypt(); 
  return 0;
}

是否可以使用openssl命令行执行相同的操作?

AFAIK,没有-aes-128-ctr密码,但在openssl中是否有任何等价物?

1 个答案:

答案 0 :(得分:1)

我检查过的openssl(1)命令行工具的版本似乎不支持任何密码的计数器模式。