- 請教如何將如下VC++代碼轉換成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;
最佳回答:
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;