#include
using namespace std;
//明文
char M[10000]="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
//分组数据
int a[64],b[64],c[64],d[64]={0};
//密钥,赋值的为K[0]这个赋值便是算法中的密钥K
int Ktmp[64],K[11][64]={{1,1,3,1,5,8,9,5,5,1,1,3,1,5,8,9,9,5,1,1,3,1,5,8,8,9,5,1,1,3,1,5,5,8,9,5,1,1,3,1,1,5,8,9,5,1,1,3,3,1,5,8,9,5,1,1,1,3,1,5,8,9,5,1}};
//θ变换用到的数组C
int C[64]={1,1,3,1,5,8,9,5,
5,1,1,3,1,5,8,9,
9,5,1,1,3,1,5,8,
8,9,5,1,1,3,1,5,
5,8,9,5,1,1,3,1,
1,5,8,9,5,1,1,3,
3,1,5,8,9,5,1,1,
1,3,1,5,8,9,5,1};
//S盒
int S[256]={0x68,0x10,0xeb,0x2b,0x48,0x9d,0x6a,0xe4,
0xe3,0xa3,0x56,0x81,0x7d,0xf1,0x85,0x93,
0x2c,0x8e,0x78,0xca,0x17,0xa9,0x61,0xd5,
0x5d,0x0b,0x8c,0x3c,0x77,0x51,0x22,0x42,
0x3f,0x54,0x41,0x80,0xcc,0x86,0xb3,0x18,
0x2e,0x57,0x06,0x62,0xf4,0x36,0xd1,0x6b,
0x1b,0x65,0x75,0x10,0xda,0x49,0x26,0xf9,
0xcb,0x66,0xe7,0xba,0xae,0x50,0x52,0xab,
0x05,0xf0,0x0d,0x73,0x3b,0x04,0x20,0xfe,
0xdd,0xf5,0xb4,0x5f,0x0a,0xb5,0xc0,0xa0,
0x71,0xa5,0x2d,0x60,0x72,0x93,0x39,0x08,
0x83,0x21,0x5c,0x87,0xb1,0xe0,0x00,0xc3,
0x12,0x91,0x8a,0x02,0x1c,0xe6,0x45,0xc2,
0xc4,0xfd,0xbf,0x44,0xa1,0x4c,0x33,0xc5,
0x84,0x23,0x7c,0xb0,0x25,0x15,0x35,0x69,
0xff,0x94,0x4d,0x70,0xa2,0xaf,0xcd,0xd6,
0x6c,0xb7,0xf8,0x09,0xf3,0x67,0xa4,0xea,
0xec,0xb6,0xd4,0xd2,0x14,0x1e,0xe1,0x24,
0x38,0xc6,0xdb,0x4b,0x7a,0x3a,0xde,0x5e,
0xdf,0x95,0xfc,0xaa,0xd7,0xce,0x07,0x0f,
0x3d,0x58,0x9a,0x98,0x9c,0xf2,0xa7,0x11,
0x7e,0x8b,0x43,0x03,0xe2,0xdc,0xe5,0xb2,
0x4e,0xc7,0x6d,0xe9,0x27,0x40,0xd8,0x37,
0x92,0x8f,0x01,0x1d,0x53,0x3e,0x59,0xc1,
0x4f,0x32,0x16,0xfa,0x74,0xfb,0x63,0x9f,
0x34,0x1a,0x2a,0x5a,0x8d,0xc9,0xcf,0xf6,
0x90,0x28,0x88,0x9b,0x31,0x0e,0xbd,0x4a,
0xe8,0x96,0xa6,0x0c,0xc8,0x79,0xbc,0xbe,
0xef,0x6e,0x46,0x97,0x5b,0xed,0x19,0xd9,
0xac,0x99,0xa8,0x29,0x64,0x1f,0xad,0x55,
0x13,0xbb,0xf7,0x6f,0xb9,0x47,0x2f,0xee,
0xb8,0x7b,0x89,0x30,0xd3,0x7f,0x76,0x82
};
// 第n组明文转换为整形
void code(int n,int a[64],int b[64],int s=64)
{
for(int i=0;i256则无法补充256bit的 明文长度,需要多添加一个分组,下面两个函数即为两种情况
int extendsion1(unsigned long len,unsigned long n)
{
int x=len%64;
code(n-1,a,b,x);
a[x]=128;
for(int i=x+1;i<32;i++)a[i]=0;
for(int i=0;i<32;i++)
{
//进行移位操作时需要注意,移位位数不能超过被处理数据所占bit数,如果溢出则无法得到想要结果,这里使用循环每次移位8bit
unsigned long q=len;
for(int j=0;j>8;
a[63-i]=q&0x000000ff;
}
}
int extendsion2(unsigned long len,unsigned long n)
{
int x=len%64;
code(n-2,a,b,x);
a[x]=128;
for(int i=x+1;i<64;i++)a[i]=0;
same(a,c);
for(int i=0;i<64;i++)a[i]=a[i]&0x000000ff;
for(int i=0;i<64;i++)printf("%02x",a[i]);
Rho(a,b);
for(int i=0;i<32;i++)a[i]=0;
for(int i=0;i<32;i++)
{
unsigned long q=len;
for(int j=0;j>8;
a[63-i]=q&0x000000ff;
}
}
int main(int argc, char *argv[]) {
unsigned long len=strlen(M);//先计算,明文字符串长度
int P=((len%64)<32)?1:0;//判断使用哪种填充方式
unsigned long n=(P==1)?(len/64+1):(len/64+2);
extension();//进行密钥拓展
if(P==1)//进行判断,如果P=1那么,len%64<32不需要额外拓展一个分组
{
for(int i=1;i<=n-1;i++)
{
code(i,a,b);
//进行a[i]&0x000000ff的目的是将高于8bit的数据置为0,以免影响输出
for(int i=0;i<64;i++)a[i]=a[i]&0x000000ff;
for(int i=0;i<64;i++)printf("%02x",a[i]);
same(a,c);
Rho(a,b);
}
extendsion1(len,n);
for(int i=0;i<64;i++)a[i]=a[i]&0x000000ff;
for(int i=0;i<64;i++)printf("%02x",a[i]);
same(a,b);
same(a,c);
Rho(a,b);
}
else
{
for(int i=1;i<=n-2;i++)
{
code(i,a,b);
for(int i=0;i<64;i++)a[i]=a[i]&0x000000ff;
for(int i=0;i<64;i++)printf("%02x",a[i]);
same(a,c);
Rho(a,b);
}
extendsion2(len,n);
for(int i=0;i<64;i++)a[i]=a[i]&0x000000ff;
for(int i=0;i<64;i++)printf("%02x",a[i]);
same(a,b);
same(a,c);
Rho(a,b);
}
cout<
最后附上运行截图,输出为转为整型的扩展后的明文数据,以及最后输出的哈希值,输出格式限制为2位16进制,即每个字节为单位输出,输出两位16进制数。