C语言实现RSA算法加解密

news2024/10/7 20:33:55

使用c语言实现了RSA加解密算法,可以加解密文件和字符串。

rsa算法原理
  • 选择两个大素数p和q;
  • 计算n = p * q;
  • 计算φ(n)=(p-1)(q-1);
  • 选择与φ(n)互素的整数d;
  • 由de=1 mod φ(n)计算得到e;
  • 公钥是(e, n), 私钥是(d, n);
  • 假设明文是M(一个整数),则密文C =  mod n,此为加密过程;
  • 解密过程为M =  mod n;
rsa.h
#pragma once
#include <iostream>
#include <fstream>
#include <tommath.h>
#include <time.h>
#include <Windows.h>
#include <string>

#define SUBKEY_LENGTH 78  //  >512 bit

#define FILE_NAME_LENGTH 99

#define PLAINTEXT_LENGTH 64

#define BINARY_LENGTH 512

using namespace std;



void initial();

int Get_char_bit(char c, int pos);

void Create_prime_number(mp_int *number);

void Generate_key(char *key_name);

void Write_key_2_File(char *file_name, mp_int *key, mp_int *n);

void ADD_0(char *binary, int mode);

void rsa_decrypt(char *src, char *dst, char *key_name, int mode);

void rsa_encrypt(char *src, char *dst, char *key_name, int mode);

void mp_print(mp_int *number);

void Get_key_from_file(mp_int *key, mp_int *n, char *key_name, int mode);

void Create_number(mp_int *number, int mode);

int Miller_rabin(mp_int *number);

void Char_2_binary(char *text, char *binary, int len);

void Binary_2_char(char *binary, char *text, int binary_len);

void quick_pow(mp_int *a, mp_int *b, mp_int *c, mp_int *d);

int Get_file_length(char *filename);
rsa.cpp
#include "rsa.h"

mp_int two;
mp_int five;
mp_int zero;
mp_int one;



int Get_char_bit(char c, int pos)
{
	return ((c >> (7 - pos)) & 1);
}



// mode 0: number of n digits
// mode 1: 2 <= number <= n - 2  for miller rabin test
void Create_number(mp_int *number, int mode)
{
	int i;
	
	srand((unsigned)time(NULL));

	char temp_number[SUBKEY_LENGTH + 1];

	temp_number[0] = rand() % 9 + 1 + 48;

	if (0 == mode)
	{
		int temp;
		for (i = 1; i <= SUBKEY_LENGTH - 2; i++)
			temp_number[i] = rand() % 10 + 48;

		temp = rand() % 10;
		if (0 == temp % 2)
			temp++;

		if (5 == temp)
			temp = 7;

		temp_number[SUBKEY_LENGTH - 1] = temp + 48;

		temp_number[SUBKEY_LENGTH] = '\0';
	}
	else if (1 == mode)
	{
		int digit = rand() % (SUBKEY_LENGTH - 2) + 2;

		for (i = 1; i <= digit - 1; i++)
			temp_number[i] = rand() % 10 + 48;

		temp_number[digit] = '\0';
	}

	mp_read_radix(number, temp_number, 10);
}



int Miller_rabin(mp_int *number)
{
	int result;
	mp_int base;
	mp_init_size(&base, SUBKEY_LENGTH);
	
	Create_number(&base, 1);

	mp_prime_miller_rabin(number, &base, &result);

	mp_clear(&base);

	return result;
}



void Create_prime_number(mp_int *number)
{
	mp_int r;
	mp_init(&r);

	int time = 100;
	int result;
	int i;

	Create_number(number, 0);

	while (1)
	{
		mp_prime_is_divisible(number, &result);

		if (0 != result)
		{
			do
			{
				mp_add(number, &two, number);
				mp_mod(number, &five, &r);
			} while (MP_EQ == mp_cmp_mag(&zero, &r));

			continue;
		}

		for (i = 0; i < time; i++)
		{
			if (!Miller_rabin(number))
				break;
		}

		if (i == time)
			return;
		else
		{
			do
			{
				mp_add(number, &two, number);
				mp_mod(number, &five, &r);
			} while (MP_EQ == mp_cmp_mag(&zero, &r));
		}
	}
}



void Generate_key(char *key_name)
{
	mp_int p, q, n, f, e, d;

	mp_init_size(&p, SUBKEY_LENGTH);
	mp_init_size(&q, SUBKEY_LENGTH);
	mp_init_size(&n, SUBKEY_LENGTH * 2);
	mp_init_size(&f, SUBKEY_LENGTH * 2);
	mp_init_size(&d, SUBKEY_LENGTH * 2);
	mp_init_size(&e, SUBKEY_LENGTH * 2);

	Create_prime_number(&p);
	Sleep(1000);
	Create_prime_number(&q);

	mp_mul(&p, &q, &n);

	mp_sub(&p, &one, &p);
	mp_sub(&q, &one, &q);

	mp_mul(&p, &q, &f);

	mp_set_int(&d, 127);

	mp_int gcd;
	mp_init_size(&gcd, SUBKEY_LENGTH * 2);

	do
	{
		mp_add(&d, &two, &d);
		mp_gcd(&d, &f, &gcd);
	} while (MP_EQ != mp_cmp_mag(&gcd, &one));

	mp_invmod(&d, &f, &e);

	char PUBLIC_KEY[FILE_NAME_LENGTH] = "d:\\public_key_";
	char PRIVATE_KEY[FILE_NAME_LENGTH] = "d:\\private_key_";


	strcat(PUBLIC_KEY, key_name);
	strcat(PRIVATE_KEY, key_name);

	Write_key_2_File(PUBLIC_KEY, &d, &n);
	Write_key_2_File(PRIVATE_KEY, &e, &n);

	mp_clear_multi(&p, &q, &e, &d, &f, &n, &gcd, NULL);
}



void initial()
{
	mp_init_set_int(&two, 2);
	mp_init_set_int(&five, 5);
	mp_init_set_int(&zero, 0);
	mp_init_set_int(&one, 1);
}



void Write_key_2_File(char *file_name, mp_int *key, mp_int *n)
{
	remove(file_name);
	FILE *fp = fopen(file_name, "w+");

	if (NULL == fp)
	{
		cout << "open file error!" << endl;
		return;
	}

	char key_str[SUBKEY_LENGTH * 2 + 1];
	char n_str[SUBKEY_LENGTH * 2 + 1];

	mp_toradix(key, key_str, 10);
	mp_toradix(n, n_str, 10);

	fprintf(fp, "%s\n", n_str);
	fprintf(fp, "%s", key_str);

	fclose(fp);
}



void mp_print(mp_int *number)
{
	char str[SUBKEY_LENGTH * 2 + 1];
	mp_toradix(number, str, 10);
	cout << str << endl;
}



// mode 0: get public key
// mode 1: get private key

void Get_key_from_file(mp_int *key, mp_int *n, char *key_name, int mode)
{
	FILE *fp = NULL;

	char PUBLIC_KEY[FILE_NAME_LENGTH] = "";
	char PRIVATE_KEY[FILE_NAME_LENGTH] = "";


	if (0 == mode)
	{
		strcat(PUBLIC_KEY, key_name);
		fp = fopen(PUBLIC_KEY, "r+");
	}
	else if (1 == mode)
	{
		strcat(PRIVATE_KEY, key_name);
		fp = fopen(PRIVATE_KEY, "r+");
	}


	if (NULL == fp)
	{
		cout << "open file error!" << endl;
		return;
	}

	char key_str[SUBKEY_LENGTH * 2 + 1];
	char n_str[SUBKEY_LENGTH * 2 + 1];

	fscanf(fp, "%s\n", n_str);
	fscanf(fp, "%s", key_str);

	mp_read_radix(n, n_str, 10);
	mp_read_radix(key, key_str, 10);

	fclose(fp);
}



void Char_2_binary(char *text, char *binary, int len)
{
	int i;
	int j;
	int k = 0;
	for (i = 0; i <= len - 1; i++)
	{
		for (j = 0; j <= 7; j++)
			binary[k++] = Get_char_bit(text[i], j) + '0';
	}

	binary[k] = '\0';
}



void Binary_2_char(char *binary, char *text, int binary_len)
{
	int i;
	int j = 0;
	int k = 0;
	int sum = 0;

	for (i = 0; i <= binary_len - 1; i++)
	{
		sum = sum + pow(2, 7 - j)*(int)((binary[i]) - '0');
		j++;

		if (8 == j)
		{
			text[k++] = sum;
			sum = 0;
			j = 0;
		}
	}
}



