⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idea.cpp

📁 IDEA加密解密功能
💻 CPP
字号:
#include <stdio.h> 
#include <iostream> 
#include <stdlib.h> 
#include <cassert> 
#include <string> 
#define NUMSUBKEYS 52 
#define NUMROUNDS 8 
#define MAXINPUTSIZE 32 
unsigned int TWOPOWER16 = 65536; 
unsigned int TWOPOWER161 = 65537; 
unsigned int inputsize; 
unsigned short esubkeys[NUMSUBKEYS]; 
unsigned short dsubkeys[NUMSUBKEYS]; 
unsigned int origkeyint[4]; 
unsigned char origkeychar[17]; 
void printHex (unsigned char* start) 
{ 
unsigned int* val = (unsigned int*)start; 
int times = inputsize/4; 
cout <<endl; 
for (int i=0; i<times;i++) { 
printf ("\t\tbits %03d to %03d: 0x%08x\n",(32*i)+1,8*4*(i+1),*val); 
start+=4; 
val = (unsigned int*)start; 
} 
} 
void runIdea(unsigned char* msg, unsigned char* outmsg,unsigned short* keysbit16,int writeflag) 
{ 
unsigned short x1,x2,x3,x4; 
unsigned short y1,y2,y3,y4; 
unsigned short x5,x6,x7,x8,x9,x10; 
unsigned short x11,x12,x13,x14; 
unsigned short xtemp; 
unsigned int writeint; 
unsigned short* msgbit16 = (unsigned short*) msg; 
x1 = *msgbit16++; 
x2 = *msgbit16++; 
x3 = *msgbit16++; 
x4 = *msgbit16++; 
int tst=9; 
for (int i=0; i<NUMROUNDS;i++) 
{ 
if (i==tst ) 
cout << "STEP 1: x1 is " << x1 << ", key is " << *keysbit16 << endl; 
x1 = (x1* (*keysbit16++)) % TWOPOWER161; 
if (i==tst ) 
cout << "\tAfter mul, x1 is " << x1 << endl; 
if (i==tst ) 
cout << "STEP 2: x2 is " << x2 << ", key is " << *keysbit16 << endl; 
x2 = (x2 + *keysbit16++) % TWOPOWER16; 
if (i==tst ) 
cout << "\tAfter add, x2 is " << x2 << endl; 
if (i==tst ) 
cout << "STEP 3: x3 is " << x3 << ", key is " << *keysbit16 << endl; 
x3 = (x3 + *keysbit16++) % TWOPOWER16; 
if (i==tst ) 
cout << "\tAfter add, x3 is " << x3 << endl; 
if (i==tst ) 
cout << "STEP 4: x4 is " << x4 << ", key is " << *keysbit16 << endl; 
x4 = (x4* (*keysbit16++)) % TWOPOWER161; 
if (i==tst ) 
cout << "\tAfter mul, x4 is " << x4 << endl; 
if (i==tst) 
cout << "STEP 5: x3 is " << x3 << ", x1 is " << x1 << endl; 
x5 = x1^x3; 
if (i==tst) 
cout << "\tAfter XOR, x5 is " << x5 << endl; 
if (i==tst) 
cout << "STEP 6(outorder): x2 is " << x2 << ", x4 is " << x4 << endl; 
x6 = x2^x4; 
if (i==tst) 
cout << "\tAfter XOR, x6 is " << x6 << endl; 
if (i==tst) 
cout << "STEP 7(outorder): x5 is " << x5 << ", key is " << *keysbit16 << endl; 
x7 = (x5* (*keysbit16++)) % TWOPOWER161; 
if (i==tst) 
cout << "\tAfter mul, x7 is " << x7 << endl; 
if (i==tst) 
cout << "STEP 8: x6 is " << x6 << ", x7 is " << x7 << endl; 
x8 = (x6+x7) % TWOPOWER16; 
if (i==tst) 
cout << "\tAfter ADD, x8 is " << x8 << endl; 
if (i==tst) 
cout << "STEP 9: x8 is " << x8 << ", key is " << *keysbit16 << endl; 
x9 = (x8* (*keysbit16++)) % TWOPOWER161; 
if (i==tst) 
cout << "\tAfter mul, x9 is " << x9 << endl; 
if (i==tst) 
cout << "STEP 10: x7 is " << x7 << ", x9 is " << x9 << endl; 
x10 = (x7+x9) % TWOPOWER16; 
if (i==tst) 
cout << "\tAfter add, x10 is " << x10 << endl; 
x11=x1^x9; 
x12=x3^x9; 
x13=x2^x10; 
x14=x4^x10; 
if (i==tst ) { 
cout << "\tSTEP11: After XOR, x11 is " << x11 << endl; 
cout << "\tSTEP12: After XOR, x12(after swap) is " << x12 << endl; 
cout << "\tStep13: After XOR, x13(after swap) is " << x13 << endl; 
cout << "\tStep14: After XOR, x14 is " << x14 << endl; 
} 
x1=x11; 
x2=x12; 
x3=x13; 
x4=x14; 
if (writeflag==1) { 
printf ("ROUND %d:\n", i+1); 
writeint = (x1<<16) + x2; 
printf("\tBits 1 to 32 0x%08x\n",writeint); 
writeint = (x3<<16) + x4; 
printf("\tBits 33 to 64 0x%08x\n\n",writeint); 
} 
} 
y1 = (x11 * (*keysbit16++)) % TWOPOWER161; 
y3 = (x13 + *keysbit16++) % TWOPOWER16; 
y2 = (x12 + *keysbit16++) %TWOPOWER16; 
y4 = (x14 * (*keysbit16)) % TWOPOWER161; 
msgbit16=(unsigned short*)outmsg; 
*msgbit16++ = y1; 
*msgbit16++ = y3; 
*msgbit16++ = y2; 
*msgbit16 = y4; 
if (writeflag==1) { 
unsigned int tempint; 
printf ("AFTER OUTPUT TRANSFORMATION AND SWAP:\n"); 
msgbit16=(unsigned short*)outmsg; 
tempint = (y1 <<16) + y3; 
printf ("\tBits 1 to 32 0x%08x\n",tempint); 
tempint = (y2 <<16) + y4; 
printf ("\tBits 33 to 64 0x%08x\n",tempint); 
} 
} 
void encrypt (unsigned char* msg,unsigned char* outmsg,int writeflag) 
{ 
int blocks = inputsize/8; 
unsigned char* inptr=msg; 
unsigned char* outptr=outmsg; 
for(int i=0;i<blocks;i++) { 
if (writeflag==1) { 
printf ("Results for Data Block %d\n", i+1); 
printf ("=======================\n\n"); 
} 
runIdea(inptr,outptr,esubkeys,writeflag); 
inptr+=8; 
outptr+=8; 
} 
} 
void decrypt (unsigned char* msg,unsigned char* outmsg,int writeflag) 
{ 
int blocks = inputsize/8; 
unsigned char* inptr=msg; 
unsigned char* outptr=outmsg; 
for(int i=0;i<blocks;i++) { 
if (writeflag==1) { 
printf ("Results for Data Block %d\n", i+1); 
printf ("=======================\n\n"); 
} 
runIdea(inptr,outptr,dsubkeys,writeflag); 
inptr+=8; 
outptr+=8; 
} 
} 
short inv(unsigned short b) 
{ 
if (b==0 || b==1) 
return b; 
int a = 65536+1; // 2^16 + 1 
int g0 = a; 
int g1 = b; 
int v0 = 0; 
int v1 = 1; 
int savev0; 
int q; 
int rem; 
int numloops = 0; 
while (g1 != 0) { 
numloops++; 
q = g0/g1; 
rem = g0 % g1; 
g0=g1; 
g1 = rem; 
savev0=v0; 
v0 = v1; 
v1 = savev0 - (v1*q); 
} 
assert (g0==1); 
if (v0 >1) 
return v0; 
else 
return 1+v0; 
} 
void printOrigKey() 
{ 
printf ("Original Key in text: %s\n",origkeychar); 
printf ("\tOriginal Key 1st 32bits: 0x%08x\n",origkeyint[0]); 
printf ("\tOriginal Key 2nd 32bits: 0x%08x\n",origkeyint[1]); 
printf ("\tOriginal Key 3rd 32bits: 0x%08x\n",origkeyint[2]); 
printf ("\tOriginal Key 4th 32bits: 0x%08x\n",origkeyint[3]); 
} 
void printKeys() 
{ 
int count=1; 
cout << "\n\n***** ENCRYPTION AND DECRYPTION SUBKEY SUMMARY *****" <<endl; 
cout << endl << "All Subkeys are 16 bits." <<endl<<endl; 
printOrigKey(); 
for (int k=0; k<NUMSUBKEYS;k++) { 
if (k%6 ==0) { 
cout <<"\nEncryption Subkeys Round " << count ; 
cout <<" Decryption Subkeys Round " << count << endl; 
count++; 
} 
printf (" subkey %02d 0x%08x",k,esubkeys[k]); 
printf (" subkey %02d 0x%08x\n",k,dsubkeys[k]); 

} 


}
void calcDKeys () 
{ 
for (int i=0;i<NUMSUBKEYS;i+=6) { 
dsubkeys[i] = inv(esubkeys[48-i]); 
dsubkeys[i+3] = inv(esubkeys[48-i+3]); 
} 
dsubkeys[1] = -1 * esubkeys[49]; 
dsubkeys[2] = -1 * esubkeys[50]; 
for (int i =7; i<NUMSUBKEYS;i+=6) { 
dsubkeys[i] = -1 * esubkeys[51-i]; 
dsubkeys[i+1] = -1 * esubkeys[50-i]; 
} 
dsubkeys[49] = -1 * esubkeys[1]; 
dsubkeys[50] = -1 * esubkeys[2]; 
for (int i=4; i< (NUMSUBKEYS) ; i+=6) { 
dsubkeys[i] = esubkeys[50-i]; 
dsubkeys[i+1] = esubkeys[51-i]; 
} 
int count=1; 
for (int k=0; k<NUMSUBKEYS;k++) { 
if (k%6 ==0) { 
count++; 
} 
} 
} 
void calcEKeys(unsigned char* userkey) 
{ 
int firstbyte = 0; 
for (int j=0; j<8;j++) { 
esubkeys[j] = (userkey[firstbyte] <<8) + userkey[firstbyte+1]; 
firstbyte = firstbyte+2; 
} 
for (int f=8; f<NUMSUBKEYS-4;f+=8) { 
for (int n=0;n<6;n++) { 
esubkeys[f+n] = (short) ((esubkeys[f+n-7] <<9) | (esubkeys[f+n-6] >>7)); 
} 
esubkeys[f+6] = (short) ((esubkeys[f+6-7] <<9) | (esubkeys[f+6-6-8]>>7)); 
esubkeys[f+7] = (short) ((esubkeys[f+7-7-8] <<9) | (esubkeys[f+7-6-8]>>7)); 
} 
esubkeys[NUMSUBKEYS-4] = (short) ((esubkeys[NUMSUBKEYS-4-7] <<9) | (esubkeys[NUMSUBKEYS-4-6] >>7)); 
esubkeys[NUMSUBKEYS-4+1] = (short) ((esubkeys[NUMSUBKEYS-4+1-7] <<9) | (esubkeys[NUMSUBKEYS-4+1-6] >>7)); 
esubkeys[NUMSUBKEYS-4+2] = (short) ((esubkeys[NUMSUBKEYS-4+2-7] <<9) | (esubkeys[NUMSUBKEYS-4+2-6] >>7)); 
esubkeys[NUMSUBKEYS-4+3] = (short) ((esubkeys[NUMSUBKEYS-4+3-7] <<9) | (esubkeys[NUMSUBKEYS-4+3-6] >>7)); 
int count=1; 
for (int k=0; k<NUMSUBKEYS;k++) { 
if (k%6 ==0) { 
count++; 
} 
} 
} 
void promptWelcome() 
{ 
if (sizeof(unsigned short)!=2){ 
cout <<" size of unsigned short is not 2 bytes. Please run on flop.engr.orst.edu or on another machine. This program needs 2 bytes for unsigned short to simulate the 16-bit subkeys" <<endl; 
exit(0); 
} 
cout << endl <<endl; 
cout << "************ WELCOME ***************" <<endl; 
cout << "This is Luo yueping's program illustrating " <<endl; 
cout << "IDEA (International Data Encryption Algorithm)" <<endl; 
cout << "************************************" <<endl; 
cout << endl <<endl; 
}
void promptForText(unsigned char* ptext,int encryptionflag) 
{ 
std::string str; 
if (encryptionflag ==1) 
cout << endl << "************ ENCRYPTION OF PLAINTEXT **********" <<endl<<endl; 
else 
cout << endl << "************ DECRYPTION OF CIPHERTEXT **********" <<endl<<endl; 
while (1) { 
cout << "Data block size is 8 bits. " <<endl; 
cout << "Therefore, please enter 8, 16, 24, 32 characters" <<endl; 
if (encryptionflag ==1) 
cout << "Your Plaintext input: "; 
else 
cout << "Your Ciphertext input: "; 

getline(cin, str); 

if ((str.size() == 8) || (str.size()==16) || (str.size()==24) || (str.size()==32)) 
break; 
else 
cout <<endl<< "ERROR: input was " << str.size()<< " instead of 8,16,24,32 chars. Try Again" <<endl <<endl; 
} 
inputsize = str.size(); 

for (int i=0;i<str.size();i++) { 
ptext[i] = str[i]; 
} 
ptext[inputsize]='\0'; 
} 
void promptForKey() 
{ 
cout << "IDEA takes in a 128-bit key. " <<endl; 
cout << "User will enter 16 alphanumeric characters" <<endl; 
cout << "These 16 alphanumeric characters will be "<< endl; 
cout << "converted to a 128-bit key. " <<endl; 
cout << " (16 char * 8bits = 128 bits)" <<endl; 
cout << endl <<endl; 
std::string str; 
while (1) { 
cout << "Please enter 16 alphanumberic characters, then press enter" <<endl; 
getline(cin, str); 
if (str.size() == 16) 
break; 
else 
cout <<endl<< "ERROR: That was not 16 alphanumeric chars. Try Again" <<endl <<endl; 
} 
for (int i=0;i<16;i++) { 
origkeychar[i] = str[i]; 
} 
origkeychar[16]='\0'; 
cout << endl << "Thank you."<<endl ; 
int firstbyte = 0; 
for (int j=0; j<4;j++) { 
origkeyint[j] = (origkeychar[firstbyte] <<24) + (origkeychar[firstbyte+1]<<16) 
+ (origkeychar[firstbyte+2]<<8) + (origkeychar[firstbyte+3]); 
firstbyte = firstbyte+4; 
} 
printOrigKey(); 
} 
void printPlainTextSummary(unsigned char* plaintext) 
{ 
plaintext[inputsize]='\0'; 
printf ("\n\nYour Plaintext\n"); 
printf ("=================================\n"); 
printf ("Plaintext in ASCII: \"%s\"\n",plaintext); 
printf ("Plaintext in Hex:"); 
printHex(plaintext); 
} 
void printCipherTextSummary(unsigned char* ciphertext) 
{ 
ciphertext[inputsize] = '\0'; 
printf ("\n\nThe Resulting Ciphertext\n"); 
printf ("=================================\n"); 
printf ("Ciphertext in Hex:"); 
printHex(ciphertext); 
} 
void printDecipherTextSummary(unsigned char* decipheredtext) 
{ 
decipheredtext[inputsize] = '\0'; 
printf ("\n\nDecrypt the Ciphertext\n"); 
printf ("=================================\n"); 
printf ("Decrypted text in ASCII: \"%s\"\n",decipheredtext); 
printf ("Decrypted text in Hex:"); 
printHex(decipheredtext); 
} 
int main() 
{ 

promptWelcome(); 
promptForKey(); 
calcEKeys(origkeychar); 
calcDKeys(); 
std::string str; 
unsigned char ciphertext[MAXINPUTSIZE+1]; 
unsigned char decipheredtext[MAXINPUTSIZE+1]; 
unsigned char plaintext[MAXINPUTSIZE+1]; 
ciphertext[MAXINPUTSIZE] = '\0'; 
decipheredtext[MAXINPUTSIZE] = '\0'; 
unsigned int myint; 
unsigned int* intptr; 
while (1) { 
cout << endl <<endl << "MAIN MENU" <<endl; 
cout << "=========" <<endl; 
cout << "Press 1 to print all Encryption/decryption keys " <<endl; 
cout << "Press 2 to encrypt plaintext with intermediate results" <<endl; 
cout << "Press 3 to decipher ciphertext with intermediate results" <<endl; 
cout << "Press 4 to encrypt, then decrypt " 
<< "(No intermediate results shown)" <<endl; 
cout << "Press 5 to quit " <<endl<<endl; 
cout << "Your choice: " ; 
getline(cin, str); 
if (str[0] == '1') 
printKeys(); 
else if (str[0]=='2') { 
promptForText(plaintext,1); 
encrypt(plaintext,ciphertext,1); 
printCipherTextSummary(ciphertext); 
} 
else if (str[0]=='3') { 
promptForText(ciphertext,0); 
decrypt(ciphertext,decipheredtext,1); 
printDecipherTextSummary(decipheredtext); 
} 
else if(str[0]=='4') { 
promptForText(plaintext,1); 
encrypt(plaintext,ciphertext,0); 
decrypt(ciphertext,decipheredtext,0); 
printPlainTextSummary(plaintext); 
printCipherTextSummary(ciphertext); 
printDecipherTextSummary(decipheredtext); 
} 
else if(str[0]=='5') { 
cout << "****** Exiting IDEA program. Good bye. " <<endl<<endl; 
exit(0); 
} 
else { 
cout <<"Error: Invalid input" <<endl; 
} 

} 

return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -