#include #include #include #include using namespace std; char* RotN( char* szText, int N ); std::string base64( const char *lpszData, unsigned long nDataLen ); int main( int argc, char* argv[] ) { char* buffer = {0}; printf("argv[1] -> %s\n", argv[1]); printf("RotN(base64(argv[1], strlen(argv[1])), N) -> %s%s",argv[2],RotN((char*)base64((const char*)argv[1], strlen(argv[1])).c_str(), atoi(argv[2]))); return 0; } char* RotN( char* szText, int N ) { char szBuf[512]; char cap, byte; for(int i = 0; i < (int)strlen(szText); i++) { byte = szText[i]; cap = byte & 32; byte &= ~cap; byte = ((byte >= 'A') && (byte <= 'Z') ? ((byte - 'A' + N) % 26 + 'A') : byte) | cap; szBuf[i] = byte; } szBuf[i] = 0x00; return szBuf; } std::string base64( const char *lpszData, unsigned long nDataLen ) { static char Base64CharTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; // reserve memory std::string sBase64; sBase64.reserve( ( nDataLen ) ); // encode unsigned long nLinePos = 0; while( nDataLen > 0 ) { unsigned long n; // get 24 bits // - get bytes available unsigned long nBytesCount; if( nDataLen >= 3 ) nBytesCount = 3; else if( nDataLen == 2 ) nBytesCount = 2; else if( nDataLen == 1 ) nBytesCount = 1; unsigned long nBase64Blocks = ceil( ( nBytesCount * 8.0 / 6.0 ) ); // - read char Bytes[ 3 ]; for( n = 0; n < nBytesCount; n++ ) Bytes[ n ] = lpszData[ n ]; lpszData += nBytesCount; nDataLen -= nBytesCount; // encode char Base64TableIndex[ 4 ]; Base64TableIndex[ 0 ] = ( Bytes[0] & 0xfc ) >> 2; Base64TableIndex[ 1 ] = ( ( Bytes[ 0 ] & 0x03 ) << 4 ) + ( ( Bytes[ 1 ] & 0xf0 ) >> 4 ); Base64TableIndex[ 2 ] = ( ( Bytes[ 1 ] & 0x0f ) << 2 ) + ( ( Bytes[ 2 ] & 0xc0 ) >> 6 ); Base64TableIndex[ 3 ] = Bytes[ 2 ] & 0x3f; for( n = 0; n < nBase64Blocks; n++ ) { sBase64.append( 1, Base64CharTable[ Base64TableIndex[ n ] ] ); nLinePos++; } // lines /* if( nLinePos >= 72 ) { sBase64.append( lpszEndOfLine ); nLinePos = 0; } */ // add padding for( n = nBase64Blocks; n < 4; n++ ) sBase64.append( 1, '=' ); } return sBase64; }