// mode 0: encrypt char*
// mode 1: encrypt file
void rsa_encrypt(char *src, char *dst, char *key_name, int mode)
{
	mp_int n;
	mp_int public_key;
	mp_int plain_number;
	mp_int cipher_number;

	mp_init_size(&n, SUBKEY_LENGTH * 2);
	mp_init_size(&public_key, SUBKEY_LENGTH * 2);
	mp_init_size(&plain_number, SUBKEY_LENGTH * 2);
	mp_init_size(&cipher_number, SUBKEY_LENGTH * 2);

	Get_key_from_file(&public_key, &n, key_name, 0);

	char plain_text[PLAINTEXT_LENGTH + 1];
	char cipher_text[PLAINTEXT_LENGTH + 2];

	char plain_binary[BINARY_LENGTH + 1];
	char cipher_binary[BINARY_LENGTH + 9];

	int i;
	int j;
	int k;
	int l;

	if (0 == mode)
	{
		int round = ceil(strlen(src) / 64.0);
		
		for (i = 0; i < round; i++)
		{
			for (l = 0; l <= BINARY_LENGTH; l++)
				plain_binary[l] = '\0';

			for (l = 0; l <= BINARY_LENGTH + 8; l++)
				cipher_binary[l] = '\0';

			for (l = 0; l <= PLAINTEXT_LENGTH; l++)
				plain_text[l] = '\0';

			for (l = 0; l <= PLAINTEXT_LENGTH + 1; l++)
				cipher_text[l] = '\0';

			for (j = 0; j <= PLAINTEXT_LENGTH - 1; j++)
			{
				plain_text[j] = src[i * PLAINTEXT_LENGTH + j];

				if ('\0' == plain_text[j])
				{
					for (k = j + 1; k <= PLAINTEXT_LENGTH - 1; k++)
						plain_text[k] = '\0';
					break;
				}
			}

			plain_text[PLAINTEXT_LENGTH] = '\0';

			Char_2_binary(plain_text, plain_binary, PLAINTEXT_LENGTH);

			mp_zero(&plain_number);
			mp_read_radix(&plain_number, plain_binary, 2);

			mp_zero(&cipher_number);

			// ----------------------------------------------------------------
			mp_exptmod(&plain_number, &public_key, &n, &cipher_number);

			//quick_pow(&plain_number, &public_key, &n, &cipher_number);
			// ----------------------------------------------------------------

			mp_toradix(&cipher_number, cipher_binary, 2);

			ADD_0(cipher_binary, 0);

			Binary_2_char(cipher_binary, cipher_text, BINARY_LENGTH + 8);

			for (l = 0; l <= PLAINTEXT_LENGTH; l++)
				dst[i*(PLAINTEXT_LENGTH + 1) + l] = cipher_text[l];
		}
		
		dst[round * (PLAINTEXT_LENGTH + 1)] = '\0';

		mp_clear_multi(&n, &public_key, &plain_number, &cipher_number, NULL);

	}
	else if (1 == mode)
	{
		ifstream fin;
		ofstream fout;
			
		fin.open(src, ios::binary);
		fout.open(dst, ios::binary);

		char ch;

		int i = 0;
		int len = Get_file_length(src) % 64;
		fout << len << endl;

		while (1)
		{
			fin.get(ch);
			plain_text[i++] = ch;

			if (i == PLAINTEXT_LENGTH || fin.eof())
			{
				if (i == PLAINTEXT_LENGTH)
					plain_text[i] = '\0';
				else
				{
					for (k = i - 1; k <= PLAINTEXT_LENGTH; k++)
						plain_text[k] = '\0';
				}
				
				for (l = 0; l <= BINARY_LENGTH; l++)
					plain_binary[l] = '\0';

				for (l = 0; l <= BINARY_LENGTH + 8; l++)
					cipher_binary[l] = '\0';

				for (l = 0; l <= PLAINTEXT_LENGTH + 1; l++)
					cipher_text[l] = '\0';

				Char_2_binary(plain_text, plain_binary, PLAINTEXT_LENGTH);

				mp_zero(&plain_number);
				mp_read_radix(&plain_number, plain_binary, 2);

				mp_zero(&cipher_number);

				// ----------------------------------------------------------------
				mp_exptmod(&plain_number, &public_key, &n, &cipher_number);

				//quick_pow(&plain_number, &public_key, &n, &cipher_number);
				// ----------------------------------------------------------------

				mp_toradix(&cipher_number, cipher_binary, 2);

				ADD_0(cipher_binary, 0);

				Binary_2_char(cipher_binary, cipher_text, BINARY_LENGTH + 8);

				for (l = 0; l <= PLAINTEXT_LENGTH; l++)
					fout << cipher_text[l];

				for (l = 0; l <= PLAINTEXT_LENGTH; l++)
					plain_text[l] = '\0';

				i = 0;
			}

			if (fin.eof())
				break;
		}

		fin.close();
		fout.close();

		mp_clear_multi(&n, &public_key, &plain_number, &cipher_number, NULL);
	}
}



