利用TEA算法实现加密密码文件生成与解密

<script language='javascript' src='http://adblog.hexun.com/show.aspx?sizetype=3&blogid=36955'></script>

下面代码利用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);  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值