C#和VC的数据转换问题。

qyfjl 2009-04-23 02:48:54
有一个加密狗的调用说明,DLL是VC写的,现在想在C#中调用,但他的接口说明及例子是VC的。
由于本人之前没有接触过VC,想请熟悉VC和C#数据之前转换的高手,把接口说明转成C#的例子,谢谢了。

VC调用说明如下:
用import dll 方式调用SUMFX.dll

//公钥
typedef struct {
unsigned int bits; /* length in bits of modulus */
unsigned char modulus[MAX_RSA_MODULUS_LEN]; /* modulus */
unsigned char exponent[MAX_RSA_MODULUS_LEN]; /* public exponent */
} R_RSA_PUBLIC_KEY;

R_RSA_PUBLIC_KEY pubkey = { 1024,
0xb6, 0x00, 0x12, 0x1e, 0xca, 0x5b, 0x95, 0x20,
0xde, 0xf4, 0xbd, 0x12, 0xd6, 0xc1, 0x61, 0x75,
0x0d, 0x1e, 0x10, 0x30, 0x6f, 0xc6, 0x12, 0x50,
0xd8, 0xb8, 0xcc, 0xfd, 0x56, 0x74, 0x2e, 0xc2,
0xb3, 0xc8, 0xa2, 0x72, 0x35, 0xb2, 0x47, 0xe8,
0xfe, 0x66, 0x56, 0x40, 0x78, 0x42, 0x8a, 0xc8,
0x73, 0x5c, 0x40, 0xb4, 0xb9, 0xec, 0x43, 0x6d,
0xd9, 0x09, 0x39, 0xe7, 0x73, 0x73, 0x68, 0xc8,
0x36, 0xbb, 0x75, 0xf0, 0x86, 0x53, 0x56, 0x62,
0xa0, 0xd0, 0x17, 0x7c, 0x29, 0x41, 0x05, 0xc1,
0xfd, 0x73, 0xc7, 0xf6, 0x42, 0xd2, 0x30, 0xa6,
0x33, 0xbf, 0x72, 0xc3, 0xfe, 0xd0, 0xc5, 0xc2,
0xc3, 0x78, 0x6c, 0xe4, 0x2f, 0xde, 0xfe, 0xf8,
0xc0, 0x38, 0x6e, 0x34, 0x47, 0x7e, 0x2e, 0x1e,
0x45, 0xde, 0xcc, 0xa5, 0x34, 0x23, 0xfa, 0xeb,
0x9b, 0x7c, 0x12, 0xbc, 0x9e, 0x4c, 0x64, 0xf9,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01 };




//读狗函数
long _stdcall G3(char * szSub,int CurDate, char *cipher)
szSub 加密标识 分销通传UH
CurDate, 时间,整数表示,取年份后两位和月份 如09年8月 908
10年2月 1002
cipher 128位字符串指针


long _stdcall V1( char * szIoctx ,int iIoctx,char * szSign,int szSignLen,int S1,char* S2,char* S3)
验证函数
szGUID2 见调用过程
iIoctx 16
szGUID1 见调用过程
szSignLen 128
S1 pubkey.bits,
S2 (char *)&(pubkey.modulus[0])
S3 (char *)&(pubkey.exponent[0])


一般调用过程
char szGUID1[150];
char szGUID2[150];
int rc,nAuth
szGUID1=任意串,大于16位,可以取机器码之类的
将szGUID1的前16位赋给szGUID2

rc = G3("RC",0903, szGUID2)
if (V1(szGUID1,16,szGUID2,128,pubkey.bits,(char *)&(pubkey.modulus[0]),(char *)&(pubkey.exponent[0])) == 0) \\是加密狗
{
if(rc = 102) \\是软件所在序列
{
nAuth = G3("UH",0903, char *cipher) \\读软件站点并反回
}
}


...全文
119 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tensionli 2009-04-24
  • 打赏
  • 举报
回复
顶,关注
xieyaming2006 2009-04-24
  • 打赏
  • 举报
回复
高深啊 不懂
teerhu 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fenglaijun 的回复:]
推荐给你的C#的AES加密

