idea怎么进行c语言编程_IDEA算法c语言实现

本文档提供了IDEA(International Data Encryption Algorithm)加密和解密的C语言实现,包括了加法、乘法和XOR操作的静态函数,以及IDEA的加密和解密过程。代码中详细注释了每个步骤,适用于理解IDEA算法的工作原理。
摘要由CSDN通过智能技术生成

/* IDEA(International Data Encryption Algorithm), refer to http://www.quadibloc.com/crypto/co040302.htm

* IDEA.c, an IDEA encryption and decryption program.

* Author shenyang

* Mar. 4th, 2011

* TODO: Fault analysis on IDEA, defence of fault analysis on IDEA.

*/

#ifndef IDEA_H

#include "IDEA.h"

#endif

#include 

#include 

/* define operation */

staticuint16_t add_mod(uint16_t a, uint16_t b);

staticuint16_t mp_mod(uint16_t a,uint16_t b);

staticuint16_t XOR(uint16_t a, uint16_t b);

staticstatus_t left_shift(uint16_t key[8],intnum);

staticvoidswap(uint16_t *a, uint16_t *b);

/* addition and mod 65536 */

staticuint16_t add_mod(uint16_t a, uint16_t b)

{

uint32_t tmp = a+b;

uint16_t ret = tmp % IDEA_ADD_MODULAR;

returnret;

}

/* multiply and mod 65537 */

staticuint16_t mp_mod(uint16_t a,uint16_t b)

{

/* Note: In IDEA, for purposes of multiplication, a 16 bit word containing all zeroes is considered to represent the number 65,536;

* other numbers are represented in conventional unsigned notation, and multiplication is modulo the prime number 65,537

*/

uint64_t tmp, tmp_a, tmp_b; //if both a and b are 2^16, the result will be 2^32 which will exceed a 32-bit int

tmp_a = a==0 ? (1<<16) : a;

tmp_b = b==0 ? (1<<16) : b;

tmp = (tmp_a * tmp_b) % IDEA_MP_MODULAR;

return(uint16_t)(tmp);

}

/* XOR */

staticuint16_t XOR(uint16_t a, uint16_t b)

{

returna^b;

}

staticvoidswap(uint16_t *a, uint16_t *b)

{

uint16_t c = 0;

c = *a;

*a = *b;

*b = c;

}

/* IDEA encryption */

status_t idea_encrypt(uint64_t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值