【Resharper使用技巧】

项目中添加一个类:Ctrl+Alt+Insert
提取一个方法成接口:Ctrl+Shift+R
快速找到并显示成员或变量的所有用法:Alt+F7
变量转到定义、转到该变量定义类的位置、查看该变量那些地方引用:Ctrl+Shift+G
该类或方法在哪里引用:Ctrl+N
资源管理器将定位到当前文件:Shift+Alt+L
移动代码块:Ctrl+Shift+Alt
查看类的结构和方法:Ctrl + F11
构造函数,属性,重写成员:Alt+Insert
整理代码:Ctrl+Alt+L
if、while、fori、try-catch:Ctrl+Alt+J包围代码
自动补全:Ctrl+Alt+V
变干净以及变规范:Ctrl + Alt + F
详细攻略
下载地址
删除提示线的方法
【Resharper使用技巧】_第1张图片

#include 
#include 

#include 
#include 
#include 
#include 
#pragma warning(disable:4996)
extern "C"
{
#include 
};

#define DATA_BUFF_LENTH 129
#define RSA_BIT 1024

using namespace std;

typedef ios_base::openmode _Ios_Openmode;

class File
{
public:
	File(string strFilePath) :m_strFilePath(strFilePath) {};
	~File() {};
	bool open(_Ios_Openmode opreator);
	void close();
	void read(char* buff, int length);
private:
	fstream m_file;
	string m_strFilePath;
};

bool File::open(_Ios_Openmode opreator)
{
	m_file.open(m_strFilePath.c_str(), opreator);
	if (m_file.fail())
	{
		cout << "open file failure!" << endl;
		return false;
	}
	return true;
}

void File::close()
{
	m_file.close();
}

void File::read(char* buff, int length)
{
	m_file.read(buff, length);
}

class RSAKey
{
public:
	RSAKey(int rsabit, int bignum)
	{
		int r;

		m_rsa_bit = rsabit;
		m_rsa = RSA_new();
		m_pubKey = RSA_new();
		m_privateKey = RSA_new();
		m_bigNum = BN_new();

		BN_set_word(m_bigNum, bignum);

		r = RSA_generate_key_ex(m_rsa, m_rsa_bit, m_bigNum, NULL);

		if (r != 0) {
			printf("RSA_generate_key_ex error\n");
		}
	}

	~RSAKey()
	{
		RSA_free(m_rsa);
		RSA_free(m_pubKey);
		RSA_free(m_privateKey);
		BN_free(m_bigNum);
	}

	void exportPrivateKey(string fileName);
	void exportPublicKey(string fileName);
	void importPrivateKey(string fileName);
	void importPublicKey(string fileName);
	void printPublicKey();
	void printPrivateKey();

	void UsePrivateRSAKeyDecode(char* dsc, char* src)
	{
		int rsa_len = RSA_size(m_privateKey);
		int len = RSA_private_decrypt(rsa_len, (unsigned char*)src, (unsigned char*)dsc, m_privateKey, RSA_NO_PADDING);

		if (len < rsa_len) {
			return;
		}
	}

	void UsePublicRSAKeyEncode(char* dsc, char* src)
	{
		int rsa_len = RSA_size(m_pubKey);
		int len = RSA_public_encrypt(rsa_len, (unsigned char*)src, (unsigned char*)dsc, m_pubKey, RSA_NO_PADDING);

		if (len < rsa_len) {
			return;
		}
	}

	void readCiphertext(char* buff, int length, string fileName)
	{
		FILE* ifile = fopen(fileName.c_str(), "rb");
		if (ifile == NULL) {
			cout << "Failed to open file" << endl;
			return;
		}
		int i = 0;
		while (i < DATA_BUFF_LENTH) {
			unsigned char c;
			fread(&c, sizeof(c), 1, ifile);
			buff[i++] = (char)c;
		}
		fclose(ifile);
	}

	void writeCiphertext(char* buff, int length, string fileName)
	{
		FILE* ofile = fopen(fileName.c_str(), "wb");
		if (ofile == NULL) {
			cout << "Failed to open file" << endl;
			return;
		}
		for (int i = 0; i < DATA_BUFF_LENTH; i++) {
			unsigned char c = buff[i];
			fwrite(&c, sizeof(c), 1, ofile);
		}
		fclose(ofile);
	}

	void readCiphertextEx(char* buff, int length, string fileName)
	{
		FILE* ifile = fopen(fileName.c_str(), "r");
		if (ifile == NULL) {
			cout << "Failed to open file" << endl;
			return;
		}
		int i = 0;
		while (i < DATA_BUFF_LENTH) {
			int num;
			fscanf(ifile, "%d", &num);
			buff[i++] = num;
		}
		fclose(ifile);
	}

