下面提到的代码使用 C 提供的 time.h 库生成 key 。
我正在尝试使用变量 a
逐字节打印键,但我面临的主要问题是当我将值从 key[i]
复制到 a 时
它会跳过零,因为我在打印 key[i]
时使用 %.2x
,但不在 a
中使用。我想将字节存储在 a
中,也带有零,如单个字节的 key[i]
打印。
其次,我想知道如何在单个字符串 finalKey
a
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define KEYSIZE 16
typedef unsigned char BYTE;
void main()
{
//2018-04-17 23:08:49
//TOTAL SECONDS = 1,524,006,529
long long int total_seconds = 1524006529;
char finalKey[KEYSIZE];
int i;
char key[KEYSIZE];
int x = 0;
printf("%lld",total_seconds);
for(x = 0;x<7200;x++){
srand (total_seconds--);
//KEYSIZE = 16 so 16 Iterations each time prints a hexadecimal
//HAVE TO STORE WHOLE KEY in finalKey after 16 iterations
for (i = 0; i< KEYSIZE; i++){
key[i] = rand()%256;
BYTE a;
// printf("%.2x", (unsigned char)key[i]);
// PRINTS = > 4dd733c6bc3d355ca2f90c4811d2960c
a = (key[i]&0xFF);
printf("%x",a);
// PRINTS => 4dd733c6bc3d355ca2f9c4811d296c ( removes 0 )
}
// system("openssl enc aes-128-cbc -d -in plaintext.txt -out output.txt -k " + finalKey +" -iv 09080706050403020100A2B2C2D2E2F2");
printf("\n");
}
}
最佳答案
您想要打印至少两个填充了前导零的字符。因此请使用:
printf("%02x",a);
2
的字段宽度指定应打印至少两个字符,0
标志表示结果应在左侧填充零。
关于c - 十六进制打印中的零跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58223849/