using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
internal class Program
{
enum VKeyGenResultExOpt
{
KGREO_Ok = 0,
KGREO_BufferToSmall = 1,
KGREO_SecurityLevelInvalid = 2,
KGREO_VariantInvalid = 3,
KGREO_UnspecifiedError = 4
};
[DllImport("LHM_Vector.dll", EntryPoint = "GenerateKeyExOpt" ,CallingConvention = CallingConvention.Cdecl)]//导入安全算法dll
private static extern VKeyGenResultExOpt GenerateKeyExOpt( byte[] ipSeedArray, uint iSeedArraySize,
uint iSecurityLevel, ref string ipVariant, ref string ipOptions,
out IntPtr iopKeyArray, uint iMaxKeyArraySize,
ref uint oActualKeyArraySize);//引用算法中定义的函数
/// <summary>
/// IntPtr转byte[]数组
/// </summary>
/// <param name="obj">Inptr</param>
/// <returns></returns>
static byte[] Intptr2byte(object obj)
{
int size = Marshal.SizeOf(obj);
IntPtr intPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(obj, intPtr, false);
byte[] by = new byte[size];
Marshal.Copy(intPtr, by, 0, by.Length);
return by;
}
catch (Exception ex)
{
Marshal.FreeHGlobal(intPtr);
}
return null;
}
static void Main(string[] args)
{
byte[] ipSeedArray=new byte[4] { 0X91,0X7C,0x39,0X1F}; //ECU处获取一个种子
uint iSeedArraySize=4;//设置种子长度
uint iSecurityLevel=7;//设置安全等级
string ipVariant = "";//默认为空
string ipOptions = "";//默认为空
IntPtr iopKeyArray; //经过安全认证返回的Key
uint oActualKeyArraySize = 0; ;//返回Key的长度
GenerateKeyExOpt(ipSeedArray, iSeedArraySize, iSecurityLevel, ref ipVariant, ref ipOptions, out iopKeyArray, 4, ref oActualKeyArraySize);//根据种子计算Key,安全等级
byte[] key= Intptr2byte(iopKeyArray);//得到的Key转成数组形式
Console.WriteLine();
Console.ReadLine();
}
}
}
c# 中调用c++ dll文件 使用VKeyGenResultExOpt函数获取KEY
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- How to make a callback to C# from C/C++ code - CodeProjec...
- 最近心血来潮,和一个朋友打算一起写一个放假查询应用软件。我负责编写假期识别算法,对方负责界面设计,交互设计,以及上...
- 一. 用户手册 Building an Application with a Native Plugin for ...
- C#教程 在拥有Python开发的基础上学习C#。因为工作中需要使用相机SDK开发一个简单的桌面应用程序 问题1:...
- 简介:使用C#和C++联合编程时,常常要在C#和C++语言之间来回切换,对于现在的我来说很容易出错,在这里记录一下...