TEA加密算法及JAVA实现

       TEA算法由剑桥大学计算机实验室的David Wheeler和Roger Needham于1994年发明。它是一种分组密码算法,其明文密文块为64比特,密钥长度为128比特。TEA算法利用不断增加的Delta(黄金分割率)值作为变化,使得每轮的加密是不同,该加密算法的迭代次数可以改变,建议的迭代次数为32轮。[来自百度百科]

       Tea算法如图1所示

                                                                                             图1 tea算法原理图

       Key为128位密钥,密钥内容自行设定。需要加密的原文Content为8位一组的数据,每次加密2个32位数据,即8组数据原文。如果原文长度不够需要自行设计填充。加密轮数一般采用32轮(原文建议也是32轮)。Delta的取值为黄金分割点,数据值为0x9e3779b9。

代码如下:

///tea算法类
package main.mytea;

import java.util.Vector;

public class MyTea {

    static MyUtils myutils;

    /**
     * 参数为8字节的明文输入和16字节的密钥,输出8字节密文
     * @param MyTeaContent
     * @param MyTeakey
     * @return
     */
    public static byte[] MyTea_Encrypt(byte[] MyTeaContent, int ByteLen, byte[] MyTeakey) {
        int[] Value = myutils.byteToInt(MyTeaContent, ByteLen);
        int sum = 0;
        int i32Index = 0;
        int i32delta = 0x9e3779b9;
        int i32times = 32;


        int[] Key = myutils.byteToInt(MyTeakey, 16);
        int v0 = Value[0], v1 = Value[1];

        for(; i32Index < i32times; i32Index++) {
            sum += i32delta;
             v0 += ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]);
             v1 += ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]);
        }
        Value[0] = v0;
        Value[1] = v1;

        int len = ByteLen/4;

        return myutils.IntToBytes(Value, len);
    }

    /**
     * 参数为8字节的明文输入和16字节的密钥,输出8字节密文
     * @param MyTeaSecContent
     * @param MyTeakey
     * @return
     */
    public static byte[] MyTea_Decrypt(byte[] MyTeaSecContent, int ByteLen, byte[] MyTeakey) {
        System.out.println("解密函数:");
        int[] Value = myutils.byteToInt(MyTeaSecContent, ByteLen);
        int i32Index = 0;
        int i32delta=0x9e3779b9;
        int i32times = 32;
        int sum = i32delta * i32times;

        int[] Key = myutils.byteToInt(MyTeakey, 16);
        int v0 = Value[0], v1 = Value[1];

        for(; i32Index < i32times; i32Index++) {
            v1 -= ((v0 << 4) + Key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + Key[3]);
            v0 -= ((v1 << 4) + Key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + Key[1]);
            sum -= i32delta;
        }
        Value[0] = v0;
        Value[1] = v1;


        int len = ByteLen/4;

        return myutils.IntToBytes(Value, len);
    }

    public static void main(String[] args) {
        byte[] MyKeys = {0x01, 0x02, 0x03, 0x04,
                         0x05, 0x06, 0x07, 0x08,
                         0x09, 0x0A, 0x0B, 0x0C,
                         0x0D, 0x0E, 0x0F, 0x10};

        byte[] MyContent = {0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x01, 0x02, 0x03};
        System.out.println("原始数据为:");
        for(int i32Index = 0; i32Index < 8; i32Index++) {
            System.out.print("0x" + Integer.toHexString(MyContent[i32Index]) + " ");
        }
        System.out.println();

        System.out.println("密钥为:");
        for(int i32Index = 0; i32Index < 16; i32Index++) {
            System.out.print("0x" + Integer.toHexString(MyKeys[i32Index]) + " ");
        }
        System.out.println();

        byte[] retEnBytes = MyTea_Encrypt(MyContent, 8, MyKeys);
        System.out.println("加密后为:");
        for(int i32Index = 0; i32Index < 8; i32Index++) {
            System.out.print("0x" + Integer.toHexString(retEnBytes[i32Index]) + " ");
        }
        System.out.println();

        byte[] retDeBytes = MyTea_Decrypt(retEnBytes, 8, MyKeys);
        System.out.println("解密后为:");
        for(int i32Index = 0; i32Index < 8; i32Index++) {
            System.out.print("0x" + Integer.toHexString(retDeBytes[i32Index]) + " ");
        }

    }
}
///字节转换工具类
package main.mytea;

import java.util.Vector;

public class MyUtils {
    public static int[] byteToInt(byte[] Mybytes, int len) {
        int[] RetInt = new int[len/4];
        int i32Count = len/4;
        int i32Index = 0;

        for(int i32Increase = 0; i32Increase<i32Count; i32Increase++) {
            RetInt[i32Increase] = transform(Mybytes[3 + i32Index]) | transform(Mybytes[2 + i32Index]) << 8 | transform(Mybytes[1 + i32Index]) << 16 | transform(Mybytes[0 + i32Index]) << 24;
            i32Index += 4;
        }

        return RetInt;
    }

    public static byte[] IntToBytes(int[] MyInts, int len) {
        byte[] RetBytes = new byte[len * 4];

        for(int i32Index = 0; i32Index < len; i32Index++) {
            RetBytes[0 + i32Index * 4] = (byte) ((MyInts[i32Index]>>24)&0xFF);
            RetBytes[1 + i32Index * 4]  = (byte) ((MyInts[i32Index]>>16)&0xFF);
            RetBytes[2 + i32Index * 4] = (byte) ((MyInts[i32Index]>>8)&0xFF);
            RetBytes[3 + i32Index * 4] = (byte) (MyInts[i32Index]&0xFF);
        }
        return RetBytes;
    }

    private static int transform(byte temp) {
        int tempInt = (int)temp;
        if(tempInt < 0) {
            tempInt += 256;
        }
        return tempInt;
    }
}

       tea算法是一种对称加密算法,对称加密算法指加密和解密使用同一组密钥。优点是算法公开,计算量小,速度快,效率高。确定:密钥管理和分发不够安全,发送前双方需要商定好密钥,如果其中一方密钥被泄露,则加密信息也就不安全了。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值