	void writeCiphertextEx(char* buff, int length, string fileName)
	{
		FILE* ofile = fopen(fileName.c_str(), "w");
		if (ofile == NULL) {
			cout << "Failed to open file" << endl;
			return;
		}
		for (int i = 0; i < DATA_BUFF_LENTH; i++) {
			unsigned char c = (char)buff[i];
			fprintf(ofile, "%d \n", c);
		}
		fclose(ofile);
	}
private:
	BIGNUM* m_bigNum;
	RSA* m_rsa;
	int m_rsa_bit;
	RSA* m_pubKey;
	RSA* m_privateKey;
};


void RSAKey::printPublicKey()
{
	RSA_print_fp(stdout, m_pubKey, 11);
}

void RSAKey::printPrivateKey()
{
	RSA_print_fp(stdout, m_privateKey, 11);
}

void RSAKey::exportPrivateKey(string fileName)
{
	if (1) {
		BIO* out;

		//long BIO_get_mem_data(BIO *b, char **pp);
		//BIO_set_mem_buf(BIO *b, BUF_MEM *bm, int c);
		//BIO_get_mem_ptr(BIO *b, BUF_MEM **pp);

		out = BIO_new(BIO_s_mem()); //BIO_s_file()

		if (out) {
			int r = PEM_write_bio_RSAPrivateKey(out, m_rsa, NULL, NULL, 0, NULL, NULL);

			if (r) {
				char* data;
				long len;

				if (0) {
					//int read = BIO_nread0(out, &data);
					len = BIO_get_mem_data(out, &data);
				}
				else {
					BUF_MEM* mem;
					int rr = BIO_get_mem_ptr(out, &mem);
					len = mem->length;
					data = mem->data;
				}

				FILE* ifile;
				ifile = fopen(fileName.c_str(), "wb");

				if (ifile) {
					fwrite(data, 1, len, ifile);
					fclose(ifile);
				}

				BIO_free(out);
			}
		}
	}
}

void RSAKey::exportPublicKey(string fileName)
{
	FILE* ifile;

	ifile = fopen(fileName.c_str(), "wb");

	if (ifile) {
		PEM_write_RSAPublicKey(ifile, m_rsa);

		fclose(ifile);
	}
}

void RSAKey::importPrivateKey(string fileName)
{
	FILE* ifile;
	ifile = fopen(fileName.c_str(), "rb");

	if (0) {
		m_privateKey = PEM_read_RSAPrivateKey(ifile, NULL, NULL, NULL);
	}
	else {
		char* key;
		int key_len, readlen, r;

		r = fseek(ifile, 0, SEEK_END);
		key_len = ftell(ifile);
		key = (char*)malloc(key_len + 1);
		fseek(ifile, 0, SEEK_SET);
		readlen = fread(key, key_len, 1, ifile);
		key[key_len] = '\0';

		BIO* b = BIO_new_mem_buf((void*)key, key_len);
		if (b) {
			m_privateKey = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL);

			BIO_free(b);
		}
	}

	fclose(ifile);
}

void RSAKey::importPublicKey(string fileName)
{
	FILE* ifile;
	ifile = fopen(fileName.c_str(), "rb");
	m_pubKey = PEM_read_RSAPublicKey(ifile, NULL, NULL, NULL);

	fclose(ifile);
}

class FileReader {
public:

	static std::string readFileAndPrint(const std::string& filename)
	{
		char DataBuff[DATA_BUFF_LENTH];
		std::ifstream file(filename);

		if (!file.is_open()) {
			std::cerr << "无法打开文件: " << filename << std::endl;
			return "";
		}

		std::string content;
		while (!file.eof()) {
			file.read(DataBuff, (DATA_BUFF_LENTH - 1));
			std::streamsize bytesRead = file.gcount();
			DataBuff[bytesRead] = '\0';  // 添加空字符
			content += DataBuff;
			//std::cout << content;
		}

		file.close();
		return content;
	}

	static void deleteFile(const std::string& filename)
	{
		if (remove(filename.c_str()) != 0) {
			std::cerr << "删除文件失败: " << filename << std::endl;
		}
		else {
			std::cout << "成功删除文件: " << filename << std::endl;
		}
	}

