在linux下编译 在32位以及64位系统需进行改变,主要是long型在32位gcc上为32bit 而在64位gcc 下为64bit 进行修改即可运行。一下为在64bit centos 下进行
python版本只是对c语言版本进行了简单封装实现。
/***********************************************************************************************/
/******************************/
/***2014年12月22日**************/
#include </usr/include/python2.7/Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
/**
* 基本的8bytes数据块加密
* param unsigned long v[2] 8bytes data
* param const unsigned long k[4 16bytes key
* return unsigned long v[2] 8bytes data
* unsigned long rounds 加密轮数可选16或32
*/
void inner_encrypt(unsigned int v[2],const unsigned int k[4],unsigned int rounds){
register unsigned int
y=v[0],z=v[1],sum=0,delta=0x9E3779B9,
a=k[0],b=k[1],c=k[2],d=k[3];
while(rounds-->0){
sum+=delta;
y+=((z<<4)+a)^(z+sum)^((z>>5)+b);
z+=((y<<4)+c)^(y+sum)^((y>>5)+d);
}
v[0]=y;
v[1]=z;
}
/**
* 基本的8bytes数据块解密
* param unsigned long v[2] 8bytes data
* param const unsigned long k[4 16bytes key
* return unsigned long v[2] 8bytes data
* unsigned long rounds 加密轮数可选16或32
*/
void inner_decrypt(unsigned int v[2],const unsigned int k[4],unsigned int rounds){
register unsigned int
y=v[0],z=v[1],delta=0x9E3779B9,
a=k[0],b=k[1],c=k[2],d=k[3],sum;
if(rounds==32)
sum=0xc6ef3720;
else if(rounds==16)
sum=0xe3779b90;
else return;
while(rounds-->0){
z-=((y<<4)+c)^(y+sum)^((y>>5)+d);
y-=((z<<4)+a)^(z+sum)^((z>>5)+b);
sum-=delta;
}
v[0]=y;
v[1]=z;
}
/**add the flag to encrypted content
* char* unflag: the encrypted content without flag
* int len:the len of the encrypted content
*/
char *flag(char *unflag,int len)
{
char flag[9]="}}}}}}}}";
char *flag_out=(char*)malloc(len+8);
memcpy(flag_out,flag,8);
memcpy(flag_out+8,unflag,len);
free(unflag);
return flag_out;
}
/**remove the flagwords from the content
* char* flag content contains flag
* int len: length of the content
*/
char *unflag(const char *flag,int len)
{
char *unflag=(char*)malloc(len-8);
memcpy(unflag,flag+8,len-9);
free((void*)flag);
return unflag;
}
/**transform byte to hexString
* encrypted content
* length of the content the value is the length of the encrypted
*/
char* byte2hex(char* encrypt,int len){
int i=0;
int j;
char* out=(char*)malloc((2*len+1)*sizeof(char));
for(j=0;i<len;i++,j+=2)
{
char tempchar1='0'+((encrypt[i]&0xf0) >> 4);
char tempchar2='0'+((encrypt[i]&0x0f));
if(tempchar1 > '9')
switch(tempchar1-'9'){
case 1: tempchar1='A'; break;
case 2: tempchar1='B'; break;
case 3: tempchar1='C'; break;
case 4: tempchar1='D'; break;
TEA加密算法 多语言实现——C
最新推荐文章于 2024-05-05 23:30:15 发布