现代对称密码之IDEA算法Cpp源码

IDEA.h的源码如下:
#ifndef IDEA_H
#define IDEA_H

typedef unsigned char byte;
typedef unsigned short word16;
typedef unsigned long word32;

class IDEA{
	public:
		void setKey(byte in[]);
		void setPlainText(byte in[]);
		word16 invMul(word16 x);
		word16 mul(word16 x, word16 y);
		void encryption(word16 in[], word16 out[], word16* EK);
		void enc();
		void IDEATest();
	private:
		void getEncRoundKey(word16* encRoundKey);
		void getDecRoundKey(word16 const* EK, word16 DK[]);
		byte key[16];
		word16 plainText[4];
		word16 cipherText[4];
		word16 deCipherText[4];
		word16 encRoundKey[52];
		word16 decRoundKey[52];
		void checkRounakey();
};

#endif
IDEA.cpp
#include "IDEA.h"
#include <fstream>


using namespace std;

void IDEA::setPlainText(byte in[]){
	int i;
	for(i=0;i<8;i+=2){
		plainText[i/2]=(in[i]<<8)+in[i+1];
	}
}

void IDEA::setKey(byte in[]){
	int i;
	for(i=0;i<16;i++){
		key[i]=in[i];
	}
	getEncRoundKey(encRoundKey);
	getDecRoundKey(encRoundKey, decRoundKey);
}

void IDEA::getEncRoundKey(word16* encRoundKey){
	int i,j;
	for(i=0,j=0;j<8;j++){
		encRoundKey[j]=(key[i]<<8)+key[1+1];
		i+=2;
	}
	for (i=0;j<52;j++){
		i++ ;
		encRoundKey[i+7]=encRoundKey[i&7]<<9 | encRoundKey[(i+1)&7]>>7;
		encRoundKey+=i&8;
		i&=7;
	}
}

void IDEA::getDecRoundKey(word16 const *EK, word16 DK[]){
	int i;
	word16 temp[52];		//计算用临时密钥组
	word16 t1,t2,t3;		//计算用临时变量
	word16 *p=temp+52;		//52为密钥数量
	t1=invMul(*EK++);
	t2=-*EK++;
	t3=-*EK++;
	*--p=invMul(*EK++);
	*--p=t3;
	*--p=t2;
	*--p=t1;
	for(i=0;i<7;i++){
		t1=*EK++;
		*--p=*EK++;
		*--p=t1;
		t1=invMul(*EK++);
		t2=-*EK++;
		t3=-*EK++;
		*--p=invMul(*EK++);
		*--p=t2;
		*--p=t3;
		*--p=t1;
	}
	t1=*EK++;
	*--p=*EK++;
	*--p=t1;
	t1=invMul(*EK++);
	t2=-*EK++;
	t3=-*EK++;
	*--p=invMul(*EK++);
	*--p=t3;
	*--p=t2;
	*--p=t1;
	for(i=0,p=temp;i<52;i++){
		*DK++=*p;
		*p++=0;
	}
}


word16 IDEA::invMul(word16 x){
	word16 t0,t1;
	word16 q,y;
	if(x<=1){
		return x;			//x=0或x=1,乘法逆元为其本身
	}
	t1=word16(0x10001L/x);
	y=word16(0x10001L%x);
	if(y==1){
		return (1-t1)&0xFFFF;
	}
	t0=1;
	do{
		q=x/y;
		x=x%y;
		t0+=q*t1;
		if(x==1){
			return t0;
		}
		q=y/x;
		y=y%x;
		t1+=q*t0;
	}while(y!=1);
	
	return (1-t1)&0xFFFF;
}

void IDEA::encryption(word16 in[], word16 out[], word16* EK){
	word16 x1,x2,x3,x4,t1,t2;
	x1=in[0];
	x2=in[1];
	x3=in[2];
	x4=in[3];
	int r=8;
	do{
		x1=mul(x1, *EK++);
		x2+=*EK++;
		x3+=*EK++;
		x4=mul(x4, *EK++);
		t2=x1^x3;
		t1=x2^x4;
		t2=mul(t2, *EK++);
		t1=t1+t2;
		t1=mul(t1, *EK++);
		t2=t1+t2;
		x1^=t1;
		x4^=t2;
		t2^=x2;
		x2=x3^t1;
		x3=t2;
	}while(--r);
	x1=mul(x1, *EK++);
	*out++=x1;
	*out++=x3+*EK++;
	*out++=x2+*EK++;
	x4=mul(x4,*EK++);
	*out=x4;
}

