好的,所以我有这个代码,我将它传递给一个非托管 dll,我只知道导出,并且有一些示例代码。我得到了正确的字符串,但后面是垃圾字节。
我基本上是从没有这个问题的 c++ 示例程序中逐字翻译代码。我假设我在这里缺少一些基本的东西,所以如果有人能告诉我那是什么,我将不胜感激。
示例 C++ 代码
void CDUKPT_TESTDlg::OnButton4()
{
// TODO: Add your control notification handler code here
unsigned char dataout[1024],tmp[1024],ksn[20],keyval[20];
int nRet,len;
memset(dataout,0,sizeof(dataout));
memset(ksn,0,sizeof(ksn));
memset(keyval,0,sizeof(keyval));
memset(tmp,0,sizeof(tmp));
UpdateData(TRUE);
two_one((unsigned char *)m_strCURKSN.GetBuffer(m_strCURKSN.GetLength()),m_strCURKSN.GetLength(),ksn);
two_one((unsigned char *)m_strMACK.GetBuffer(m_strMACK.GetLength()),m_strMACK.GetLength(),keyval);
two_one((unsigned char *)m_EncryptDat.GetBuffer(m_EncryptDat.GetLength()),m_EncryptDat.GetLength(),dataout);
len=m_EncryptDat.GetLength()/2;
//extern int __stdcall ExtractDat(unsigned char *input,
//unsigned short len,unsigned char *output,unsigned char *key,
//unsigned char *ksn);
nRet=ExtractDat(dataout,len,tmp,keyval,ksn); //External Call
//Good string+bad trailing data comes back in tmp
m_Result=tmp;
UpdateData(FALSE);
}
这段代码吐出这个܉Òdÿo
这是我的 VB.Net 代码
Public Function Encrypt(ByVal inp As String) As String
Dim tmpSB As New StringBuilder
Dim i As Integer
Dim tKsn As Char() = TwoOne(StrCurKsn)
For i = tKsn.Length To 19
tKsn = tKsn + Chr(0)
Next
Dim tMack As Char() = TwoOne(StrMack)
For i = tMack.Length To 19
tMack = tMack + Chr(0)
Next
Dim tEnc As Char() = TwoOne(inp)
For i = tEnc.Length To 1023
tEnc = tEnc + Chr(0)
Next
Dim len As Integer = tEnc.Length / 2
Dim tmpStr(1023) As Char
Array.Clear(tmpStr, 0, 1024)
Dim tmpPtr = Marshal.StringToHGlobalAnsi(tmpStr)
Dim nRet = ExtractDat(tEnc, len, tmpPtr, tMack, tKsn)
tmpStr = Marshal.PtrToStringAnsi(tmpPtr)
Dim tsl = tmpStr.Length
Encrypt = tmpStr
End Function
这段代码吐了出来
܉Òdÿo ålUäÙålUäÙålUäÙålUäÙålUäÙålUäÙålUäÙ
所以我得到了正确的字符串,但后面跟着一串重复的垃圾字符。我希望我在这里做了一些公然错误的事情,但是我尝试将数据作为字节和字符提取,并以许多不同的方法进行转换,但我似乎无法摆脱这些字符......还有,ExtractDat
不返回字符串的长度(不是问题,因为它不应该,这真的很烦人)。