c#调用c的dll 遇到的字符串问题! 知道的进 内详 谢谢

pop1982 2005-02-27 08:22:23
原来的DES是
master_key:主密钥的BCD码
inputbuf:要加密的8位数据,不足位数后补'\0'
outputbuf:加密后得到的密文
void DES(char *key,char *s_text,char *d_text)
{
des_key_schedule sch;
des_set_key((des_cblock *)key,sch);
des_ecb_encrypt((des_cblock *)s_text,(des_cblock *)d_text,&(sch[0]),1);
}

为了给c#调用 主要为了返回d_text改成*d_text 我改成
void DES(char *key,char *s_text,char **d_text)
{
des_key_schedule sch;
des_set_key((des_cblock *)key,sch);
des_ecb_encrypt((des_cblock *)s_text,(des_cblock *)(*d_text),&(sch[0]),1);
}

c#里调用
[DllImport("testdll.dll",EntryPoint="DES",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
public static extern void DES(string key,string s_text,ref string d_text);

string returnValue=new string("");
DES("12345678","12345678",ref returnValue);
textBox1.text=returnValue;

但取出来的是8位中文乱吗

我直接用tc跑 把结果 %c打出来
是8个特殊字符

2种不一样
是不是哪里的编码错了 还是char 不能用string 调用

谢谢大家了



...全文
234 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
pop1982 2005-02-28
  • 打赏
  • 举报
回复
谢谢大家
一个问题就是 我用tc跑出来的 和用c#跑出来的结果不一样
是因为c是ascii c#是uncode ?
我是要把数据用socket发出去 要转成byte的


tsys2000 2005-02-28
  • 打赏
  • 举报
回复
mark
shwtyl 2005-02-27
  • 打赏
  • 举报
回复
给提供两个函数供参考
LPWSTR DupString (LPSTR input)
{
if (input == NULL)
return NULL;
int size = MultiByteToWideChar( CP_ACP, 0, input,(int) strlen(input)+1, NULL, 0);
LPWSTR returnVal = (LPWSTR) malloc (size * sizeof(WCHAR));
if (returnVal == NULL)
return NULL;
MultiByteToWideChar( CP_ACP, 0, input,(int) strlen(input)+1, returnVal, size);
return returnVal;
}
HRESULT DupString(LPSTR input, int size, LPWSTR *output)
{
HRESULT hr = S_OK;
int wcSize;
LPWSTR s = NULL;
int ret;

ASSERT (input != NULL);

wcSize = MultiByteToWideChar( CP_ACP, 0, input,size, NULL, 0);
if (wcSize == 0)
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto ExitHere;
}
s = (LPWSTR) malloc ((size+1) * sizeof(WCHAR));
if (s == NULL)
{
hr = E_OUTOFMEMORY;
goto ExitHere;
}
ret = MultiByteToWideChar( CP_ACP, 0, input,size, s, wcSize);
if (ret == 0)
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto ExitHere;
}
s[size] = 0;
*output = s;

ExitHere:
if (hr != S_OK)
free(s);
return hr;
}
shwtyl 2005-02-27
  • 打赏
  • 举报
回复
你在C代码中用的是ascii编码,而C#默认的是unicode码。
解决方案是做如下修改
void DES(IN wchar *key,IN wchar *s_text,OUT wchar **d_text)

把输入key和s_text由wchar 转换为char
输出之前把char转成wchar即可。
pop1982 2005-02-27
  • 打赏
  • 举报
回复
StringBuilder ggg=new StringBuilder("xxxxxxxx");
StringBuilder fff=new StringBuilder("xxxxxxxx");

des 后 后4位一直是xxxx就是说程序没作用到后4位
但用tc跑 没问题
难道c 和c# 每个char占的位数不一样?


pop1982 2005-02-27
  • 打赏
  • 举报
