gpt4 book ai didi

c - Speck Cipher - C 中的解密问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:41 26 4
gpt4 key购买 nike

我正在尝试通过添加解密模块来扩展 Speck 密码 ( https://github.com/inmcm/Simon_Speck_Ciphers ) 的以下实现。我按照 NSA 原始论文中关于 Speck 和 Simon (https://eprint.iacr.org/2013/404) 的说明实现了解密算法。

目前我有点卡住了,因为我无法正确破译从加密例程生成的密文。我已经看过在 SO 上发布的其他类似问题,但无济于事。

通过检查我的代码和标准输出,我注意到从第一次迭代开始,在解密阶段存储在 x_word 中的值是不正确的。因此,以下指令可能存在问题:

x_word = rotate_left( (sub_mod((x_word ^ *(round_key_ptr + 21 - i)), y_word, 65535)), 7);

我在这里发布我的代码的相关部分:

#define rotate_left(x,n) (x >> (word_size - n)) | (x << n)
#define rotate_right(x,n) (x << (word_size - n)) | (x >> n)

void Speck_Encrypt_32(uint8_t *key_schedule, uint8_t *plaintext, uint8_t *ciphertext) {

const uint8_t word_size = 16;
uint16_t y_word = *(uint16_t *)plaintext;
uint16_t x_word = *(((uint16_t *)plaintext) + 1);
uint16_t *round_key_ptr = (uint16_t *)key_schedule;
uint16_t * word_ptr = (uint16_t *)ciphertext;

for(uint8_t i = 0; i < 22; i++) { // Block size 32 has only one round number option

x_word = ((rotate_right(x_word, 7)) + y_word) ^ *(round_key_ptr + i);
y_word = (rotate_left(y_word, 2)) ^ x_word;

printf("y_word - (%d) - %u\n", i, (unsigned int)y_word);
printf("x_word - (%d) - %u\n", i, (unsigned int)x_word);
}
// Assemble Ciphertext Output Array
*word_ptr = y_word;
word_ptr += 1;
*word_ptr = x_word;
return;
}

static inline uint16_t sub_mod(uint16_t a, uint16_t b, uint16_t m)
{
if ( a>=b )
return a - b;
else
return m - b + a;
}


void Speck_Decrypt_32(uint8_t *key_schedule, uint8_t *plaintext, uint8_t *ciphertext) {

const uint8_t word_size = 16;
// Swapping cipher text words
uint16_t y_word = *(uint16_t *)ciphertext;
uint16_t x_word = *(((uint16_t *)ciphertext) + 1);
uint16_t *round_key_ptr = (uint16_t *)key_schedule;
uint16_t * word_ptr = (uint16_t *)plaintext;

for(int i=0; i < 4; i++) {
printf("Ciphertext Byte %02d: %02x \n",i,ciphertext[i]);
printf("Plaintext Byte %02d: %02x \n",i,plaintext[i]);
}

// Reading round keys in reverse order
for(uint8_t i = 0; i < 22; i++) { // Block size 32 has only one round number option

//printf("y_word - (%d) - %u\n", i, (unsigned int)y_word);
//printf("x_word - (%d) - %u\n", i, (unsigned int)x_word);
// Inverting rotations and using custom modular subtraction
y_word = rotate_right((x_word ^ y_word), 2);
x_word = rotate_left( (sub_mod((x_word ^ *(round_key_ptr + 21 - i)), y_word, 65535)), 7);

}
// Assemble Plaintext - Swapping plaintext words
*word_ptr = y_word;
word_ptr += 1;
*word_ptr = x_word;
return;
}

我有一段时间没有用 C 编写代码了,可能会有一些错误。

最佳答案

鉴于 repo 协议(protocol)的作者没有绕过编写 C 解密例程,我首先担心 C 设置或加密加密例程的有效性。

与其从那里开始,不如看看其中一个有效的 C 实现:

https://github.com/tomasaazevedo/Speck_8-bit_C

https://github.com/yuehann/Simon_Speck

https://github.com/mysummergit/myssl/blob/master/demos/mycipher/speck.c

关于c - Speck Cipher - C 中的解密问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44977145/

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