openssl 命令行解密 aes ctr 128

发布于 2025-01-08 11:38:10 字数 1778 浏览 0 评论 0原文

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

#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;
}

Is it possible to do the same thing using the openssl command line?

AFAIK,没有 -aes-128-ctr 密码,但是 openssl 中有等效的吗?

I'm using the following working code to decrypt a file:

#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;
}

Is it possible to do the same thing using the openssl command line?

AFAIK, there's no -aes-128-ctr cipher but is there any equivalent in openssl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小耗子 2025-01-15 11:38:10

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

No version of the openssl(1) command line tool that I've checked appears to support counter mode for any cipher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文