回复
extern "C" __declspec(dllexport) int des_set_key(des_cblock *key,des_key_schedule schedule)
{
register DES_LONG c,d,t,s;
register unsigned char *in;
register DES_LONG *k;
register int i;

k=(DES_LONG *)schedule;
in=(unsigned char *)key;

c2l(in,c);
c2l(in,d);

PERM_OP (d,c,t,4,0x0f0f0f0fL);
HPERM_OP(c,t,-2,0xcccc0000L);
HPERM_OP(d,t,-2,0xcccc0000L);
PERM_OP (d,c,t,1,0x55555555L);
PERM_OP (c,d,t,8,0x00ff00ffL);
PERM_OP (d,c,t,1,0x55555555L);
d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
c&=0x0fffffffL;

for (i=0; i<16; i++)
{
if (shifts2[i])
{ c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
else
{ c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
c&=0x0fffffffL;
d&=0x0fffffffL;

s= des_skb[0][ (c )&0x3f ]|
des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
((c>>22L)&0x38)];
t= des_skb[4][ (d )&0x3f ]|
des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
des_skb[6][ (d>>15L)&0x3f ]|
des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];

/* table contained 0213 4657 */
*(k++)=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
s= ((s>>16L)|(t&0xffff0000L));

s=(s<<4L)|(s>>28L);
*(k++)=s&0xffffffffL;
}
return(0);
}

extern "C" __declspec(dllexport) void des_ecb_encrypt(des_cblock *input,des_cblock *output,des_key_schedule ks,int encrypt)
{
register DES_LONG l;
register unsigned char *in,*out;
DES_LONG ll[2];

in=(unsigned char *)input;
out=(unsigned char *)output;
c2l(in,l); ll[0]=l;
c2l(in,l); ll[1]=l;
des_encrypt(ll,ks,encrypt);
l=ll[0]; l2c(l,out);
l=ll[1]; l2c(l,out);
l=ll[0]=ll[1]=0;
}

extern "C" __declspec(dllexport) void des_encrypt(DES_LONG *data,des_key_schedule ks,int encrypt)
{
register DES_LONG l,r,u;

/* for XENIX delay */

char delay_str [2];

union fudge {
DES_LONG l;
unsigned short s[2];
unsigned char c[4];
} U,T;
register int i;
register DES_LONG *s;

u=data[0];
r=data[1];

IP(u,r);

l=(r<<1)|(r>>31);
r=(u<<1)|(u>>31);

/* clear the top bits on machines with 8byte longs */
l&=0xffffffffL;
r&=0xffffffffL;

s=(DES_LONG *)ks;
if (encrypt)
{
for (i=0; i<32; i+=4)
{
D_ENCRYPT(l,r,i+0); /* 1 */
/* for XENIX delay */
strcpy(delay_str,"");
D_ENCRYPT(r,l,i+2); /* 2 */
}
}
else
{
for (i=30; i>0; i-=4)
{
D_ENCRYPT(l,r,i-0); /* 16 */
/* for XENIX delay */
strcpy(delay_str,"");
D_ENCRYPT(r,l,i-2); /* 15 */
}
}
l=(l>>1)|(l<<31);
r=(r>>1)|(r<<31);
/* clear the top bits on machines with 8byte longs */
l&=0xffffffffL;
r&=0xffffffffL;

FP(r,l);
data[0]=l;
data[1]=r;
}

pop1982 2005-02-27
  • 打赏
  • 举报
回复
谢谢 storm97
但我就对一半 奇怪 后4位就是不对
storm97 2005-02-27
  • 打赏
  • 举报
回复
c的代码不要改了,就用void DES(char *key,char *s_text,char *d_text)
c#这边改改:
[DllImport("testdll.dll",EntryPoint="DES",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
public static extern void DES(StringBuilder key,StringBuilder s_text,StringBuilder d_text);
pop1982 2005-02-27
  • 打赏
  • 举报
回复
谢谢 Montaque
现在是好一半
奇怪
string aaa="";
string bbb="1234567812345678";
int ccc=16;
char ddd='0';
string eee="gggggggg";
string ggg="";
string fff="";


DES("hhhhhhhh",eee,ref fff);

textBox6.Text=fff;


_DES("hhhhhhhh",fff,ref ggg);

rtbSend.Text=ggg;

textBox6.Text出来的是tc printf("%c" 的也不一样
rtbSend.Text出来的是gggg+乱码



pop1982 2005-02-27
  • 打赏
  • 举报
回复
因为服务器段写死的
所以只能用这个des算法
因为只传1个key c#里的有iv和key
感觉不大一样 不敢用
CharSet=CharSet.Ansi 改为 CharSet=CharSet.Auto
还是没有用啊
Montaque 2005-02-27
  • 打赏
  • 举报
回复
CharSet=CharSet.Ansi 改为 CharSet=CharSet.Auto

另外其实 System.Security 下面有现成的 DES 加密算法

110,539

社区成员

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

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

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