建立一个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解密
}
}
}