分布式服务器框架之Server.Core库中实现 XXTEA分组加密算法

1 XXTEA分组加密算法介绍

        在密码学中,微型加密算法(Tiny Encryption Algorithm,TEA)是一种易于描述和执行的块密码,通常只需要很少的代码就可实现。其设计者是剑桥大学计算机实验室的大卫·惠勒与罗杰·尼达姆。这项技术最初于1994年提交给鲁汶的快速软件加密的研讨会上,并在该研讨会上演讲中首次发表。

在给出的代码中:加密使用的数据为2个32位无符号整数,密钥为4个32位无符号整数即密钥长度为128位

XTEA是TEA的升级版,增加了更多的密钥表,移位和异或操作等等。

XXTEA,又称Corrected Block TEA,又是XTEA的升级版 。目前XXTEA还暂未被破解。

        XXTEA加密算法比较复杂,如果要彻底理解得把引用链接里给出的公式吃透差不多才能理解,这里面又涉及密码学和数学的很多知识,读者实在看不懂也没关系,直接拿去用就行,知道怎么用就可以了。

2 代码实现

XXTEAUtil.cs代码实现

using System;
using System.Collections.Generic;
using System.Text;

namespace Servers.Core.Utils
{
    public static class XXTEAUtil
    {
        public static readonly byte[] SoketKey = { 102, 120, 22, 24, 88, 6, 119, 88 };
        
        private static int RightMove(this int value, int pos)
        {
            if (pos != 0)
            {
                // int.MaxValue = 0x7FFFFFFF 整数最大值
                int mask = int.MaxValue;

                //无符号整数最高位不表示正负但操作数还是有符号的,有符号数右移1位,正数时高位补0,负数时高位补1
                value = value >> 1;

                //和整数最大值进行逻辑与运算,运算后的结果为忽略表示正负值的最高位
                value = value & mask;

                //逻辑运算后的值无符号,对无符号的值直接做右移运算,计算剩下的位
                value = value >> pos - 1;
            }

            return value;
        }

        private static int[] ToIntArray(byte[] data, bool includeLenth)
        {

            int n = (data.Length & 3) == 0 ? (data.Length.RightMove(2)) : (data.Length.RightMove(3)) + 1;
            int[] result;

            if (includeLenth)
            {
                result = new int[n + 1];
                result[n] = data.Length;
            }
            else
            {
                result = new int[n];
            }

            n = data.Length;
            for (int i = 0; i < n; ++i)
            {
                result[i.RightMove(2)] |= (0x000000ff & data[i]) << ((i & 3) << 3);
            }

            return result;
        }
        public static byte[] ToByteArray(int[] data, bool includeLength)
        {
            int n = data.Length << 2;
            if (includeLength)
            {
                int m = data[^1];
                if (m > n)
                {
                    return null;
                }
                else
                {
                    n = m;
                }
            }

            byte[] result = new byte[n];
            for (int i = 0; i < n; ++i)
            {
                result[i] = (byte)((data[i.RightMove(2)].RightMove((i & 3) << 3)) & 0xff);
            }

            return result;
        }

        //加密
        private static int[] Encrypt(int[] v, int[] k)
        {
            int n = v.Length - 1;
            if (n < 1)
                return v;

            if (k.Length < 4)
            {
                int[] key = new int[4];
                System.Array.Copy(k, 0, key, 0, k.Length);
                k = key;
            }

            int z = v[n], y = v[0];
            int delta = -1640531527;
            int sum = 0, e;
            int p, q = 6 + 52 / (n + 1); ;

            while (q-- > 0)
            {
                sum = (int)(sum + delta);
                e = sum.RightMove(2) & 3;

                for (p = 0; p < n; p++)
                {
                    y = v[p + 1];
                    z = v[p] += (z.RightMove(5) ^ y << 2) + (y.RightMove(3) ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                }

                y = v[0];
                z = v[n] += (z.RightMove(5) ^ y << 2) + (y.RightMove(3) ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
            }

            return v;
        }

        //解密
        private static int[] Decrypt(int[] v, int[] k)
        {
            int n = v.Length - 1;

            if (n < 1)
            {
                return v;
            }

            if (k.Length < 4)
            {
                int[] key = new int[4];

                System.Array.Copy(k, 0, key, 0, k.Length);
                k = key;
            }

            int z = v[n], y = v[0], delta = -1640531527, sum, e;
            int p, q = 6 + 52 / (n + 1);

            sum = q * delta;
            while (sum != 0)
            {
                e = sum.RightMove(2) & 3;
                for (p = n; p > 0; p--)
                {
                    z = v[p - 1];
                    y = v[p] -= (z.RightMove(5) ^ y << 2) + (y.RightMove(3) ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                }
                
                z = v[n];
                y = v[0] -= (z.RightMove(5) ^ y << 2) + (y.RightMove(3) ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                sum = sum - delta;
            }

            return v;
        }

        public static string Base64Encode(string data)
        {
            try
            {
                byte[] encodeDataByte = System.Text.Encoding.UTF8.GetBytes(data);
                string encodeData = Convert.ToBase64String(encodeDataByte);
                return encodeData;
            }
            catch (Exception e)
            {
                throw new Exception("Error in base64Encode" + e.Message);
            }
        }

        public static string Base64Decode(string data)
        {
            try
            {
                byte[] toDecodeDataByte = Convert.FromBase64String(data);
                return System.Text.Encoding.UTF8.GetString(toDecodeDataByte);
            }
            catch (Exception e)
            {
                throw new Exception("Error in base64Decode" + e.Message);
            }
        }

        //byte数组 加密
        public static byte[] Encrypt(byte[] data)
        {
            if (null == data || data.Length == 0)
                return data;

            return ToByteArray(Encrypt(ToIntArray(data, true), ToIntArray(SoketKey, false)), false);
        }

        //bute数组 解密
        public static byte[] Decrypt(byte[] data)
        {
            if (null == data || data.Length == 0)
                return data;

            return ToByteArray(Decrypt(ToIntArray(data, false), ToIntArray(SoketKey, false)), true);
        }

        //字符串加密
        public static string Encrypt(string source,string key)
        {
            System.Text.Encoding encoder = System.Text.Encoding.UTF8;
            byte[] bytData = encoder.GetBytes(source);
            byte[] bytKey = encoder.GetBytes(key);
            if (bytData.Length == 0)
                return "";

            return Convert.ToBase64String(
                ToByteArray(Encrypt(ToIntArray(bytData, true), ToIntArray(bytKey, false)), false)
                ) ;
        }

        //字符串解密
        public static string Decrypt(string source, string key)
        {
            if (source.Length == 0)
                return "";

            System.Text.Encoding encoder = System.Text.Encoding.UTF8;
            byte[] bytData = Convert.FromBase64String(source);
            byte[] bytKey = encoder.GetBytes(key);

            return encoder.GetString(
                ToByteArray(Decrypt(ToIntArray(bytData, false), ToIntArray(bytKey, false)), true)
                );
        }
        
    }
}

3 参考资料

参考链接:https://blog.csdn.net/gsls200808/article/details/48243019

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值