/////////////////////////////////////////////////////////
//加密函数
Function EncrypKey (Src:String; Key:String):string;
var
idx :integer;
KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
Range :Integer;begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='xxsswww';
KeyPos:=0;
SrcPos:=0;
SrcAsc:=0;
Range:=256;Randomize;
offset:=Random(Range);
dest:=format('%1.2x',[offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;
if KeyPos < KeyLen then KeyPos:= KeyPos + 1 else KeyPos:=1;
SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);
dest:=dest + format('%1.2x',[SrcAsc]);
offset:=SrcAsc;
end;
Result:=Dest;
end; //解密函数
Function UncrypKey (Src:String; Key:String):string;
var idx :integer;KeyLen :Integer;KeyPos :Integer;offset :Integer; dest :string;SrcPos :Integer;SrcAsc :Integer;TmpSrcAsc :Integer;Range :Integer; begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='xxsswww';
KeyPos:=0;
SrcPos:=0;
SrcAsc:=0;
Range:=256;
offset:=StrToInt('$'+ copy(src,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
if KeyPos < KeyLen Then KeyPos := KeyPos + 1 else KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= offset then
TmpSrcAsc := 255 + TmpSrcAsc - offset
else
TmpSrcAsc := TmpSrcAsc - offset;
dest := dest + chr(TmpSrcAsc);
offset:=srcAsc;
SrcPos:=SrcPos + 2;
until SrcPos >= Length(Src);
Result:=Dest;
end;/////////////////////////////////////////////////////////上面是DELPHI的一个加密文本的代码,同一个文本,同一个密钥,每次加密后的文本都是不同的,但都能正确解密,求VC++同样功能的,如果能直接翻译上面的代码最好,100分,CSDN的朋友们帮帮忙.
//加密函数
Function EncrypKey (Src:String; Key:String):string;
var
idx :integer;
KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
Range :Integer;begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='xxsswww';
KeyPos:=0;
SrcPos:=0;
SrcAsc:=0;
Range:=256;Randomize;
offset:=Random(Range);
dest:=format('%1.2x',[offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;
if KeyPos < KeyLen then KeyPos:= KeyPos + 1 else KeyPos:=1;
SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);
dest:=dest + format('%1.2x',[SrcAsc]);
offset:=SrcAsc;
end;
Result:=Dest;
end; //解密函数
Function UncrypKey (Src:String; Key:String):string;
var idx :integer;KeyLen :Integer;KeyPos :Integer;offset :Integer; dest :string;SrcPos :Integer;SrcAsc :Integer;TmpSrcAsc :Integer;Range :Integer; begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='xxsswww';
KeyPos:=0;
SrcPos:=0;
SrcAsc:=0;
Range:=256;
offset:=StrToInt('$'+ copy(src,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
if KeyPos < KeyLen Then KeyPos := KeyPos + 1 else KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= offset then
TmpSrcAsc := 255 + TmpSrcAsc - offset
else
TmpSrcAsc := TmpSrcAsc - offset;
dest := dest + chr(TmpSrcAsc);
offset:=srcAsc;
SrcPos:=SrcPos + 2;
until SrcPos >= Length(Src);
Result:=Dest;
end;/////////////////////////////////////////////////////////上面是DELPHI的一个加密文本的代码,同一个文本,同一个密钥,每次加密后的文本都是不同的,但都能正确解密,求VC++同样功能的,如果能直接翻译上面的代码最好,100分,CSDN的朋友们帮帮忙.
加密解密都需要提供密码,密码不同,加密出来的文本也不同#include "..\stdafx.h"
#include <stack>
#include <assert.h>
#include <memory.h>
#include <stdio.h>
#include <string.h>
#include <atlconv.h>
#include <Wincrypt.h>
#include <atlbase.h>
#include <wchar.h>
#include <gdiplus.h>
#include <Winsock2.h>
#include "BaseTool.h"#pragma comment(lib, "gdiplus.lib")
#pragma comment(lib, "ws2_32.lib")
// CALG_MD5 MD5 Hashing algorithm.
// CALG_RC2 RC2 block encryption algorithm.
// CALG_RC4 RC4 stream encryption algorithm.
#ifdef STREAM_ENCRYPTION
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 1
#else
#define ENCRYPT_ALGORITHM CALG_RC2
#define ENCRYPT_BLOCK_SIZE 8
#endif#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////typedef unsigned int uint4;
typedef unsigned short int uint2;
typedef unsigned char uchar;
////////////////////////////////////////////////////////////////////////////////////////
// implementation of CCryptTool classconst int CCryptTool::m_nBlockSize = 1000;
HRESULT CCryptTool::DecryptByte(byte *pSourceBuf, int nSourceBufLen, byte *pDesBuf, int nDesBufLen, const CString &strPsw)
{
USES_CONVERSION; HCRYPTPROV hCryptProv(NULL);
HCRYPTKEY hKey(NULL);
HCRYPTHASH hHash(NULL); PBYTE pbBuffer(NULL);
DWORD dwBlockLen(0);
DWORD dwBufferLen(0);
DWORD dwCount(0); HRESULT hr = E_FAIL; try
{
// Get a handle to the default provider.
if( !CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
{
throw _T("Error during CryptAcquireContext!");
} //--------------------------------------------------------------------
// Decrypt the file with a session key derived from a password. //--------------------------------------------------------------------
// Create a hash object.
if( !CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash))
{
throw _T("Error during CryptCreateHash!");
}
//--------------------------------------------------------------------
// Hash in the password data.
LPCWSTR szPassword = T2W( (LPTSTR)(LPCTSTR)strPsw );
#pragma warning(disable:4267)
if( !CryptHashData( hHash, reinterpret_cast<const unsigned char *>(szPassword), wcslen(szPassword), 0))
{
throw _T("Error during CryptHashData!");
}
#pragma warning(default:4267)
//--------------------------------------------------------------------
// Derive a session key from the hash object. if(!CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, NULL, &hKey))
{
throw _T("Error during CryptDeriveKey!");
}
//--------------------------------------------------------------------
// Destroy the hash object. CryptDestroyHash(hHash);
hHash = 0; //--------------------------------------------------------------------
// The decryption key is now available, either having been imported
// from a BLOB read in from the source file or having been created
// using the password. This point in the program is not reached if
// the decryption key is not available. //--------------------------------------------------------------------
// Determine the number of bytes to decrypt at a time.
// This must be a multiple of ENCRYPT_BLOCK_SIZE. dwBlockLen = m_nBlockSize - m_nBlockSize % ENCRYPT_BLOCK_SIZE;
dwBufferLen = dwBlockLen; //--------------------------------------------------------------------
// Allocate memory. pbBuffer = new byte[dwBufferLen];
if( pbBuffer == NULL)
{
throw _T("Out of memory!\n");
}
//--------------------------------------------------------------------
// Decrypt source buffer, and write to destination buffer.
BOOL bFinal = TRUE;
BYTE *pEofDestBuf = pDesBuf + nDesBufLen; nDesBufLen = 0; do {
//--------------------------------------------------------------------
// Read up to dwBlockLen bytes from source buffer.
bFinal = ( (DWORD)nSourceBufLen <= dwBufferLen) ? TRUE : FALSE;
dwCount = bFinal ? nSourceBufLen : dwBufferLen;
nSourceBufLen -= dwCount; memcpy( pbBuffer, pSourceBuf , dwCount);
pSourceBuf += dwCount;
//--------------------------------------------------------------------
// Decrypt data.
if(!CryptDecrypt( hKey, 0, bFinal, 0, pbBuffer, &dwCount))
{
throw _T("Error during CryptDecrypt!");
}
//--------------------------------------------------------------------
// Write data to destination file. if ( pDesBuf + dwCount <= pEofDestBuf)
{
memcpy( pDesBuf, pbBuffer, dwCount);
pDesBuf += dwCount;
nDesBufLen += dwCount;
}
else
{
throw _T(" Out of memory");
} } while( !bFinal ); hr = S_OK; }
catch( CException *e )
{
e->Delete();
}
catch(...)
{ }
//--------------------------------------------------------------------
// Free memory. if( pbBuffer )
{
delete []pbBuffer;
pbBuffer = NULL;
} //--------------------------------------------------------------------
// Destroy session key. if(hKey)
{
CryptDestroyKey(hKey);
} //--------------------------------------------------------------------
// Destroy hash object.
if(hHash)
{
CryptDestroyHash(hHash);
} //--------------------------------------------------------------------
// Release provider handle. if(hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
} return hr;}
HRESULT CCryptTool::DecryptString(const CString &strSource, CString &strDest, const CString &strPsw)
{
USES_CONVERSION; CBase64Tool base64Tool;
HRESULT hr = E_FAIL;
byte* pSourceBuffer = NULL;
byte* pDestBuffer = NULL;
int nBufferLength = 0; strDest.Empty();
if ( strSource.IsEmpty() )
{
return S_FALSE;
}
//
try
{
VERIFY_SUCCEEDED( base64Tool.Decode_Base64( strSource, NULL, nBufferLength) ) ;
pSourceBuffer = new byte[nBufferLength];
VERIFY_SUCCEEDED( base64Tool.Decode_Base64( strSource, pSourceBuffer, nBufferLength) );
//
pDestBuffer = new byte[nBufferLength + sizeof(WCHAR)];
ZeroMemory( pDestBuffer, nBufferLength + sizeof(WCHAR)); VERIFY_SUCCEEDED( DecryptByte( pSourceBuffer, nBufferLength, pDestBuffer, nBufferLength, strPsw) );
strDest = W2T( reinterpret_cast<LPWSTR>(pDestBuffer) );
hr = S_OK;
}
catch( CException *e)
{
e->Delete();
}
catch(...)
{ } delete []pSourceBuffer;
delete []pDestBuffer; return hr;
}HRESULT CCryptTool::EncryptByte(byte *pSourceBuf, int nSourceBufLen, byte *pDesBuf, int& nDesBufLen, const CString &strPsw)
{
USES_CONVERSION;
HRESULT hr(E_FAIL); HCRYPTPROV hCryptProv(NULL);
HCRYPTKEY hKey(NULL);
HCRYPTHASH hHash(NULL); PBYTE pbBuffer(NULL);
DWORD dwBlockLen(0);
DWORD dwBufferLen(0);
DWORD dwCount(0); //获取一个CSP句柄
//NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
try
{
if( !CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
{
//密钥容器不存在
if( !CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
throw _T("acquire CSP failed");
}
} //--------------------------------------------------------------------
// 创建一个会话密钥(session key)
// 会话密钥也叫对称密钥,用于对称加密算法。
// (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
// CryptReleaseContext 期间的阶段。) //--------------------------------------------------------------------
// Create a hash object.
if( !CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash))
{
throw _T("Error during CryptCreateHash!\n");
} //--------------------------------------------------------------------
// 用输入的密码产生一个散列
LPCWSTR szPassword = T2W( (LPTSTR)(LPCTSTR)strPsw );
#pragma warning(disable:4267)
if( !CryptHashData( hHash, reinterpret_cast<const unsigned char *>(szPassword), wcslen(szPassword), 0))
{
throw _T("Error during CryptHashData. \n");
}
#pragma warning(default:4267) //--------------------------------------------------------------------
// 通过散列生成会话密钥(session key)
if( !CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, NULL, &hKey))
{
throw _T("Error during CryptDeriveKey!\n");
} //--------------------------------------------------------------------
// Destroy the hash object. CryptDestroyHash(hHash);
hHash = NULL; //--------------------------------------------------------------------
// The session key is now ready. //--------------------------------------------------------------------
// 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
// 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
// 数据长度。 dwBlockLen = m_nBlockSize - m_nBlockSize % ENCRYPT_BLOCK_SIZE; //--------------------------------------------------------------------
// Determine the block size. If a block cipher is used,
// it must have room for an extra block. dwBufferLen = (ENCRYPT_BLOCK_SIZE > 1) ? (dwBlockLen + ENCRYPT_BLOCK_SIZE) : dwBlockLen; // In a do loop, encrypt the source buffer and write to the source buffer. pbBuffer = new BYTE[ dwBufferLen ];
BOOL bFinal = TRUE;
BYTE *pEofDestBuf = pDesBuf + nDesBufLen;
nDesBufLen = 0; do
{ //--------------------------------------------------------------------
// Read up to dwBlockLen bytes from the source buffer.
bFinal = ( (DWORD)nSourceBufLen <= dwBufferLen) ? TRUE : FALSE;
dwCount = bFinal ? nSourceBufLen : dwBufferLen;
nSourceBufLen -= dwCount; //--------------------------------------------------------------------
// 加密数据
memcpy( pbBuffer, pSourceBuf , dwCount);
pSourceBuf += dwCount; if(!CryptEncrypt(
hKey, //密钥
0, //如果数据同时进行散列和加密,这里传入一个散列对象
bFinal, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
0, //保留
pbBuffer, //输入被加密数据,输出加密后的数据
&dwCount, //输入被加密数据实际长度,输出加密后数据长度
dwBufferLen)) //pbBuffer的大小。
{
throw _T("Error during CryptEncrypt. \n");
} //--------------------------------------------------------------------
// Write data to the destination file.
if ( pDesBuf + dwCount <= pEofDestBuf)
{
memcpy( pDesBuf, pbBuffer, dwCount);
pDesBuf += dwCount;
nDesBufLen += dwCount;
}
else
{
throw _T("Out of memory");
}
}
while( !bFinal );
//--------------------------------------------------------------------
// End the do loop when the last block of the source file has been
// read, encrypted, and written to the destination file.
hr = S_OK;
}
catch(...)
{
hr = E_FAIL;
}
// Free memory.
delete []pbBuffer;
pbBuffer = NULL; //--------------------------------------------------------------------
// Destroy session key. if(hKey)
{
CryptDestroyKey(hKey);
} //--------------------------------------------------------------------
// Destroy hash object. if(hHash)
{
CryptDestroyHash(hHash);
}
//--------------------------------------------------------------------
// Release provider handle. if(hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
} return hr;
}
首先获取一个随机数,获取dest,再利用一个循环取你的密码(Src)的每一个字符,加上获取随机数的dest部分,再mod255,得到SrcAsc ;接着通过SrcAsc与旧的密文求异或获得TmpSrcAsc,最后再加上dest,最后循环。
大概意思就是这样,顺着这个写,就可以得到c++的源码,何况你是懂delphi的
HRESULT CCryptTool::EncryptString(const CString& strSource, CString& strDest, const CString& strPsw )
{
USES_CONVERSION; CBase64Tool base64Tool;
CString strTemp; HRESULT hr = E_FAIL;
byte *pDestBuffer = NULL; strDest.Empty();
if ( strSource.IsEmpty() )
{
return S_FALSE;
} try
{
int nSourceBufLen = strSource.GetLength() * sizeof( WCHAR ); // unicode
int nDestBufLen = nSourceBufLen ;
if ( (nDestBufLen % ENCRYPT_BLOCK_SIZE) != 0 )
{
nDestBufLen += ENCRYPT_BLOCK_SIZE - (( nSourceBufLen) % ENCRYPT_BLOCK_SIZE);
} if ( ENCRYPT_BLOCK_SIZE > 1 )
{
nDestBufLen += ENCRYPT_BLOCK_SIZE;
} LPWSTR lpszSource = T2W( (LPTSTR)(LPCTSTR)strSource ); pDestBuffer = new byte[nDestBufLen + sizeof( WCHAR )];
ZeroMemory( pDestBuffer, nDestBufLen + sizeof( WCHAR )); VERIFY_SUCCEEDED( EncryptByte( reinterpret_cast<unsigned char *>(lpszSource), nSourceBufLen, pDestBuffer, nDestBufLen, strPsw) );
VERIFY_SUCCEEDED( base64Tool.Encode_Base64( pDestBuffer, nDestBufLen, strDest) ); hr = S_OK;
}
catch( CException *e)
{
e->Delete();
}
catch(...)
{ }
delete []pDestBuffer;
return hr;
}
原理就是设置了一个暗密钥。条件:原文,明密钥,暗密钥位数N
加密过程:
1,随机生成一个N位的整数作为暗密钥。
2,用明密钥和暗密钥组合起来,随便设计一个加密方法,生成密文。
3,最终密文 = 暗密钥加上面的密文;解密过程:
1,取密文的前N位,得到暗密钥。
2,对加密过程2进行逆操作,得到明文。
{
long nCount = 0;
int nSourceBufLength = strSource.GetLength();
if ( strSource.GetLength() >= 2 && strSource.Right(2) == _T("==") )
{
nCount = 2;
}
else
{
if ( strSource.GetLength() >= 1 && strSource.Right(1) == _T("=") )
{
nCount = 1;
}
}
int nBufLengthNeed = (nSourceBufLength / 4) * 3 - nCount;
if ( nDestBufLength <= 0)
{
nDestBufLength = nBufLengthNeed;
return ((nDestBufLength == 0) ? E_FAIL : S_OK );
} if ( nDestBufLength < nBufLengthNeed )
{
return E_INVALIDARG;
} if( pDestBuf == NULL )
{
return E_INVALIDARG;
} nDestBufLength = nBufLengthNeed;
//
//
int num[4];
long nIndex = 0;
const TCHAR* p = (LPCTSTR)strSource;
long nLen = 0;
int i = 0;
int nBytesCount = 3; for( nLen = nSourceBufLength ; nLen >= 4; nLen -= 4, p += 4 )
{
for( i = 0; i < 4; ++i )
{
BOOL bEqualMark = FALSE; if( p[i] >= _T('A') && p[i] <= _T('Z') )
{
num[i] = p[i] - 65;
}
else if ( p[i] >= _T('a') && p[i] <= _T('z') )
{
num[i] = p[i] - 71;
}
else if( p[i] >= _T('0') && p[i] <= _T('9') )
{
num[i] = p[i] + 4;
}
else if( p[i] == _T('+') )
{
num[i] = 62;
}
else if( p[i] == _T('/') )
{
num[i] = 63;
}
else if( p[i] == _T('=') )
{
num[i] = 0;
nBytesCount--;
bEqualMark = TRUE;
}
else
{
// invalidate character
return E_INVALIDARG;
}
if ( nBytesCount <= 0 )
{
return E_FAIL;
} if ( nBytesCount < 3 && !bEqualMark)
{
// nBytesCount 小于3, 表明结束标志符'='已经出现,
// !bEqualMark 表示在'='结束符之后又出现了'='之外的字符
// 此种情况为非法情况,该字符串并不是合法的base64编码字符串
return E_INVALIDARG;
}
} pDestBuf[nIndex++] = (num[0] << 2) + ((num[1] & 0x30) >> 4);
if ( nBytesCount >= 2 )
{
pDestBuf[nIndex++] = ((num[1] & 0x0F) << 4) + ((num[2] & 0x3C) >> 2);
}
if ( nBytesCount >= 3 )
{
pDestBuf[nIndex++] = ((num[2] & 0x03) << 6) + num[3];
}
}
return S_OK;
}HRESULT CBase64Tool::Encode_Base64(const byte *pSourceBuf, int nSourceBufLength, CString& strEncode)
{
if( pSourceBuf == NULL || nSourceBufLength <= 0 )
{
return E_INVALIDARG;
}
long nLen = 0;
int nIndex = 0;
const byte* p = pSourceBuf;
int nDestBufLength = 1 + ((nSourceBufLength + ( 3 - nSourceBufLength % 3)) * 4 / 3 ); TCHAR *to = strEncode.GetBuffer( nDestBufLength ); for( nLen = nSourceBufLength; nLen >= 3; nLen -= 3, p += 3 )
{
to[nIndex++] = char_map[(p[0] & 0xFC) >> 2];
to[nIndex++] = char_map[((p[0] & 0x03) << 4) | ((p[1] & 0xF0) >> 4)];
to[nIndex++] = char_map[((p[1] & 0x0F) << 2) | ((p[2] & 0xC0) >> 6)];
to[nIndex++] = char_map[p[2] & 0x3F];
}
if( nLen == 2 )
{
to[nIndex++] = char_map[(p[0] & 0xFC) >> 2];
to[nIndex++] = char_map[((p[0] & 0x03) << 4) | ((p[1] & 0xF0) >> 4)];
to[nIndex++] = char_map[(p[1] & 0x0F) << 2];
to[nIndex++] = '=';
}
else if( nLen == 1 )
{
to[nIndex++] = char_map[(p[0] & 0xFC) >> 2];
to[nIndex++] = char_map[(p[0] & 0x03) << 4];
to[nIndex++] = '=';
to[nIndex++] = '=';
}
to[nIndex++] = _T('\0');
strEncode.ReleaseBuffer();
return S_OK;
}
{
public:
static const TCHAR char_map[];public:
// base64解码。
// pDestBuf : 解码后的数据缓冲区
// nDestBufLength : 解码缓冲区的长度。函数调用返回后,nDestBufLength为实际数据的长度
// 如果 nDestBufLength = 0, 则函数通过nDestBufLength返回解码所需缓冲区的长度
HRESULT Decode_Base64( const CString& strSource, byte *pDestBuf, int& nDestBufLength); HRESULT Encode_Base64( const byte *pSourceBuf, int nSourceBufLength, CString& strEncode );
};class CCryptTool
{
public:
HRESULT DecryptFile(const CString &strSourceFileName, const CString &strDestFileName, const CString &strPsw);
HRESULT DecryptString(const CString& strSource, CString& strDest1, const CString& strPsw );
HRESULT DecryptByte(byte *pSourceBuf, int nSourceBufLen, byte *pDesBuf, int nDesBufLen, const CString &strPsw);
HRESULT EncryptFile(const CString& strSourceFileName, const CString& strDestFileName, const CString& strPsw);
HRESULT EncryptString( const CString& strSource, CString& strDest, const CString& strPsw);
HRESULT EncryptByte( byte* pSourceBuf, int nSourceBufLen, byte* pDesBuf, int& nDesBufLen, const CString& strPsw);
protected:
static const int m_nBlockSize;
};
CString EncrypKey(const CString& strSrc, CString strKey)
{
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("%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("%1.2x", nSrcAsc);
strDest += strTemp;
nOffset = nSrcAsc;
}
return strDest;
}CString UncrypKey(const CString& strSrc, CString strKey)
{
int nKeyLen = strKey.GetLength();
if (nKeyLen == 0)
strKey = _T("xxsswww");
char *stopstring;
int nOffset = strtoul(strSrc.Mid(0,2), &stopstring, 16);
int nSrcPos = 2;
int nSrcAsc = 0;
int nKeyPos = 0;
int nTempSrcAsc = 0;
CString strDest;
do
{
nSrcAsc = strtoul(strSrc.Mid(nSrcPos,2), &stopstring, 16);
if (nSrcAsc == 0) break;
if (nKeyPos < nKeyLen-1)
nKeyPos += 1;
else
nKeyPos = 1;
nTempSrcAsc = nSrcAsc ^ strKey[nKeyPos];
if (nTempSrcAsc <= nOffset)
{
nTempSrcAsc = 255 + nTempSrcAsc - nOffset;
}
else
nTempSrcAsc -= nOffset;
CString strTemp;
strTemp.Format("%c",nTempSrcAsc);
strDest += strTemp;
nOffset = nSrcAsc;
nSrcPos += 2; } while(nSrcPos <= strSrc.GetLength()); return strDest;
}字符没问题,但是汉字有问题,就是把你的代码转译为了c++而已,测试没问题
{
int nKeyLen = strKey.GetLength();
if (nKeyLen == 0)
strKey = _T("xxsswww");
char *stopstring;
int nOffset = strtoul(strSrc.Mid(0,2), &stopstring, 16);
int nSrcPos = 2;
int nSrcAsc = 0;
int nKeyPos = 0;
int nTempSrcAsc = 0;
CString strDest;
do
{
nSrcAsc = strtoul(strSrc.Mid(nSrcPos,2), &stopstring, 16);
//if (nSrcAsc == 0) break;
if (nKeyPos < nKeyLen-1)
nKeyPos += 1;
else
nKeyPos = 1;
nTempSrcAsc = nSrcAsc ^ strKey[nKeyPos];
if (nTempSrcAsc <= nOffset)
{
nTempSrcAsc = 255 + nTempSrcAsc - nOffset;
}
else
nTempSrcAsc -= nOffset;
CString strTemp;
strTemp.Format("%c",nTempSrcAsc);
strDest += strTemp;
nOffset = nSrcAsc;
nSrcPos += 2; } while(nSrcPos <= strSrc.GetLength());
strDest=strDest.Mid(0,(strSrc.GetLength()-2)/2);
return strDest;
} 这样就没问题鸟.