我需要将此C ++函数重写为C#
bool DesDecrypt(const BYTE *InBuff, DWORD dwInBuffSize, BYTE *OutBuff, DWORD dwOutBuffSize, const char *TerminalID)
{
...
for(DWORD i = 0 ; i < dwInBuffSize/8 ; i++)
DES.des_ecb_encrypt((des_cblock *)InBuff+i, (des_cblock *)OutBuff+i, sched, DES_DECRYPT) ;
}
我卡住的地方是指针算术。在C ++方面,您可以看到作者的用法
InBuff+i
因此,它是前进的指针并将其传递给函数。
在C#上,我的函数如下所示:
public static bool DesDecrypt(byte[] inBuff, uint inBuffSize, byte[] outBuff, uint outBufSize, string terminalID)
{
.....
}
我被困在如何重写上面的循环(特别是如何将指针传递到字节数组中的下一个元素)到C#。在C#中没有指针算法,因此如果我执行类似的操作,它将仅传递字节数组的第i个值。
那么如何在C#上模拟将指针传递给数组中的下一个元素呢?
这是我在C#中的解密功能
public static byte[] DecryptDES_ECB(byte [] ciphertext, byte [] key)
我应该使用而不是C ++版本:
DES.des_ecb_encrypt
我正在寻找这样的包装作为C#方面的解决方案
public static byte[] DecryptDES_ECB(byte[] ciphertext, int cipherOffset, byte[] key)
{
byte [] tmp = new byte [ciphertext.Length - cipherOffset];
for(int i = 0; i<ciphertext.Length - cipherOffset; i++)
{
tmp[i] = ciphertext[cipherOffset + i];
}
return DecryptDES_ECB(tmp, key);
}
您认为这行得通吗?现在,我将在C#端循环调用此函数,并像在C ++中那样传递偏移量。
最佳答案
如果使用LINQ扩展名并编写inBuff.Skip(i)
,则将获得一个IEnumerable,该IEnumerable的元素以i inBuff元素开头。除非您调用ToList
方法,否则不会出现复制和其他内存分配,但是您可以像对待它一样将新的IEnumerable当作子数组使用。
关于c# - 将C++函数重写为C#(将指针传递给数组中的下一个元素),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33774585/