	static void modifyImage(const std::string& inputFilename, const std::string& outputFilename)
	{
		cv::Mat img = cv::imread(inputFilename);

		if (img.empty()) {
			std::cerr << "无法打开图像: " << inputFilename << std::endl;
			return;
		}

		for (int y = 0; y < img.rows; ++y) {
			for (int x = 0; x < img.cols; ++x) {
				cv::Vec3b& pixel = img.at(y, x);

				for (int c = 0; c < 3; ++c) {
					int value = pixel[c];
					int tensAndAbove = value / 10 * 10;

					if (c == 0) { // Blue
						pixel[c] = tensAndAbove + 1;
					}
					else if (c == 1) { // Green
						pixel[c] = tensAndAbove + 2;
					}
					else if (c == 2) { // Red
						pixel[c] = tensAndAbove + 3;
					}
				}
			}
		}

		if (!cv::imwrite(outputFilename, img)) {
			std::cerr << "无法保存图像: " << outputFilename << std::endl;
		}
	}
	//这是一个基于C++的静态方法,用于获取char值的个位、十位和百位。如果没有十位或百位,该方法将返回零。
	static int getDigit(unsigned char value, int place)
	{
		int intValue = static_cast(value);
		for (int i = 0; i < place; ++i) {
			intValue /= 10;
		}
		return intValue % 10;
	}
	//这是一个基于C++的静态方法,用于修改int值的个位数,同时保持其他位数不变。
	static int modifyOnesDigit(int value, int newOnesDigit)
	{
		return value / 10 * 10 + newOnesDigit;
	}

	static void ReadPic(string keyPath, const std::string& inImage, const std::string& strOutPaht)
	{
		std::ofstream file(strOutPaht, std::ios_base::app);
		if (!file.is_open()) {
			std::cerr << "无法打开文件: " << strOutPaht << std::endl;
			return;
		}
		size_t start = 0;

		std::string strContest;
		FILE* ifile;
		ifile = fopen(keyPath.c_str(), "rb");
		RSA* privateKey = NULL;
		char* key;
		int key_len, readlen, r;

		r = fseek(ifile, 0, SEEK_END);
		key_len = ftell(ifile);
		key = (char*)malloc(key_len + 1);
		fseek(ifile, 0, SEEK_SET);
		readlen = fread(key, key_len, 1, ifile);
		key[key_len] = '\0';

		BIO* b = BIO_new_mem_buf((void*)key, key_len);
		if (b) {
			privateKey = PEM_read_bio_RSAPrivateKey(b, NULL, NULL, NULL);
			BIO_free(b);
		}
		fclose(ifile);

		cv::Mat img = cv::imread(inImage);

		if (img.empty()) {
			std::cerr << "无法打开图像: " << inImage << std::endl;
			return;
		}
		int index = 0;
		char DataBuff[DATA_BUFF_LENTH];
		char dsc[DATA_BUFF_LENTH];
		for (int y = 0; y < img.rows; ++y) {
			for (int x = 0; x < img.cols; ++x) {
				cv::Vec3b& pixel = img.at(y, x);
				unsigned char ucValue = 0;
				for (int c = 0; c < 3; ++c) {
					int value = pixel[c];
					if (c == 0) {     // Blue
						ucValue += getDigit(pixel[c], 0) * 100;
					}
					else if (c == 1) { // Green
						ucValue += getDigit(pixel[c], 0) * 10;
					}
					else if (c == 2) { // Red
						ucValue += getDigit(pixel[c], 0);
					}
				}
				if (index >= DATA_BUFF_LENTH)
				{
					int rsa_len = RSA_size(privateKey);
					//不加密start
					//memcpy(dsc, DataBuff, DATA_BUFF_LENTH);
					//不加密end
					//加密start
					int len = RSA_private_decrypt(rsa_len, (unsigned char*)DataBuff, (unsigned char*)dsc, privateKey, RSA_NO_PADDING);
					if (len < rsa_len) {
						return;
					}
					//加密end
					file.write(dsc, DATA_BUFF_LENTH - 1);
			
					index = 0;
				}
				DataBuff[index++] = static_cast(ucValue);
			}
		}
		file.close();
	}

