#define _WIN64//定义64位
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace FSP256COMCSharpx86
{
class Program
{
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256Init",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern bool PEA256Init();//PEA256初始化
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256Open",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern IntPtr PEA256Open();//PEA256初始化
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256Close",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern void PEA256Close(IntPtr handle);//PEA256初始化
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256EncryptUserKey",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern UInt32 PEA256EncryptUserKey(IntPtr handle, string keystring, string keybuffer, string filename);//设置加密主密钥
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256DecryptUserKey",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern UInt32 PEA256DecryptUserKey(IntPtr handle, string keystring, string keybuffer, string filename);//设置解密主密钥
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256EncryptBytes",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern UInt32 PEA256EncryptBytes(IntPtr handle, byte[] bytes, int len, UInt32 subkey); //字节流加密
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256DecryptBytes",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern UInt32 PEA256DecryptBytes(IntPtr handle, byte[] bytes, int len, UInt32 subkey); //字节流解密
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256EncryptBytesEx",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern UInt32 PEA256EncryptBytesEx(IntPtr handle, byte[] inbytes, byte[] outbytes, int len, UInt32 subkey); //字节流加密
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256DecryptBytesEx",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern UInt32 PEA256DecryptBytesEx(IntPtr handle, byte[] inbytes, byte[] outbytes, int len, UInt32 subkey); //字节流解密
#if _WIN64
[DllImport("D:\\libpea256x64\\libpea256x64.dll",
#else
[DllImport("D:\\libpea256x86\\libpea256x86.dll",
#endif
EntryPoint = "PEA256Test",
SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)
]
public static extern bool PEA256Test();//PEA256内部测试函数
static void Main(string[] args)
{
if (PEA256Init())
{
PEA256Test();
IntPtr handle = PEA256Open();
if (handle != null)
{
byte[] Array = { 0, 1, 2, 3 };
byte[] ArrayEncrypt = { 0, 1, 2, 3 };
byte[] ArrayDecrypt = new byte[4];
UInt32 subkey = PEA256EncryptUserKey(handle, "123", "", "");//主密钥为字符串"123",返回子密钥subkey(可以自定义,但必须与解密的子密钥同步)
subkey = PEA256EncryptBytes(handle, Array, 4, subkey);//加密解密共用数组Array,子密钥不级联时,不需要返回值。(级联用于连续数据包加解密时前后有关联的情况;不级联是连续数据包加解密时前后无关联的情况)
subkey = PEA256DecryptUserKey(handle, "123", "", "");//主密钥为字符串"123",返回子密钥subkey(可以自定义,但必须与加密的子密钥同步)
subkey = PEA256DecryptBytes(handle, Array, 4, subkey);//加密解密共用数组Array,子密钥不级联时,不需要返回值。(级联用于连续数据包加解密时前后有关联的情况;不级联是连续数据包加解密时前后无关联的情况)
int sum = 0;
for (int i = 0; i < 4; i++)
{
sum ^= i ^ Array[i];
}
if (sum != 0) Console.WriteLine("加解密1错误!!!");
else Console.WriteLine("加解密1正确!!!");
subkey = PEA256EncryptUserKey(handle, "", "0123456789ABCDEF0123456789ABCDEF", "");//主密钥为固定32个字节首字节非零的密钥缓冲区,返回子密钥subkey(可以自定义,但必须与解密的子密钥同步)
subkey = PEA256EncryptBytesEx(handle, ArrayEncrypt, ArrayDecrypt, 4, subkey);//加密解密不共用数组,子密钥不级联时,不需要返回值。(级联用于连续数据包加解密时前后有关联的情况;不级联是连续数据包加解密时前后无关联的情况)
subkey = PEA256DecryptUserKey(handle, "", "0123456789ABCDEF0123456789ABCDEF", "");//主密钥为固定32个字节首字节非零的密钥缓冲区,返回子密钥subkey(可以自定义,但必须与加密的子密钥同步)
subkey = PEA256DecryptBytesEx(handle, ArrayDecrypt, ArrayEncrypt, 4, subkey);//加密解密不共用数组,子密钥不级联时,不需要返回值。(级联用于连续数据包加解密时前后有关联的情况;不级联是连续数据包加解密时前后无关联的情况)
sum = 0;
for (int i = 0; i < 4; i++)
{
sum ^= i ^ ArrayEncrypt[i];
}
if (sum != 0) Console.WriteLine("加解密2错误!!!");
else Console.WriteLine("加解密2正确!!!");
subkey = PEA256EncryptUserKey(handle, "", "", "123.PEA.KEY");//主密钥为随机密钥格式文件123.PEA.KEY(不存在自动随机生成),返回子密钥subkey(可以自定义,但必须与解密的子密钥同步)
subkey = PEA256EncryptBytesEx(handle, ArrayEncrypt, ArrayEncrypt, 4, subkey);//加密解密共用数组ArrayEncrypt,子密钥不级联时,不需要返回值。(级联用于连续数据包加解密时前后有关联的情况;不级联是连续数据包加解密时前后无关联的情况)
subkey = PEA256DecryptUserKey(handle, "", "", "123.PEA.KEY");//主密钥为随机密钥格式文件123.PEA.KEY(不存在自动随机生成),返回子密钥subkey(可以自定义,但必须与加密的子密钥同步)
subkey = PEA256DecryptBytesEx(handle, ArrayEncrypt, ArrayEncrypt, 4, subkey);//加密解密共用数组ArrayEncrypt,子密钥不级联时,不需要返回值。(级联用于连续数据包加解密时前后有关联的情况;不级联是连续数据包加解密时前后无关联的情况)
sum = 0;
for (int i = 0; i < 4; i++)
{
sum ^= i ^ ArrayEncrypt[i];
}
if (sum != 0) Console.WriteLine("加解密3错误!!!");
else Console.WriteLine("加解密3正确!!!");
subkey = PEA256EncryptUserKey(handle, "", "", "123.TXT");//主密钥为任意格式文件123.TXT(前64KB数据有效,文件不存在主密钥为字符串“123.TXT”),返回子密钥subkey(可以自定义,但必须与解密的子密钥同步)
subkey = PEA256EncryptBytesEx(handle, ArrayEncrypt, ArrayDecrypt, 4, subkey);//加密解密不共用数组,子密钥不级联时,不需要返回值。(级联用于连续数据包加解密时前后有关联的情况;不级联是连续数据包加解密时前后无关联的情况)
subkey = PEA256DecryptUserKey(handle, "", "", "123.TXT");//主密钥为任意格式文件123.TXT(前64KB数据有效,文件不存在主密钥为字符串“123.TXT”),返回子密钥subkey(可以自定义,但必须与加密的子密钥同步)
subkey = PEA256DecryptBytesEx(handle, ArrayDecrypt, ArrayEncrypt, 4, subkey);//加密解密不共用数组,子密钥不级联时,不需要返回值。(级联用于连续数据包加解密时前后有关联的情况;不级联是连续数据包加解密时前后无关联的情况)
sum = 0;
for (int i = 0; i < 4; i++)
{
sum ^= i ^ ArrayEncrypt[i];
}
if (sum != 0) Console.WriteLine("加解密4错误!!!");
else Console.WriteLine("加解密4正确!!!");
PEA256Close(handle);
Console.ReadLine();
}
}
}
}
}