为了加密密钥,我声明了unsigned char *
:
unsigned char *key = (unsigned char *)"0123456789012345";
我想这样做,以便密钥全是0
(而不是ASCII字符'0')。
我对C有点生疏,所以我宣布这样:
unsigned char *iv = (unsigned char *){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
哪个给了我警告,所以我该如何正确地做到这一点?
答案 0 :(得分:4)
你可以写
unsigned char iv[16] = { 0 };
至于此声明
unsigned char *iv = (unsigned char *){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
尝试使用复合文字,然后其有效记录看起来就像在示范程序中显示的那样
#include <stdio.h>
int main(void)
{
enum { N = 16 };
unsigned char *iv = ( unsigned char[N] ){ 0 };
for ( size_t i = 0; i < N; i++ ) printf( "%d ", iv[i] );
putchar( '\n' );
return 0;
}
其输出
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
答案 1 :(得分:2)
您必须为存储分配内存:
$ docker run -it python:latest
Python 3.6.5 (default, Mar 31 2018, 01:15:58)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
可替换地:
unsigned char iv [16];
memset (iv, 0, sizeof iv);
答案 2 :(得分:-1)
我建议使用memset()命令。
memset(iv, 0, sizeof(*iv))
编辑:我的错误,不小心把iv留下了明星