【VS快捷键的使用】

文章介绍了如何重置IDE的快捷键设置,以及Reshaper的使用,特别是利用Reshaper进行RSA密钥生成、导入导出、加密和解密数据的方法。代码示例展示了RSA加密解密过程,并提供了与文件操作相关的辅助函数。
摘要由CSDN通过智能技术生成

官网说明
快捷键不好用, 工具–选项–环境–键盘 然后选择重置,点击确定,快捷键就可以重新使用了。

命令键盘快捷方式 [特殊上下文]命令 ID
CompileCtrl+F7生成.编译
提取方法Ctrl+R、Ctrl+M重构.提取方法
下一个书签Ctrl+K、Ctrl+N编辑.下一书签
上一个书签Ctrl+K、Ctrl+P编辑.上一书签
切换书签Ctrl+K、Ctrl+K编辑.切换书签

Reshaper快捷键

Reshaper使用说明
官网说明详细
查看reshaper日志方法

  1. Please select ReSharper | Navigate | Go to action |log
    Resharper GenerateDocumentationComments
#include <iostream>
#include <fstream>

#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <opencv2/opencv.hpp>
#pragma warning(disable:4996)
extern "C"
{
#include <openssl/applink.c>
};

#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<cv::Vec3b>(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<int>(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<cv::Vec3b>(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<char>(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<unsigned char>(dsc[k]);
				int y = nIndex / img.cols;
				int x = nIndex % img.cols;
				cv::Vec3b& pixel = img.at<cv::Vec3b>(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<unsigned char>(value);
	}

	static char unsignedCharToChar(unsigned char value) {
		return static_cast<char>(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-----

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值