TEA加密算法 多语言实现——C

在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;
   
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值