TEA算法及C#调用实例

建立一个c++控制台空项目,选生成DLL。要想能被C#程序调用,还要编译成64位的DLL。

tea.def

LIBRARY
EXPORTS
  btea_encrypt
  btea_decrpyt

tea.cpp

//宏定义  
#define LIBEXPORT_API extern "C" __declspec(dllexport)
#define MX                (z>>5^y<<2)+(y>>3^z<<4)^(sum^y)+(k[p&3^e]^z)
#define DELTA             0x9e3779b9
#define S_LOOPTIME        1        //5
#define BLOCK_SIZE        64       //PAGE_SIZE,根据你所要加密的数据包长度修改此参数(单位:字节)

/*
*key  maybe 128bit =16 Bytes.
*buf  maybe BLOCK_SIZE
*/

LIBEXPORT_API void __stdcall btea_encrypt(unsigned char* buf, unsigned char* key)
{
    unsigned char n = BLOCK_SIZE / 4;
    unsigned long *v = (unsigned long *)buf;
    unsigned long *k = (unsigned long *)key;
    unsigned long z = v[n - 1], y = v[0], sum = 0, e;
    unsigned char p, q;
    // Coding Part 

    q = S_LOOPTIME + 52 / n;
    while (q-- > 0)
    {
        sum += DELTA;
        e = sum >> 2 & 3;
        for (p = 0; p < n - 1; p++)
            y = v[p + 1],
            z = v[p] += MX;
        y = v[0];
        z = v[n - 1] += MX;
    }
}


/*
*key  maybe 128bit =16Bytes.
*buf  maybe BLOCK_SIZE
inbuf == outbuf == buf
*/

LIBEXPORT_API void __stdcall btea_decrpyt(unsigned char* buf, unsigned char* key)
{
    unsigned char n = BLOCK_SIZE / 4;
    unsigned long *v = (unsigned long *)buf;
    unsigned long *k = (unsigned long *)key;
    unsigned long z = v[n - 1], y = v[0], sum = 0, e;
    unsigned char  p, q;

    //Decoding Part...
    q = S_LOOPTIME + 52 / n;
    sum = q * DELTA;
    while (sum != 0)
    {
        e = sum >> 2 & 3;
        for (p = n - 1; p > 0; p--)
            z = v[p - 1],
            y = v[p] -= MX;
        z = v[n - 1];
        y = v[0] -= MX;
        sum -= DELTA;
    }
}

program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Tea_test_C_Sharp
{
    class Program
    {
        [DllImport("tea.dll", EntryPoint = "btea_encrypt", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern void btea_encrypt(byte[] buf, byte[] key);

        [DllImport("tea.dll", EntryPoint = "btea_decrpyt", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern void btea_decrpyt(byte[] buf, byte[] key);
        static void Main(string[] args)
        {
            //TEA密钥
            byte[] TEA_key = new byte[16] {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10};

            //数据缓冲区
            byte[] TX_buffer=new byte[32];
            byte[] RX_buffer=new byte[32];
            for (int i = 0; i < 32; i++)
            {
                TX_buffer[i] = (byte)i;
                RX_buffer[i] = (byte)i;
            }
            btea_encrypt(TX_buffer, TEA_key);      //TEA加密
            btea_decrpyt(TX_buffer, TEA_key);      //TEA解密
        }
    }
}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值