word16 IDEA::mul(word16 x, word16 y){
	word32 p;
	p=(word32)x*y;
	if(p){
		y=p&0xFFFF;		//取低16位
		x=p>>16;
		return (y-x)+(y<x) ;
	}else if(x){
		return 1-y;
	}else{
		return 1-x;
	}
}

void IDEA::enc(){
	encryption(plainText, cipherText, encRoundKey);
	encryption(cipherText, deCipherText, decRoundKey);
}

void IDEA::IDEATest(){
	ofstream out("ideatest.out");
	out<<"The input key is:"<<endl;
	int i;
	for(i=0;i<16;i++){
		out<<hex<<int(key[i])<<" ";
	}
	out<<endl;
	out<<"The plain text is:"<<endl;
	for(i=0;i<4;i++){
		out<<hex<<plainText[i]<<" ";
	}
	out<<endl;
	out<<"The cipherText is:"<<endl;
	for(i=0;i<4;i++){
		out<<hex<<cipherText[i]<<" ";
	}
	out<<endl;
	out<<"The deCipherText is:"<<endl;
	for(i=0;i<4;i++){
		out<<hex<<deCipherText[1]<<" ";
	}
	out<<endl;
}
main.cpp的源代码如下:
#include <iostream>
#include "IDEA.h"

/* run this program using the console pauser&nbs***bsp;add your own getch, system("pause")&nbs***bsp;input loop */

int main(int argc, char** argv) {
	IDEA idea;
	byte key[16]= {0x10, 0x1A, 0x0C, 0x0B, 0x01, 0x11, 0x09, 0x07,
				0x32, 0xA1, 0xB3, 0x06, 0x23, 0x12, 0xD3, 0xF1};
	idea.setKey(key);
	byte plainText[8]={0xA7, 0x95, 0x87, 0x23, 0x1F, 0x2C, 0x6D, 0x73};
	idea.setPlainText(plainText);
	idea.enc();
	idea.IDEATest();
	
	return 0;
}

全部评论

相关推荐

||&nbsp;先说下主播个人情况:211本,暑期实习之前有过一段中大厂的后端实习,暑期拿过腾讯的实习offer,综合考虑业务和语言最终去了美团。实习期间体感还是不错的,5月初去的,去了就一直急着要需求做,担心因为没有产出导致转正失败,在第二个星期就和mt透露我希望能够留用。虽然第一个由于美团新人landing的友好性基本没做什么需求,但是后面也写出了小2w行的代码量(不包含单测)。中期经常主动加班赶需求,经常持续一两个星期加班到10点甚至更后面。mt对我确实不错,也是言传身教,实习期间给我讲了很多关于单测,ddd,set化等的理解,也是受益匪浅,此外在做需求的时候,也能看出把比较有含金量的部分交给我做...
菜菜菜小白菜菜菜:我在字节实习了四个月,有转正的压力所以周末大部分也在公司自学,也是因为一些原因转正拖的很久,这个点还没答辩,过段时间才回去答辩。整个不确定性的焦虑贯穿了我的秋招三个月,我也曾经犹豫过是不是应该放弃转正走秋招更快,最后因为沉没成本一直舍不得放弃,前前后后七个月真的挺累的,尤其是没有来字节实习的同学已经校招拿到意向时更加焦虑。这段时间也跟mentor聊了很多次,他告诉我未来工作上或者生活上,比这些更头疼的事情会更多,关键还是要调整好自己的心态。转正没有通过从过程上来看其实跟你自身没太大的关系,拖了三个月不出结果显然是ld的问题,并且今年美团最近的开奖大家似乎都不是很乐观,所以不去也罢。我在字节实习的时候,6月份有一个赶上春招末期的25届同事刚面进来,也拿到了小sp的薪水。不要对这件事有太大的压力,时代的问题罢了
点赞 评论 收藏
分享
笑着秋招😊:我一直认为努力有回报是一件很幸福很幸福的事情,恭喜你
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务