2 onolay onOlay 于 2016.03.23 16:09 提问

请教如何将如下VC++代码转换成C#代码,谢谢
c#
int nKeyLen = strKey.GetLength();
if (nKeyLen == 0)
    strKey = _T("xxsswww");
srand((unsigned)time(NULL));
int nRange = 256;
int nOffset = (rand() % nRange);
CString strDest = _T("");
strDest.Format(_T("%1.2x"), nOffset);

int nSrcAsc = 0;
int nKeyPos = 0;
for (int nSrcPos = 0; nSrcPos < strSrc.GetLength(); nSrcPos++)
{
    int n = strSrc[nSrcPos];
    nSrcAsc = (strSrc[nSrcPos] + nOffset) % 255;
    if (nKeyPos < nKeyLen - 1)
        nKeyPos += 1;
    else
        nKeyPos = 1;
    nSrcAsc = nSrcAsc ^ strKey[nKeyPos];
    CString strTemp = _T("");
    strTemp.Format(_T("%1.2x"), nSrcAsc);
    strDest += strTemp;
    nOffset = nSrcAsc;
}
return strDest;

2个回答

caozhy
caozhy   Ds   Rxr 2016.03.24 07:00
已采纳
 int nKeyLen = strKey.Length;
if (nKeyLen == 0)
    strKey = "xxsswww;
Random rnd = new Random();
int nRange = 256;
int nOffset = (rnd.NextInt() % nRange);
string strDest = string.Format("{0:X2}", nOffset);
int nSrcAsc = 0;
int nKeyPos = 0;
for (int nSrcPos = 0; nSrcPos < strSrc.Length; nSrcPos++)
{
    int n = strSrc[nSrcPos];
    nSrcAsc = (strSrc[nSrcPos] + nOffset) % 255;
    if (nKeyPos < nKeyLen - 1)
        nKeyPos += 1;
    else
        nKeyPos = 1;
    nSrcAsc = nSrcAsc ^ strKey[nKeyPos];
    string strTemp = "";
    strTemp = string.Format("{0:X2}", nSrcAsc);
    strDest += strTemp;
    nOffset = nSrcAsc;
}
return strDest;
caozhy
caozhy   Ds   Rxr 2016.03.24 07:01

nSrcAsc = (strSrc[nSrcPos] + nOffset) % 255;
看上去应该 %256 才对,你检查下。

onOlay
onOlay int nOffset = (rnd.NextInt() % nRange);这一句报错了 error CS1061: “System.Random”不包含“NextInt”的定义,并且找不到可接受类型为“System.Random”的第一个参数的扩展方法“NextInt”(是否缺少 using 指令或程序集引用?) 是否缺少一个引用?? 请问是哪一个?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!