#include <iostream>
#include <stdio.h>
#include <iomanip>
using namespace std;
typedef unsigned char byte;
//加法
byte add(byte a,byte b)
{
return a^b;
}
//乘法
byte multiply(byte a,byte b)
{
//16bit
unsigned short int res, i, temp;
res=0,temp=a;
//乘
for(i=1; i<=0x80; i<<=1,temp<<=1)
{
if(i&b)
{
res^=temp;
}
}
//取余
for(i=0x8000, temp=0x8d80; i>0x80; i>>=1, temp>>=1)
if(i&res)
res^=temp;
return (byte)res;
}
//倍乘
byte xtime(byte a)
{
if(a&0x80)//最高位为1
{
//先左移然后和AES要求多项式对应的二进制 0xb1 取模
return (a<<1)^0x1b;
}
else
{
return a<<1;
}
}
int main()
{
int a,b;
int num;
while(1)
{
cout<<"please choose 1.add 2.multiply 3.xtime 4.esc"<<endl;
cin>>num;
switch(num)
{
case 1:
cout<<"please enter two Hexadecimal numbers."<<endl;
cin>>hex>>a;
cin>>hex>>b;
printf("%X\n",add(a,b));
break;
case 2:
cout<<"please enter two Hexadecimal numbers."<<endl;
cin>>hex>>a;
cin>>hex>>b;
printf("%X\n",multiply(a,b));
break;
case 3:
cout<<"please enter a Hexadecimal number."<<endl;
cin>>hex>>a;
printf("%X\n",xtime(a));
break;
//cout<<setiosflags(ios::uppercase)<<hex<<(xtime(a))<<endl;
case 4:
goto end;
default:
cout<<"enter the wrong number!"<<endl;
}
}
end:
return 0;
}
AES简单实现
最新推荐文章于 2021-05-23 06:18:09 发布