// mode 0: for encrypt
// mode 1: for decrypt
void ADD_0(char *binary, int mode)
{
	int i;
	int difference;

	if (0 == mode)
		difference = BINARY_LENGTH + 8 - strlen(binary);
	else if(1 == mode)
		difference = BINARY_LENGTH - strlen(binary);;
	
	for (i = strlen(binary); i >= 0; i--)
		binary[i + difference] = binary[i];
	for (i = 0; i < difference; i++)
		binary[i] = '0';
}



// mode 0: decrypt char*
// mode 1: decrypt file
void rsa_decrypt(char *src, char *dst, char *key_name, int mode)
{
	mp_int n;
	mp_int private_key;
	mp_int plain_number;
	mp_int cipher_number;

	mp_init_size(&n, SUBKEY_LENGTH * 2);
	mp_init_size(&private_key, SUBKEY_LENGTH * 2);
	mp_init_size(&plain_number, SUBKEY_LENGTH * 2);
	mp_init_size(&cipher_number, SUBKEY_LENGTH * 2);

	Get_key_from_file(&private_key, &n, key_name, 1);

	char plain_text[PLAINTEXT_LENGTH + 1];
	char cipher_text[PLAINTEXT_LENGTH + 2];

	char plain_binary[BINARY_LENGTH + 1];
	char cipher_binary[BINARY_LENGTH + 9];

	int j;
	int k;
	int l;

	if (0 == mode)
	{
		k = -1;
		do
		{
			k++;

			for (l = 0; l <= PLAINTEXT_LENGTH; l++)
				plain_text[l] = '\0';

			for (l = 0; l <= BINARY_LENGTH; l++)
				plain_binary[l] = '\0';

			for (l = 0; l <= BINARY_LENGTH + 8; l++)
				cipher_binary[l] = '\0';

			for (j = 0; j <= PLAINTEXT_LENGTH; j++)
				cipher_text[j] = src[k * (PLAINTEXT_LENGTH + 1) + j];

			cipher_text[PLAINTEXT_LENGTH + 1] = '\0';

			Char_2_binary(cipher_text, cipher_binary, PLAINTEXT_LENGTH + 1);

			mp_zero(&cipher_number);

			mp_read_radix(&cipher_number, cipher_binary, 2);

			mp_zero(&plain_number);

			// ----------------------------------------------------------------
			mp_exptmod(&cipher_number, &private_key, &n, &plain_number);

			//quick_pow(&cipher_number, &private_key, &n, &plain_number);
			// ----------------------------------------------------------------

			mp_toradix(&plain_number, plain_binary, 2);

			ADD_0(plain_binary, 1);

			Binary_2_char(plain_binary, plain_text, BINARY_LENGTH);

			for (l = 0; l <= PLAINTEXT_LENGTH - 1; l++) 
				dst[k*PLAINTEXT_LENGTH + l] = plain_text[l];
		
		} while ('\0' != src[(k + 1)*(PLAINTEXT_LENGTH + 1)]);

		dst[(k + 1) * PLAINTEXT_LENGTH] = '\0';
	}
	else if (1 == mode)
	{
		ifstream fin;
		ofstream fout;

		fin.open(src, ios::binary);
		fout.open(dst, ios::binary);

		int i = 0;

		int limit = PLAINTEXT_LENGTH;

		char ch;

		string temp = "";
		while (1)
		{
			fin.get(ch);
			if ('\n' == ch)
				break;
			temp += ch;
		}

		int len = atoi(temp.c_str());

		while (1)
		{
			fin.get(ch);
			cipher_text[i++] = ch;

			if (i == PLAINTEXT_LENGTH + 1)
			{
				if (i == PLAINTEXT_LENGTH + 1) 
					cipher_text[i] = '\0';
				else
					cipher_text[i - 1] = '\0';

				for (l = 0; l <= BINARY_LENGTH + 8; l++)
					cipher_binary[l] = '\0';

				for (l = 0; l <= PLAINTEXT_LENGTH; l++)
					plain_text[l] = '\0';

				for (l = 0; l <= BINARY_LENGTH; l++)
					plain_binary[l] = '\0';

				Char_2_binary(cipher_text, cipher_binary, PLAINTEXT_LENGTH + 1);

				mp_zero(&cipher_number);
				mp_read_radix(&cipher_number, cipher_binary, 2);

				mp_zero(&plain_number);

				// ----------------------------------------------------------------
				mp_exptmod(&cipher_number, &private_key, &n, &plain_number);

				//quick_pow(&cipher_number, &private_key, &n, &plain_number);
				// ----------------------------------------------------------------

				mp_toradix(&plain_number, plain_binary, 2);

				ADD_0(plain_binary, 1);

				Binary_2_char(plain_binary, plain_text, BINARY_LENGTH);
				
				if (fin.peek() == EOF)
					limit = len;

				for (l = 0; l < limit; l++)
					fout << plain_text[l];

				for (l = 0; l <= PLAINTEXT_LENGTH + 1; l++)
					cipher_text[l] = '\0';

				i = 0;
			}
			
			if (fin.eof())
				break;
		}

		fin.close();
		fout.close();
	}

	mp_clear_multi(&n, &private_key, &plain_number, &cipher_number, NULL);
}



