⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trstr.cpp

📁 linux 下 源代码测试系统 用 tar 打开
💻 CPP
字号:
/* * $File: trstr.cpp * $Author: Jiakai -- gy_jk@126.com * $Date: Sat Aug 02 21:45:19 2008 *//*Copyright (C) (2008) (Jiakai) <gy_jk@126.com>This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "trstr.h"#include "common.h"#include <zlib.h>#include <cstdio>#include <cstring>using namespace std;//-------------Data:----------------// |---p1----| |----p2---| |---p3--|// 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 x// |---char 0----| |----char 1---| (to mark weather char 1 is added to make the length even.)//-------------------------------Function Statements---------------------static void encrypt_uchar(const Byte *source, unsigned int slen, string &dest);static void initdict_encrypt(char dict[64]);static bool decrypt_str(const string &ciphertext, Byte *dest, Bytes_len &dest_len);static void initdict_decrypt(Byte dict[256]);//-----------------------------------------------------------------------bool encrypt(const Byte *plaintext, Bytes_len plaintext_len, string &ciphertext){	Bytes_len dest_len = plaintext_len + 12;	dest_len += dest_len / 100;	Byte *dest = new Byte[dest_len];	if (compress2(dest, &dest_len, plaintext, plaintext_len, 9) != Z_OK)	{		delete []dest;		return false;	}	encrypt_uchar(dest, dest_len, ciphertext);	delete []dest;	char tmp[STRING_MAX_LEN];	sprintf(tmp, "%lu ", plaintext_len);	ciphertext.insert(0, tmp);	return true;}Byte * decrypt(const string &ciphertext, Bytes_len &plaintext_len){	Byte *source = new Byte[ciphertext.length() / 3 * 2];	Bytes_len source_len;	string::size_type realpos = 1;	while (ciphertext[realpos - 1] != ' ')		realpos ++;	if (!decrypt_str(ciphertext.substr(realpos), source, source_len) || sscanf(ciphertext.c_str(), "%lu", &plaintext_len) == EOF)	{		delete []source;		return NULL;	}	Byte *plaintext = new Byte[plaintext_len];	if (uncompress(plaintext, &plaintext_len, source, source_len)!= Z_OK)	{		delete []source;		delete []plaintext;		return NULL;	}	delete []source;	return plaintext;}void encrypt_uchar(const Byte *source, unsigned int slen, string &dest){	dest.erase();	static bool dict_inited = false;	static char dict[64];	if (!dict_inited)		initdict_encrypt(dict);	int t = slen / 2;	for (int i = 0; i < t; i ++)	{		const Byte *data = &source[i * 2];		unsigned int p1 = (*data >> 2);		unsigned int p2 = (*data & 0x03) << 4;		data ++;		p2 |= (*data >> 4);		unsigned int p3 = (*data & 0x0F) << 1;		dest.append(1, dict[p1]);		dest.append(1, dict[p2]);		dest.append(1, dict[p3]);	}	if (slen % 2)	{		const Byte *data = &source[slen - 1];		unsigned int p1 = (*data >> 2);		unsigned int p2 = (*data & 0x03) << 4;		unsigned int p3 = 1;		dest.append(1, dict[p1]);		dest.append(1, dict[p2]);		dest.append(1, dict[p3]);	}}void initdict_encrypt(char dict[64]){	int len = 0;	for (char i = 'a'; i <= 'z'; i ++)		dict[len ++] = i;	for (char i = 'A'; i <= 'Z'; i ++)		dict[len ++] = i;	for (char i = '0'; i <= '9'; i ++)		dict[len ++] = i;	dict[len ++] = ':';	dict[len ++] = ';';}bool decrypt_str(const string &ciphertext, Byte *dest, Bytes_len &dest_len){	static bool dict_inited = false;	static Byte dict[256];	if (!dict_inited)		initdict_decrypt(dict);	dest_len = 0;	if (ciphertext.length() % 3)		return false;	string::size_type t = ciphertext.length() / 3;	for (string::size_type i = 0; i < t; i ++)	{		Byte p1 = dict[(unsigned int)ciphertext[i * 3]];		Byte p2 = dict[(unsigned int)ciphertext[i * 3 + 1]];		Byte p3 = dict[(unsigned int)ciphertext[i * 3 + 2]];		dest[dest_len ++] = (p1 << 2) | (p2 >> 4);		dest[dest_len ++] = ((p2 & 0x0F) << 4) | (p3 >> 1);	}	if (dict[(unsigned int)*ciphertext.rbegin()] & 1)		dest_len --;	return true;}void initdict_decrypt(Byte dict[256]){	memset(dict, 0, sizeof(dict));	Byte value = 0;	for (char i = 'a'; i <= 'z'; i ++)		dict[(unsigned int)i] = value++;	for (char i = 'A'; i <= 'Z'; i ++)		dict[(unsigned int)i] = value++;	for (char i = '0'; i <= '9'; i ++)		dict[(unsigned int)i] = value++;	dict[(unsigned int)':'] = value ++;	dict[(unsigned int)';'] = value ++;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -