下面代码利用TEA算法实现加密密码文件生成与解密,一共有五个文件,分别是main.cpp, encode.h,encode.cpp,encodepwfile.h,encodepwfile.cpp。分别将下面的代码拷贝到对应文件中,然后到dev-c++建个工程,把文件添加到工程中编译即可。
//mail.cpp
#include <cstdlib>
#include "encodepwfile.h"
#include "encode.h"
int main(int argc, char *argv[])
{
//testEncodeBuf();
if (4 == argc)
{
writePWfile(argv[1], argv[2], argv[3]);
outputPW(argv[1]);
}
else if (2 == argc)
{
outputPW(argv[1]);
}
else
{
char* filename = "d://test.dat";
writePWfile(filename, "nihao", "shi");
outputPW(filename);
}
system("PAUSE");
return EXIT_SUCCESS;
}
//encode.h file
#ifndef __ENCODE__H__
#define __ENCODE__H__
//Helper function
void printStr(char* s, int n);
//Encode function
void DecryptTEA(unsigned long *firstChunk, unsigned long *secondChunk);
void EncryptTea(unsigned long *firstChunk, unsigned long *secondChunk);
void DecryptBuffer(char* buffer, int size);
void EncryptBuffer(char* buffer, int size);
//Testing function
void TestEncodeTea();
void testEncodeBuf();
#endif
//encode.cpp file
#include <iostream>
#include "encode.h"
//Helper function
void printStr(char* s, int n)
{
for (int i = 0; i < n; i++)
{
printf("%c",s[i]);
}
printf("/n");
}
//Encode function
void DecryptTEA(unsigned long *firstChunk, unsigned long *secondChunk)
{
unsigned long *key = (unsigned long *)"ABcde34gdbbddddd";
unsigned long sum = 0;
unsigned long y = *firstChunk;
unsigned long z = *secondChunk;
unsigned long delta = 0x9e3779b9;
sum = delta << 5;
for(int i=0; i<32; i++)
{
z -= (y << 4) + key[2] ^ y + sum ^ (y >> 5) + key[3];
y -= (z << 4) + key[0] ^ z + sum ^ (z >> 5) + key[1];
sum -= delta;
}
*firstChunk = y;
*secondChunk = z;
return;
}
void EncryptTea(unsigned long *firstChunk, unsigned long *secondChunk)
{
unsigned long y=*firstChunk;
unsigned long z=*secondChunk;
unsigned long sum=0;
unsigned long *key = (unsigned long *)"ABcde34gdbbddddd";
unsigned long delta=0x9e3779b9;
for (int i=0; i < 32; i++)
{
sum += delta;
y += ((z<<4) + key[0]) ^ (z + sum) ^ ((z>>5) + key[1]);
z += ((y<<4) + key[2]) ^ (y + sum) ^ ((y>>5) + key[3]);
}
*firstChunk = y;
*secondChunk = z;
}
void DecryptBuffer(char* buffer, int size)
{
char *p = buffer;
printf("Before DecryptBuffer/n");
printStr(buffer, size);
while (p < buffer + size)
{
DecryptTEA((unsigned long *)p, (unsigned long *)(p + sizeof(unsigned long)));
p += sizeof(unsigned long) * 2;
}
printf("After DecryptBuffer/n");
printStr(buffer, size);
}
void EncryptBuffer(char* buffer, int size)
{
char *p = buffer;
printf("Before EncryptBuffer/n");
printStr(buffer, size);
while (p < buffer + size)
{
EncryptTea((unsigned long *)p, (unsigned long *)(p + sizeof(unsigned long)));
p += sizeof(unsigned long) * 2;
}
printf("After EncryptBuffer/n");
printStr(buffer, size);
}
//Testing function
void TestEncodeTea()
{
char s[32] = "aceplusaaa";
EncryptTea((unsigned long *)s, (unsigned long *)(s + sizeof(unsigned long)));
printStr(s, 32);
DecryptTEA((unsigned long *)s, (unsigned long *)(s + sizeof(unsigned long)));
printStr(s, 32);
}
void testEncodeBuf()
{
char str[32] = "wo/0shi";
printStr(str, 32);
EncryptBuffer(str, 32);
printf("-----After EncryptBuffer------------------");
printStr(str, 32);
DecryptBuffer(str,32);
printf("-----After DecryptBuffer------------------");
printStr(str, 32);
}
//encodepwfile.cpp file
#ifndef __ENCODE_PW_FILE__H__
#define __ENCODE_PW_FILE__H__
void outputPW(char* filename);
void writePWfile(char* filename, char* userid, char* password);
#endif
//encodepwfile.cpp file
#include <iostream>
#include "encode.h"
#include "encodepwfile.h"
#define NULL_CHAR '/0'
#define EXIT_ERROR -1
#define DATASIZE 32
//Help function
void trimBlanks(char *buffer)
{
char *currentLocation = buffer + strlen(buffer) - 1;
while ((currentLocation >= buffer) && (' ' == *currentLocation))
{
*currentLocation = NULL_CHAR;
currentLocation--;
}
return;
}
void EncryptUseridPassword(char *encryptedData, int len, char *userid, char *password)
{
//Generate decryte data
char* decryptedData = new char[len];
memset(decryptedData, NULL_CHAR, len);
memcpy(decryptedData, userid, strlen(userid));
memcpy(decryptedData + strlen(userid) + 1, password, strlen(password));
EncryptBuffer(decryptedData, len);
memcpy(encryptedData, decryptedData, len);
delete decryptedData;
return;
}
void DecryptUseridPassword(char *encryptedData, int len, char *userid, char *password)
{
DecryptBuffer(encryptedData, len);
strcpy(userid, encryptedData);
trimBlanks(userid);
char* p = encryptedData + strlen(encryptedData) + 1;
strcpy(password, p);
trimBlanks(password);
}
void GetEncryptedDataFromFile(char *filename, char *encryptedData, int bufferSize)
{
FILE *f_ptr = NULL;
f_ptr = fopen(filename, "r");
if (NULL == f_ptr )
{
printf("No such a file. Please input a valid file!");
exit(EXIT_ERROR);
}
memset(encryptedData, NULL_CHAR, bufferSize);
fgets(encryptedData, bufferSize, f_ptr);
if (EOF == fclose(f_ptr))
{
printf("An error occurred closing the file Continuing anyway...");
}
return;
}
void outputPW(char* filename)
{
char encryptedData[DATASIZE];
char userid[DATASIZE];
char password[DATASIZE];
GetEncryptedDataFromFile(filename, encryptedData, sizeof(encryptedData));
DecryptUseridPassword(encryptedData, sizeof(encryptedData), userid, password);
printf("This is the userid:%s, password:%s from %s./n/n/n", userid, password, filename);
}
void writePWfile(char* filename, char* userid, char* password)
{
FILE *f_ptr = NULL;
f_ptr = fopen(filename, "wb+");
if (NULL == f_ptr )
{
printf("No such a file. Please input a valid file!/n");
system("PAUSE");
exit(EXIT_ERROR);
}
char encryptedData[DATASIZE];
EncryptUseridPassword(encryptedData, sizeof(encryptedData), userid, password);
fwrite(encryptedData, 1, sizeof(encryptedData), f_ptr);
if (EOF == fclose(f_ptr))
{
printf("An error occurred closing the file Continuing anyway...");
system("PAUSE");
}
printf("Write the userid:%s, password:%s to %s successfully!/n/n/n", userid, password, filename);
}