// compute d = a ^ b (mod c)
void quick_pow(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
{
	mp_int temp;
	mp_int temp_a;
	mp_int temp_b;

	mp_init_size(&temp, SUBKEY_LENGTH * 2);
	mp_init_size(&temp_a, SUBKEY_LENGTH * 2);
	mp_init_size(&temp_b, SUBKEY_LENGTH * 2);

	mp_copy(a, &temp_a);
	mp_copy(b, &temp_b);

	mp_set_int(d, 1);

	mp_mod(&temp_a, c, &temp_a);

	while (MP_GT == mp_cmp(&temp_b, &zero))
	{
		mp_mod(&temp_b, &two, &temp);
		if (MP_EQ == mp_cmp(&one, &temp))
		{
			mp_mul(d, &temp_a, d);
			mp_mod(d, c, d);
		}

		mp_div_2(&temp_b, &temp_b);

		mp_mul(&temp_a, &temp_a, &temp_a);
		mp_mod(&temp_a, c, &temp_a);
	}

	mp_clear_multi(&temp, &temp_a, &temp_b, NULL);
}

int Get_file_length(char *filename) 
{
	FILE *fp = fopen(filename, "rb");
	if (NULL == fp)
		return -1;

	fseek(fp, 0, SEEK_END);

	int temp = ftell(fp);

	fclose(fp);
	return temp;
}
main.h
#pragma once
#include "rsa.h"
 main.cpp
#include "main.h"


// example
void main()
{
	initial();

	// generate key belong to arg " "
	Generate_key("hello_world");
	

	// ----------------------example: encrypt char* ---------------------------
	char a[200] = "to_be_or_not_to_be_it_is_a_question";
	char b[400];
	char c[200];

	//rsa_encrypt(a, b, "hello_world", 0);
	//rsa_decrypt(b, c, "hello_world", 0);
	
	cout << "c: " << c << endl;
	// ------------------------------------------------------------------------


	// ----------------------example: encrypt file ----------------------------

	rsa_encrypt("d:\\a.gif", "d:\\m", "d:\\public_key_hello_world", 1);

    rsa_decrypt("d:\\m", "D:\\b.gif", "d:\\private_key_hello_world", 1);

	// ------------------------------------------------------------------------

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1341398.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

小梅哥Xilinx FPGA学习笔记16——FSM(状态机)的学习

目录 一、 状态机导读 1.1 理论学习 1.2 状态机的表示 1.3 状态机编码 1.4 状态机描述方式 二 、实战演练一&#xff08;来自野火&#xff09; 2.1 实验目标 2.2 模块框图 2.3 状态转移图绘制 2.4 设计文件 2.5 仿真测试文件 2.6 仿真结果 三、 实战演练二&…

时序预测 | Matlab实现SSA-CNN-GRU麻雀算法优化卷积门控循环单元时间序列预测

时序预测 | Matlab实现SSA-CNN-GRU麻雀算法优化卷积门控循环单元时间序列预测 目录 时序预测 | Matlab实现SSA-CNN-GRU麻雀算法优化卷积门控循环单元时间序列预测预测效果基本介绍程序设计参考资料 预测效果 基本介绍 Matlab实现SSA-CNN-GRU麻雀算法优化卷积门控循环单元时间序…

分布式事务之最终一致性

分布式事务之最终一致性 参考链接分布式事务基础理论概述案例解决方案:RocketMQ可靠消息注意事项&#xff1a;代码实现 参考链接 原文链接&#xff1a;https://blog.csdn.net/jikeyeka/article/details/126296938 分布式事务基础理论 基于上述的CAP和BASE理论,一般情况下会保…

Grafana Loki 组件介绍

Loki 日志系统由以下3个部分组成&#xff1a; Loki是主服务器&#xff0c;负责存储日志和处理查询。Promtail是专为loki定制的代理&#xff0c;负责收集日志并将其发送给 loki 。Grafana用于 UI展示。 Distributor Distributor 是客户端连接的组件&#xff0c;用于收集日志…

小米SU7汽车发布会; 齐碳科技C+轮融资;网易 1 月 3 日发布子曰教育大模型;百度文心一言用户数已突破 1 亿

投融资 • 3200 家 VC 投资的创业公司破产&#xff0c;那个投 PLG 的 VC 宣布暂停投资了• 云天励飞参与 AI 技术与解决方案提供商智慧互通 Pre-IPO 轮融资• 百度投资 AIGC 公司必优科技• MicroLED量测公司点莘技术获数千万级融资• 智慧互通获AI上市公司云天励飞Pre-IPO轮战…

10. Opencv检测并截取图中二维码

1. 说明 在二维码扫描功能开发中,使用相机扫描图片时,往往图片中的信息比较多样,可能会造成二维码检测失败的问题。一种提高检测精度的方式就是把二维码在图片中单独抠出来,去除其它冗余信息,然后再去识别这张提取出来的二维码。本篇博客记录采用的一种实现二维码位置检测…

OSPF被动接口配置-新版(14)

目录 整体拓扑 操作步骤 1.基本配置 1.1 配置R1的IP 1.2 配置R2的IP 1.4 配置R4的IP 1.5 配置R5的IP 1.6 配置PC-1的IP地址 1.7 配置PC-2的IP地址 1.8 配置PC-3的IP地址 1.9 配置PC-4的IP地址 1.10 检测R1与PC3连通性 1.11 检测R2与PC4连通性 1.12 检测R4与PC1连…

docker小白第九天

docker小白第九天 安装redis集群 cluster(集群)模式-docker版本&#xff0c;哈希槽分区进行亿级数据存储。如果1~2亿条数据需要缓存&#xff0c;请问如何设计这个存储案例。单机存储是不可能的&#xff0c;需要分布式存储&#xff0c;如果使用redis又该如何部署。 哈希取余分…

第十二章 Sleuth分布式请求链路跟踪

Sleuth分布式请求链路跟踪 gitee:springcloud_study: springcloud&#xff1a;服务集群、注册中心、配置中心&#xff08;热更新&#xff09;、服务网关&#xff08;校验、路由、负载均衡&#xff09;、分布式缓存、分布式搜索、消息队列&#xff08;异步通信&#xff09;、数…

nginx报错upstream sent invalid header

nginx报错upstream sent invalid header 1.报错背景 最近由于nginx 1.20的某个漏洞需要升级到nginx1.25的版本。在测试环境升级完nginx后&#xff0c;发现应用直接报错502 bad gateway了。 然后查看nginx的errlog&#xff0c;发现&#xff1a; upstream sent invalid head…

语言模型:从n-gram到神经网络的演进

目录 1 前言2 语言模型的两个任务2.1 自然语言理解2.2 自然语言生成 3 n-gram模型4 神经网络语言模型5 结语 1 前言 语言模型是自然语言处理领域中的关键技术之一&#xff0c;它致力于理解和生成人类语言。从最初的n-gram模型到如今基于神经网络的深度学习模型&#xff0c;语言…

LMX2571 芯片配置Verliog SPI驱动

前言 本实验使用ZYNQ的PL(FPGA)对LMX2571芯片进行配置&#xff0c;以下连接为相关的原理和软件使用资料。 TICS Pro 配置时钟芯片 文献阅读–Σ-Δ 小数频率合成器原理 LMX2571芯片数据手册 一、LMX2571配置时序分析 1.1 写时序 LMX2571使用24位寄存器进行编程。一个24位移位…

Codeforces Round 918 (Div. 4)(AK)

A、模拟 B、模拟 C、模拟 D、模拟 E、思维&#xff0c;前缀和 F、思维、逆序对 G、最短路 A - Odd One Out 题意&#xff1a;给定三个数字&#xff0c;有两个相同&#xff0c;输出那个不同的数字。 直接傻瓜写法 void solve() {int a , b , c;cin >> a >>…

机器学习 -- 数据预处理

系列文章目录 未完待续…… 目录 系列文章目录 前言 一、数值分析简介 二、内容 前言 tips&#xff1a;这里只是总结&#xff0c;不是教程哈。 以下内容仅为暂定&#xff0c;因为我还没找到一个好的&#xff0c;让小白&#xff08;我自己&#xff09;也能容易理解&#x…

Java线上问题排查思路

1、Java 服务常见问题 Java 服务的线上问题从系统表象来看大致可分成两大类: 系统环境异常、业务服务异常。 系统环境异常&#xff1a;主要从CPU、内存、磁盘、网络四个方面考虑。比如&#xff1a;CPU 占用率过高、CPU 上下文切换频率次数较高、系统可用内存长期处于较低值、…

工业产线看板的智能化应用

在数字化浪潮兴起之前&#xff0c;许多制造企业主要依赖手工生产和传统的生产管理方法&#xff0c;生产数据的收集和分析主要依赖于人工&#xff0c;导致信息传递滞后、生产过程不透明&#xff0c;难以及时调整生产计划。在传统的生产环境中&#xff0c;生产过程的各个环节缺乏…

留言板(Mybatis连接数据库版)

目录 1.添加Mybatis和SQL的依赖 2.建立数据库和需要的表 3.对应表中的字段&#xff0c;补充Java对象 4.对代码进行逻辑分层 5.后端逻辑代码 之前的项目实例【基于Spring MVC的前后端交互案例及应用分层的实现】https://blog.csdn.net/weixin_67793092/article/details/134…

K8S结合Prometheus构建监控系统

一、Prometheus简介 Prometheus 是一个开源的系统监控和警报工具&#xff0c;用于收集、存储和查询时间序列数据。它专注于监控应用程序和基础设施的性能和状态&#xff0c;并提供丰富的查询语言和灵活的告警机制1、Prometheus基本介绍 数据模型&#xff1a;Prometheus 使用时…

Spring Boot笔记1

1. SpringBoot简介 1.1. 原有Spring优缺点分析 1.1.1. Spring的优点分析 Spring是Java企业版&#xff08;Java Enterprise Edition&#xff0c;javeEE&#xff09;的轻量级代替品。无需开发重量级的Enterprise JavaBean&#xff08;EJB&#xff09;&#xff0c;Spring为企业…

20231227在Firefly的AIO-3399J开发板的Android11的挖掘机的DTS配置单后摄像头ov13850

20231227在Firefly的AIO-3399J开发板的Android11的挖掘机的DTS配置单后摄像头ov13850 2023/12/27 18:40 1、简略步骤&#xff1a; rootrootrootroot-X99-Turbo:~/3TB$ cat Android11.0.tar.bz2.a* > Android11.0.tar.bz2 rootrootrootroot-X99-Turbo:~/3TB$ tar jxvf Androi…