	static void writePic(string keyPath, const std::string& filename, const std::string& inImage, const std::string& outImage)
	{
		FILE* ifile;
		ifile = fopen(keyPath.c_str(), "rb");
		RSA* pPubKey = PEM_read_RSAPublicKey(ifile, NULL, NULL, NULL);
		fclose(ifile);
		int rsa_len = RSA_size(pPubKey);

		char DataBuff[DATA_BUFF_LENTH];
		std::ifstream file(filename);

		cv::Mat img = cv::imread(inImage);

		if (img.empty()) {
			std::cerr << "无法打开图像: " << inImage << std::endl;
			return;
		}

		if (!file.is_open()) {
			std::cerr << "无法打开文件: " << filename << std::endl;
		}

		char dsc[DATA_BUFF_LENTH];
		int nCount = img.rows * img.cols;
		int nIndex = 0;
		while (!file.eof()) {
			file.read(DataBuff, (DATA_BUFF_LENTH - 1));
			std::streamsize bytesRead = file.gcount();
			DataBuff[bytesRead] = '\0';  // 添加空字符
			//不加密start
			//memcpy(dsc, DataBuff, DATA_BUFF_LENTH);
			//不加密end
			//加密start
			int len = RSA_public_encrypt(rsa_len, (unsigned char*)DataBuff, (unsigned char*)dsc, pPubKey, RSA_NO_PADDING);
			dsc[128] = '\0';
			if (len < rsa_len) {
				break;
			}
			//加密end
			if ((nIndex + DATA_BUFF_LENTH) >= nCount)
			{
				break;
			}
			for (int k = 0; k < DATA_BUFF_LENTH; k++)
			{
				unsigned char  ucValue = static_cast(dsc[k]);
				int y = nIndex / img.cols;
				int x = nIndex % img.cols;
				cv::Vec3b& pixel = img.at(y, x);
				for (int c = 0; c < 3; ++c) {
					if(pixel[c] >240)
					{
						pixel[c] -= 10;
					}
					if (c == 0) {      // Blue
						int digit = getDigit(ucValue, 2);
						int value = modifyOnesDigit(pixel[c], digit);
						pixel[c] = value;
					}
					else if (c == 1) { // Green
						int digit = getDigit(ucValue, 1);
						int value = modifyOnesDigit(pixel[c], digit);
						pixel[c] = value;
					}
					else if (c == 2) { // Red
						int digit = getDigit(ucValue, 0);
						int value = modifyOnesDigit(pixel[c], digit);
						pixel[c] = value;
					}
				}
				nIndex++;
			}
		}
		file.close();

		if (!cv::imwrite(outImage, img)) {
			std::cerr << "无法保存图像: " << outImage << std::endl;
		}
	}

	static unsigned char charToUnsignedChar(char value) {
		return static_cast(value);
	}

	static char unsignedCharToChar(unsigned char value) {
		return static_cast(value);
	}
};

void MainTest()
{
	RSAKey rsa(RSA_BIT, RSA_F4);

	//rsa.exportPrivateKey("private.key");
	//rsa.exportPublicKey("public.key");

	rsa.importPublicKey("public.key");
	rsa.importPrivateKey("private.key");

	rsa.printPrivateKey();
	rsa.printPublicKey();

	char DataBuff[DATA_BUFF_LENTH];

	for (size_t i = 0; i < DATA_BUFF_LENTH; i++) {
		DataBuff[i] = 'A' + char(i % 26);
	}

	char enData[DATA_BUFF_LENTH];
	rsa.UsePublicRSAKeyEncode(enData, DataBuff);
	enData[128] = '\0';
	cout << "-----------------------------------" << endl;
	cout << "encode :" << enData << endl;
	cout << "-----------------------------------" << endl;
	rsa.writeCiphertextEx(enData, DATA_BUFF_LENTH, "ciphertext.txt");
	rsa.readCiphertextEx(DataBuff, DATA_BUFF_LENTH, "ciphertext.txt");

	char deData[DATA_BUFF_LENTH];
	rsa.UsePrivateRSAKeyDecode(deData, DataBuff);
	deData[128] = '\0';
	cout << "-----------------------------------" << endl;
	cout << "decode :" << deData << endl;
	cout << "-----------------------------------" << endl;
}

int main(int argc, char* argv[])
{
	//MainTest();
	FileReader::deleteFile("D:/step1out.png");
	FileReader::deleteFile("D:/XRBuildMapCopy.cpp");
	FileReader::writePic("public.key", "public.key", "D:/step1.png", "D:/step1out.png");
	FileReader::ReadPic("private.key", "D:/step1out.png", "D:/XRBuildMapCopy.cpp");

	//FileReader::modifyImage("D:/step1.png", "D:/step1out.png");
	//FileReader::modifyImage("D:/step1out2.png", "D:/step1out.png");
	return 0;
}

-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAN1YwhRPXSQocld8zFJ3FgezaPsudgqmUWLpRG62X3bRV4TQvbtUpa+h
73YFVfBY9cIOr9H1lWY0SJpR2lfA0INoJT04UrHy+O/43XjFBPXAcgdVk55PYHWs
Vr9yGJuXNshVHmjR3k3tHrjFzMJGXUux5wJrZHP2Z65n3hEIVYLfAgMBAAE=
-----END RSA PUBLIC KEY-----

你可能感兴趣的:(C++入门到精通,tornado,plotly)