gpt4 book ai didi

c - 如何在 C 中将字符数组与整数进行异或

转载 作者:行者123 更新时间:2023-11-30 16:39:47 25 4
gpt4 key购买 nike

我正在尝试用 C 实现加密算法。我有一个 unsigned char 数组;

 unsigned char ciphertext[] = { 0xA5, 0xB2, 0x3C, 0xAB, 0x03, 0xF1, 0xD3, 0x1C, 0x7F, 0xAD, 0x37, 0xA8, 0x8C, 0x8B, 0xCD, 0x90, 0xD4, 0xC2, 0x30, 0xAB, 0xD2, 0x3F, 0x3D, 0xAF, 0x58, 0x94, 0x1F, 0x50, 0xAF, 0xA2, 0xCE, 0x01 };

我需要将 char 数组与 1 到 256 之间的所有值进行异或。我如何在 C 中做到这一点?先感谢您。 编辑: 我想将我的 char 数组与;

unsigned char [] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
unsigned char two = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02};
unsigned char three[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03};

等等...这就是为什么我无法对每个元素进行异或的原因。这会是不同的事情。

最佳答案

类似于:

int i;
for (i = 0; i < length; i++) {
cryptotext[i] = ciphertext[i] ^ (i%256);
}

请注意,如果您不想保留纯文本,则只能使用一个数组:

ciphertext[i] ^= (i%256);

关于c - 如何在 C 中将字符数组与整数进行异或,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46929139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com