问题描述
- 请教如何将如下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;
解决方案二:
nSrcAsc = (strSrc[nSrcPos] + nOffset) % 255;
看上去应该 %256 才对,你检查下。
时间: 2024-10-01 08:04:04