C++转C#,很少代码,求救

cow8063 2017-11-01 04:22:52
1、
unsigned char ChgByte_data(unsigned char bData)
{
unsigned char a,b;
a= (bData & 0x0f)<<4;
b= (bData & 0xf0)>>4;
return (a+b);
}
2、
void Decryption_data(unsigned char *key, unsigned char *s_text, unsigned char *d_text, unsigned int dataLength)
{
while(dataLength >= 8)
{
Decryption(&key[0],&s_text[0],&d_text[0]); // 解密
dataLength -= 8;
s_text += 8;
d_text +=8;
}
}
3、
unsigned char getMAC(unsigned char *pData, unsigned char dataLength,unsigned char *pKey,unsigned char keyLen,unsigned char bFlag,unsigned char *pMAC)
{
unsigned char gRand[8];
unsigned char macData[8];
unsigned char i;
unsigned char *pDecryptKey;
unsigned char len;
// 初始值为xxxx0000
for(i = 0; i < 8; i++)
{
gRand[i] = mac_init[i];
}
// 开始计MAC
while(dataLength >= 8)
{
memcpy(macData, pData, 8);
for(i = 0; i < 8; i++)
{
gRand[i] = macData[i] ^ gRand[i];
}
// 用密钥的前半部来进行加密操作
Encryption(&pKey[0],&gRand[0],&gRand[0]); // 加密
dataLength -= 8;
pData += 8;
}
// 处理最后一个Dn
memcpy(macData, pData, dataLength);
macData[dataLength] = 0x80;
len = 8 - dataLength - 1;
for(i = 0; i < len; i++)
{
macData[i+dataLength+1]= 0x00;
}
for(i = 0; i < 8; i++)
{
gRand[i] = macData[i] ^ gRand[i];
}

// 用密钥的前半部来进行加密操作,
Encryption(&pKey[0],&gRand[0],&gRand[0]); // 加密
// 如果key为16位
if(keyLen == LONG_KEY_LENGTH)
{
pDecryptKey = pKey + 8; // 16字节密钥右半部分解密
Decryption(pDecryptKey,&gRand[0],&gRand[0]); // 解密
Encryption(&pKey[0],&gRand[0],&gRand[0]); // 加密
}
memcpy(pMAC, gRand, 4);
return(true);
}
谢谢各位大神
...全文
264 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
白衣如花 2017-11-02
  • 打赏
  • 举报
回复
引用 6 楼 u012948520 的回复:
+=8 应该是指针后移8个元素,即去掉数组前8个元素。 byte[] temp = new byte[s_text.Length]; Array.Copy(s_text, 8, temp , 0, temp .Length); s_text = temp;
byte[] temp = new byte[s_text.Length];改成byte[] temp = new byte[s_text.Length - 8];
白衣如花 2017-11-02
  • 打赏
  • 举报
回复
+=8 应该是指针后移8个元素,即去掉数组前8个元素。 byte[] temp = new byte[s_text.Length]; Array.Copy(s_text, 8, temp , 0, temp .Length); s_text = temp;
白衣如花 2017-11-02
  • 打赏
  • 举报
回复
引用 4 楼 cow8063 的回复:
这个函数不用管, 如下 void Decryption_data(unsigned char *key, unsigned char *s_text, unsigned char *d_text, unsigned int dataLength) { while(dataLength >= 8) { Decryption(&key[0],&s_text[0],&d_text[0]); // 解密 dataLength -= 8; s_text += 8; d_text +=8; } } 已经转为C#,但有部分不知 private void Decryption_data(byte[] key, byte[] s_text, byte[] d_text, uint dataLength) { while (dataLength >= 8) { Decryption(&key[0], &s_text[0], &d_text[0]); // 如何转 dataLength -= 8; s_text += 8;// 如何转 d_text += 8;// 如何转 } }
去掉C#用ref 来实现引用传值,和C++的&十分类似
cow8063 2017-11-02
  • 打赏
  • 举报
回复
引用 1 楼 zpc38368330 的回复:
Decryption,这个函数没有原型,这不是c++的公用函数吧
这个函数不用管, 如下 void Decryption_data(unsigned char *key, unsigned char *s_text, unsigned char *d_text, unsigned int dataLength) { while(dataLength >= 8) { Decryption(&key[0],&s_text[0],&d_text[0]); // 解密 dataLength -= 8; s_text += 8; d_text +=8; } } 已经转为C#,但有部分不知 private void Decryption_data(byte[] key, byte[] s_text, byte[] d_text, uint dataLength) { while (dataLength >= 8) { Decryption(&key[0], &s_text[0], &d_text[0]); // 如何转 dataLength -= 8; s_text += 8;// 如何转 d_text += 8;// 如何转 } }
threenewbee 2017-11-01
  • 打赏
  • 举报
回复
指针转数组,别的照抄就可以了。
白衣如花 2017-11-01
  • 打赏
  • 举报
回复
nchar 用 C#的 byte 其他的都差不多
zpc38368330 2017-11-01
  • 打赏
  • 举报
回复
Decryption,这个函数没有原型,这不是c++的公用函数吧

110,533

社区成员

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

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

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