C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace SWSoft.IO
{
/// <summary>
/// DES数据加密标准算法由于密钥长度较小(56位),已经不适应当今分布式开放网络对数据加密安全性的要求,
/// 因此1997年NIST公开征集新的数据加密标准,即AES。经过三轮的筛选,比利时Joan Daeman和V…
[/Quote]
格拉 2009-04-24
  • 打赏
  • 举报
回复
数据类型不一样搞死人
lstc 2009-04-24
  • 打赏
  • 举报
回复
C#要调用C的DLL,需要通过DLLImport,结构体需要用C#重写;
您给出的读狗、验证都是自己在加密狗厂家提供的接口之上做了二次封装的,最好参考厂家给出的C#和VC的Demo,因为涉及数据类型的转换。
最后,软件判断加密狗的方式太简单了,很容易被破解,既然使用了1024位RSA算法,就应该将USB通信加密、然后使用RSA算法来对密文进行签名/验证,不然真的很容易被破解
fenglaijun 2009-04-23
  • 打赏
  • 举报
回复
继续帮顶
zcandyly20211 2009-04-23
  • 打赏
  • 举报
回复
友情up!
fenglaijun 2009-04-23
  • 打赏
  • 举报
回复
推荐给你的C#的AES加密

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace SWSoft.IO
{
/// <summary>
/// DES数据加密标准算法由于密钥长度较小(56位),已经不适应当今分布式开放网络对数据加密安全性的要求,
/// 因此1997年NIST公开征集新的数据加密标准,即AES。经过三轮的筛选,比利时Joan Daeman和Vincent Rijmen
/// 提交的Rijndael算法被提议为AES的最终算法。此算法将成为美国新的数据加密标准而被广泛应用在各个领域中。
/// 尽管人们对AES还有不同的看法,但总体来说,AES作为新一代的数据加密标准汇聚了强安全性、高性能、高效率、
/// 易用和灵活等优点。AES设计有三个密钥长度:128,192,256位,相对而言,AES的128密钥比DES的56密钥强1021倍。
/// </summary>
public class AES
{
/// <summary>
/// AES加密
/// </summary>
/// <param name="Data">需要加密的明文-byte[]</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>密文</returns>
public static Byte[] Encrypt(Byte[] Data, String Key, String Vector)
{
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);

Byte[] Cryptograph = null; // 加密后的密文

Rijndael Aes = Rijndael.Create();
try
{
// 开辟一块内存流
using (MemoryStream Memory = new MemoryStream())
{
// 把内存流对象包装成加密流对象
using (CryptoStream Encryptor = new CryptoStream(Memory,
Aes.CreateEncryptor(bKey, bVector),
CryptoStreamMode.Write))
{
// 明文数据写入加密流
Encryptor.Write(Data, 0, Data.Length);
Encryptor.FlushFinalBlock();

Cryptograph = Memory.ToArray();
}
}
}
catch
{
Cryptograph = null;
}

return Cryptograph;
}

/// <summary>
/// AES加密
/// </summary>
/// <param name="Data">需要加密的明文-string</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>密文</returns>
public static string Encrypt(string Data, String Key, String Vector)
{
byte[] byte0 = MyData.ToBytes(Data);//转换成byte[]
byte[] byte1 = Encrypt(byte0, Key, Vector);//加密成byte[]
return MyData.toHexString(byte1);//转换成16进制字符串
}

/// <summary>
/// AES解密
/// </summary>
/// <param name="Data">被解密的密文</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>明文</returns>
public static Byte[] Decrypt(Byte[] Data, String Key, String Vector)
{
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);

Byte[] original = null; // 解密后的明文

Rijndael Aes = Rijndael.Create();
try
{
// 开辟一块内存流,存储密文
using (MemoryStream Memory = new MemoryStream(Data))
{
// 把内存流对象包装成加密流对象
using (CryptoStream Decryptor = new CryptoStream(Memory,
Aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
// 明文存储区
using (MemoryStream originalMemory = new MemoryStream())
{
Byte[] Buffer = new Byte[1024];
Int32 readBytes = 0;
while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
}

original = originalMemory.ToArray();
}
}
}
}
catch
{
original = null;
}

return original;
}


/// <summary>
/// AES解密
/// </summary>
/// <param name="Data">需要解密的明文-string</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>明文</returns>
public static string Decrypt(string Data, String Key, String Vector)
{
try
{
byte[] byte0 = MyData.ToHexBytes(Data);//将16进制字符串转换成byte[]
byte[] byte1 = Decrypt(byte0, Key, Vector);//解密成byte[]
return UTF8Encoding.UTF8.GetString(byte1);//还原成字符串
}
catch (Exception)
{
return null;
}
}
}

public class MyData
{
public static byte[] ToHexBytes(string data)
{
if (data != null && data.Length > 0)
{
byte[] bytes = new byte[data.Length % 2 == 0 ? data.Length / 2 : data.Length - 1];
for (int i = 0, j = 0; i < data.Length; i += 2, j++)
{
bytes[j] = byte.Parse(data.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
return bytes;
}
return null;
}

public static byte[] ToBytes(string data)
{
if (data != null && data.Length > 0)
{
return UTF8Encoding.UTF8.GetBytes(data);
}
return null;
}

/// <summary>
/// 转换成16进制字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string toHexString(byte[] data)
{
if (data != null)
{
string hex = string.Empty;
for (int i = 0; i < data.Length; i++)
{
hex += data[i].ToString("X2");
}
return hex;
}
return null;
}
}
}
boywangliang 2009-04-23
  • 打赏
  • 举报
回复
只能帮顶
螳螂86 2009-04-23
  • 打赏
  • 举报
回复
帮顶,,最近VC++和C# 混合 数据问题挺多的。
itcrazyman 2009-04-23
  • 打赏
  • 举报
回复
mark up 帮顶

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