www.pudn.com > CipherSystem.rar > IDEACipherSystem.cpp


#include "stdafx.h" 
#include "IDEACipherSystem.h" 
 
////////////////////////////////////////////////////////////////////////////////////// 
////////////////        Implement class of IDEAEncryptSystem        ////////////////// 
////////////////////////////////////////////////////////////////////////////////////// 
 
int IDEACipherSystem::AthwartMul(int x) 
{ 
	if (x == 0 || x == 65536) return 65536; 
	unsigned int i; 
	for (i=1; ; i += 65537) 
	{ 
		if (i % x == 0) return i / x; 
	} 
} 
 
int IDEACipherSystem::AthwartAdd(int x) 
{ 
	int i; 
	for (i=0; ; i += MOD_ADD_2_16) 
	{ 
		int temp = i - x; 
		if (temp >= 0) return temp; 
	} 
	return 0; 
} 
 
void IDEACipherSystem::SubKeyModifyForDecrypt() 
{ 
	int tempArray[52]; 
	int displace = 0; 
	int i; 
	for (i=0; i<4; i++) tempArray[displace++] = subKeyStored16bit[48 + i]; 
	for (i=42; i>=0; i-=6) 
	{ 
		tempArray[displace++] = subKeyStored16bit[i + 4]; 
		tempArray[displace++] = subKeyStored16bit[i + 5]; 
		int j; 
		for (j=0; j<4; j++) tempArray[displace++] = subKeyStored16bit[i + j]; 
	} 
	for (i=0; i<52; i++) 
	{ 
		int select = i % 6; 
		switch (select) 
		{ 
		case 0: ; 
		case 3: subKeyStored16bit[i] = AthwartMul(tempArray[i]); 
			break; 
		case 1: ; 
		case 2: subKeyStored16bit[i] = AthwartAdd(tempArray[i]); 
			break; 
		default: subKeyStored16bit[i] = tempArray[i]; 
		} 
	} 
	for (i=6; i<=42; i+=6) 
	{ 
		int temp = subKeyStored16bit[i + 1]; 
		subKeyStored16bit[i + 1] = subKeyStored16bit[i + 2]; 
		subKeyStored16bit[i + 2] = temp; 
	} 
} 
 
void IDEACipherSystem::Decrypt() 
{ 
	binary_stream plainBinaryStream; 
	binary_stream cipherBinaryStream; 
	int i; 
	for (i=0; i<(int)text.length(); i++) 
	{ 
		if (text[i] == '0') cipherBinaryStream.push_back(false); 
		else cipherBinaryStream.push_back(true); 
	} 
 
	binary_stream cipherText64bit; 
	binary_stream keyStream128bit; 
	int textDisplace = 0; 
	int keyWordDisplace = 0; 
 
	while (textDisplace < (int)cipherBinaryStream.size()) 
	{ 
		cipherText64bit.clear(); 
		keyStream128bit.clear(); 
 
		for (i=0; i<64; i++) cipherText64bit.push_back(cipherBinaryStream[textDisplace++]); 
		for (i=0; i<128; i++)  
			keyStream128bit.push_back(keyBinaryStream[keyWordDisplace++ % (int)keyBinaryStream.size()]); 
		SubKeyCreation(keyStream128bit); 
		SubKeyModifyForDecrypt(); 
		SetSubDataBlock(cipherText64bit); 
		Iterative(); 
 
		for (i=0; i<4; i++) 
		{ 
			IntToBinaryStream(subDataBlock16bit[i], cipherText64bit); 
			int j; 
			for (j=0; j<(int)cipherText64bit.size(); j++) plainBinaryStream.push_back(cipherText64bit[j]); 
		} 
	} 
	BinaryToChar(plainBinaryStream, text); 
} 
 
void IDEACipherSystem::Encrypt() 
{ 
	binary_stream textReserve; 
	CharToBinary(text, textReserve); 
 
	binary_stream plainText64bit; 
	binary_stream cipherBinaryStream; 
	binary_stream keyStream128bit; 
	int textDisplace = 0; 
	int keyDisplace = 0; 
 
	while (textDisplace < (int)textReserve.size()) 
	{ 
		plainText64bit.clear(); 
		keyStream128bit.clear(); 
 
		int i; 
		for (i=0; i<64; i++) plainText64bit.push_back(textReserve[textDisplace++]); 
		for (i=0; i<128; i++)  
			keyStream128bit.push_back(keyBinaryStream[keyDisplace++ % (int)keyBinaryStream.size()]); 
 
		SubKeyCreation(keyStream128bit); 
		SetSubDataBlock(plainText64bit); 
		Iterative(); 
 
		for (i=0; i<4; i++) 
		{ 
			IntToBinaryStream(subDataBlock16bit[i], plainText64bit); 
			int j; 
			for (j=0; j<(int)plainText64bit.size(); j++)  
			{ 
				cipherBinaryStream.push_back(plainText64bit[j]); 
			} 
		} 
	} 
	text.erase(); 
	text.reserve(int(cipherBinaryStream.size())); 
	int i; 
	for (i=0; i<(int)cipherBinaryStream.size(); i++) text += char(int(cipherBinaryStream[i]) + 48); 
} 
 
 
void IDEACipherSystem::GetKey(const string& str) 
{ 
	CharToBinary(str, keyBinaryStream); 
} 
 
void IDEACipherSystem::GetData(const string& input) 
{ 
	text = input; 
	int left = (int)text.length() % 8;             //确保输入是8的倍数 
	int i; 
	if (left != 0) left = 8 - left; 
	for (i=0; i> i)) binaryStream16bit.push_back(true); 
		else binaryStream16bit.push_back(false); 
	} 
	return; 
} 
 
int IDEACipherSystem::BinaryStreamToInt(const binary_stream& binaryStream16bit) 
{ 
	int result = 0; 
	int power = 1; 
	int i; 
	for (i=15; i>=0; i--) 
	{ 
		result += (int)binaryStream16bit[i] * power; 
		power *= 2; 
	} 
	return result; 
}