📄 md5dlg.cpp
字号:
FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG(a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG(d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG(b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH(a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH(d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH(a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II(a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II(d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II(b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II(b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II(a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
CString sp1,sp2,sp3,sp4,sp,tempa,tempb,tempc,tempd,temp1[8],temp2[8],temp3[8],temp4[8];
sp1.Format(_T("%08x"),state[0]);
sp2.Format(_T("%08x"),state[1]);
sp3.Format(_T("%08x"),state[2]);
sp4.Format(_T("%08x"),state[3]);
{temp1[0]=sp1.GetAt(6);temp1[1]=sp1.GetAt(7);temp1[2]=sp1.GetAt(4);temp1[3]=sp1.GetAt(5);temp1[4]=sp1.GetAt(2);temp1[5]=sp1.GetAt(3);temp1[6]=sp1.GetAt(0);temp1[7]=sp1.GetAt(1);tempa=temp1[0]+temp1[1]+temp1[2]+temp1[3]+temp1[4]+temp1[5]+temp1[6]+temp1[7];}
{temp2[0]=sp2.GetAt(6);temp2[1]=sp2.GetAt(7);temp2[2]=sp2.GetAt(4);temp2[3]=sp2.GetAt(5);temp2[4]=sp2.GetAt(2);temp2[5]=sp2.GetAt(3);temp2[6]=sp2.GetAt(0);temp2[7]=sp2.GetAt(1);tempb=temp2[0]+temp2[1]+temp2[2]+temp2[3]+temp2[4]+temp2[5]+temp2[6]+temp2[7];}
{temp3[0]=sp3.GetAt(6);temp3[1]=sp3.GetAt(7);temp3[2]=sp3.GetAt(4);temp3[3]=sp3.GetAt(5);temp3[4]=sp3.GetAt(2);temp3[5]=sp3.GetAt(3);temp3[6]=sp3.GetAt(0);temp3[7]=sp3.GetAt(1);tempc=temp3[0]+temp3[1]+temp3[2]+temp3[3]+temp3[4]+temp3[5]+temp3[6]+temp3[7];}
{temp4[0]=sp4.GetAt(6);temp4[1]=sp4.GetAt(7);temp4[2]=sp4.GetAt(4);temp4[3]=sp4.GetAt(5);temp4[4]=sp4.GetAt(2);temp4[5]=sp4.GetAt(3);temp4[6]=sp4.GetAt(0);temp4[7]=sp4.GetAt(1);tempd=temp4[0]+temp4[1]+temp4[2]+temp4[3]+temp4[4]+temp4[5]+temp4[6]+temp4[7];}
sp=tempa+tempb+tempc+tempd;
m_msg2=sp;
UpdateData(FALSE);
}
void CMD5Dlg::Init()
{
state[0]=0x67452301;
state[1]=0xefcdab89;
state[2]=0x98badcfe;
state[3]=0x10325476;
}
UINT4 CMD5Dlg::count_file()///////确定MD5文档中有多少个512位,从而确定主体循环的次数
{
FILE *fp;
UINT4 c=0;
fp=fopen("MD5明文处理后二进制表示.txt","r+");
if((fp=fopen("MD5明文处理后二进制表示.txt","r+"))==NULL)
{AfxMessageBox("can not open file\n");exit(0);}
while( !feof(fp) )
{
fgetc(fp);
c++;
}
return c-1;
}
////////////////////////////////////////////////////////////////////////////////////////SHA1相关加密函数
void CMD5Dlg::SHA_minwen()////sha明文的二进制表示函数
{
UpdateData(TRUE);
FILE *fp;
char ch;
static int i=0,j,str[8];
////////建立一个名为“明文处理后二进制表示.txt”保存好输入的明文二进制信息
fp=fopen("SHA明文处理后二进制表示.txt","w");//建立文件“明文二进制处理后表示.txt”
int m=m_msg1.GetLength();
for(i=0;i<m;i++)
{
ch=m_msg1.GetAt(i);
for(j=0;j<8;j++)//把每个字符分别转换为二进制数
{
str[j]=ch%2;
ch=ch/2;
}
for(j=7;j>=0;j--)
fprintf(fp,"%d",str[j]);//把每组八位二进制数依次存入文件中
}
////////填充原信息长度使其满足512的倍数余448
int n_binary=m_msg1.GetLength()*8;//字符个数计算
int temp=n_binary;
int n=n_binary%512;//对512取余看其是否是512的倍数再余448
if(n==448)//如果刚好余448则追加64位表示原始信息长度的数据
;
else if(n<448)//如果不满足n=448则对其实行追加至448,追加规则为追加的第一位为1其于为0直至n为448
{
int i=1;
int k=448-n;
fseek(fp,0,SEEK_END);
fprintf(fp,"%d",1);
while(i<=k-1)//信息的填充
{
i++;
fseek(fp,0,SEEK_END);
fprintf(fp,"%d",0);
}
}
else if(n>448)
{
int i=1;
int k=512-n;
fseek(fp,0,SEEK_END);
fprintf(fp,"%d",1);
while(i<=k+447)//信息的填充
{
i++;
fseek(fp,0,SEEK_END);
fprintf(fp,"%d",0);
}
}
////////追加最后64位信息长度
j=63;
int len_letter[64]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};;//保存原信息的64位长度
while(temp>=1)//生成需追加的64位数
{
len_letter[j]=temp%2;
temp/=2;
j--;
}
for(i=0;i<=63;i++)
{
fseek(fp,0,SEEK_END);
fprintf(fp,"%d",len_letter[i]);//追加64位表示原始信息长度的数据
}
fclose(fp);
}
UINT4 CMD5Dlg::sha_c_to_l(int l, int t)////sha1的明文划分为16个字的数据元组
{
int i=0;
UINT4 m=0x0;
FILE *fp;
fp=fopen("SHA明文处理后二进制表示.txt","r+");
fseek(fp,t*512+l*32,0);//
char buffer[33];
if(!feof(fp))
fread(buffer,sizeof(char),32,fp);
for(i=0;i<=31;i++)
{
if(buffer[i]=='1')
m+=squ(31-i);
}
fclose(fp);
return m;
}
void CMD5Dlg::OnSHA()
{
UpdateData(TRUE);
if(m_msg1.IsEmpty())
{
m_msg1=' ';AfxMessageBox("无输入时默认明文信息为空!");
}
SHA_minwen();
SHA_init();
UINT4 W[80]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};/////80个字的缓冲区
int i;
UINT4 q=0,TEMP;
if(count_file_sha()%512==0)
q=count_file_sha()/512-1;//////q个512位执行tp次
else
q=count_file_sha()/512;
for(UINT4 k=0;k<=q;k++){
UINT4 A=H[0],B=H[1],C=H[2],D=H[3],E=H[4];
/////////产生16个子串
for(i=0;i<=15;i++)
W[i]=sha_c_to_l(i,0);
for(i=16;i<80;i++)
W[i]=S((W[i-3]^W[i-8]^W[i-14]^W[i-16]),1);
for(i=0;i<=19;i++)//使用F1的前二十轮循环
{
TEMP=S(A,5)+F1(B,C,D)+E+W[i]+K(i);
E=D;D=C;C=S(B,30);B=A;A=TEMP;
}
for(i=20;i<=39;i++)//使用F2的第二个二十轮循环
{
TEMP=S(A,5)+F2(B,C,D)+E+W[i]+K(i);
E=D;D=C;C=S(B,30);B=A;A=TEMP;
}
for(i=40;i<=59;i++)//使用F3的第三个二十轮循环
{
TEMP=S(A,5)+F3(B,C,D)+E+W[i]+K(i);
E=D;D=C;C=S(B,30);B=A;A=TEMP;
}
for(i=60;i<=79;i++)//使用F4的第四个二十轮循环
{
TEMP=S(A,5)+F4(B,C,D)+E+W[i]+K(i);
E=D;D=C;C=S(B,30);B=A;A=TEMP;
}
H[0]=H[0]+A;
H[1]=H[1]+B;
H[2]=H[2]+C;
H[3]=H[3]+D;
H[4]=H[4]+E;
}
CString sp1,sp2,sp3,sp4,sp5,sp,tempa,tempb,tempc,tempd,tempe,temp1[8],temp2[8],temp3[8],temp4[8],temp5[8];
sp1.Format(_T("%08x"),H[0]);
sp2.Format(_T("%08x"),H[1]);
sp3.Format(_T("%08x"),H[2]);
sp4.Format(_T("%08x"),H[3]);
sp5.Format(_T("%08x"),H[4]);
{temp1[0]=sp1.GetAt(6);temp1[1]=sp1.GetAt(7);temp1[2]=sp1.GetAt(4);temp1[3]=sp1.GetAt(5);temp1[4]=sp1.GetAt(2);temp1[5]=sp1.GetAt(3);temp1[6]=sp1.GetAt(0);temp1[7]=sp1.GetAt(1);tempa=temp1[0]+temp1[1]+temp1[2]+temp1[3]+temp1[4]+temp1[5]+temp1[6]+temp1[7];}
{temp2[0]=sp2.GetAt(6);temp2[1]=sp2.GetAt(7);temp2[2]=sp2.GetAt(4);temp2[3]=sp2.GetAt(5);temp2[4]=sp2.GetAt(2);temp2[5]=sp2.GetAt(3);temp2[6]=sp2.GetAt(0);temp2[7]=sp2.GetAt(1);tempb=temp2[0]+temp2[1]+temp2[2]+temp2[3]+temp2[4]+temp2[5]+temp2[6]+temp2[7];}
{temp3[0]=sp3.GetAt(6);temp3[1]=sp3.GetAt(7);temp3[2]=sp3.GetAt(4);temp3[3]=sp3.GetAt(5);temp3[4]=sp3.GetAt(2);temp3[5]=sp3.GetAt(3);temp3[6]=sp3.GetAt(0);temp3[7]=sp3.GetAt(1);tempc=temp3[0]+temp3[1]+temp3[2]+temp3[3]+temp3[4]+temp3[5]+temp3[6]+temp3[7];}
{temp4[0]=sp4.GetAt(6);temp4[1]=sp4.GetAt(7);temp4[2]=sp4.GetAt(4);temp4[3]=sp4.GetAt(5);temp4[4]=sp4.GetAt(2);temp4[5]=sp4.GetAt(3);temp4[6]=sp4.GetAt(0);temp4[7]=sp4.GetAt(1);tempd=temp4[0]+temp4[1]+temp4[2]+temp4[3]+temp4[4]+temp4[5]+temp4[6]+temp4[7];}
{temp5[0]=sp5.GetAt(6);temp5[1]=sp5.GetAt(7);temp5[2]=sp5.GetAt(4);temp5[3]=sp5.GetAt(5);temp5[4]=sp5.GetAt(2);temp5[5]=sp5.GetAt(3);temp5[6]=sp5.GetAt(0);temp4[7]=sp4.GetAt(1);tempe=temp4[0]+temp4[1]+temp4[2]+temp4[3]+temp4[4]+temp4[5]+temp4[6]+temp4[7];}
sp=tempa+tempb+tempc+tempd+tempe;
m_msg3=sp;
UpdateData(FALSE);
}
UINT4 CMD5Dlg::count_file_sha()//////计算SHA明文二进制表示文本中数据有多少位
{ FILE *fp;
UINT4 c=0;
fp=fopen("SHA明文处理后二进制表示.txt","r+");
if((fp=fopen("SHA明文处理后二进制表示.txt","r+"))==NULL)
{AfxMessageBox("can not open file\n");exit(0);}
while( !feof(fp) )
{
fgetc(fp);
c++;
}
return c-1;
}
UINT4 CMD5Dlg::K(int t)
{
UINT4 temp;
if(t>=0&&t<=19)
return temp=0x5A827999;
else if(t>=20&&t<=39)
return temp=0x6ED9EBA1;
else if(t>=40&&t<=59)
return temp=0x8F1BBCDC;
else
return temp=0xCA62C1D6;
}
void CMD5Dlg::SHA_init()//初始化SHA的5个缓冲区初始值
{
H[0]=0x67452301;
H[1]=0xEFCDAB89;
H[2]=0x98BADCFE;
H[3]=0x10325476;
H[4]=0xC3D2E1F0;
}
int flag=0;//全局变量,用来切换说明文字的标志
void CMD5Dlg::OnDelare()
{
CString str1,str2;
CStdioFile file;
if(flag==0){
if(file.Open("MD5说明.txt",CFile::modeRead|CFile::typeText)==0){
str1="Create File MD5说明.txt failed!";
AfxMessageBox(str1);
return;
}
while(file.GetPosition()!=file.GetLength()){
file.ReadString(str1);
str2+=str1;
str2+="\r\n";
}
m_msg4.SetWindowText(str2);flag=1 ;}
else
{
if(file.Open("SHA说明.txt",CFile::modeRead|CFile::typeText)==0){
str1="Create File SHA说明.txt failed!";
AfxMessageBox(str1);
return;
}
while(file.GetPosition()!=file.GetLength()){
file.ReadString(str1);
str2+=str1;
str2+="\r\n";
}
m_msg4.SetWindowText(str2); flag=0; }
}
void CMD5Dlg::OnHELP()
{
AfxMessageBox("原则上此两种加密算法字长都在2的32次方即2的29次方个字符以内,但字符越长加密越慢(所以建议不要过长):\n操作时只需在明文框中输入你想要待加密的字符串,然后可以选择MD5或SHA按钮,\n 可以分别加密也可以同时显示出来,本加密系统采用十六进制显示。\n根据算法规则:\nMD5输出为固定32位;\nSHA1输出固定为40位;");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -