rsa算法实现超级大数(超过unsigned long long)的加解密和数字签名(c++实现)

本文介绍了使用C++实现RSA算法,包括对超过unsigned long long范围的大数进行加解密以及数字签名的过程。作者分享了自己的研究成果,提供了加密和解密程序的实现。
摘要由CSDN通过智能技术生成

经过我的不屑努力,和网上某些大神的帮助,我终于编写成功了rsa算法的加解密程序,现在就无偿贡献给大家

加密程序:

#include <algorithm>  

#include <bitset>

#include <cctype>

#include <cerrno>

#include <clocale>

#include <cmath>

#include <complex>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <ctime>

#include <deque> 

#include <exception> 

#include <fstream>

#include <functional>

#include <limits>

#include <list>

#include <map> 

#include <iomanip>

#include <ios>

#include<iosfwd>

#include <iostream>

#include <istream> 

#include <ostream> 

#include <queue> 

#include <set> 

#include <sstream>

#include <stack>

#include <stdexcept>

#include <streambuf>

#include <string>

#include <utility>

#include <vector>

#include <cwchar>

#include <cwctype>

#include <stdbool.h>

#include <stdint.h>

#include<assert.h>   

#include <ctype.h>     

#include <errno.h>     

#include <float.h>

#include <limits.h>

#include<locale.h>

#include <math.h>

#include <stdio.h>

#include<stdlib.h>

#include <string.h>

#include<time.h>

using namespace std; 

string n, e,  p, j, b, a[100010],n2,d,lo;

unsigned long long i,c;

inline int compare(string str1, string str2) {//相等返回0,大于返回1,小于返回-1

    if (str1.size() > str2.size()) return 1; //长度长的整数大于长度小的整数

    else if (str1.size() < str2.size()) return -1;

    else                              return str1.compare(str2); //若长度相等,则头到尾按位比较

}

string SUB_INT(string str1, string str2);

string ADD_INT(string str1, string str2) {//高精度加法

    int sign = 1; //sign 为符号位

    string str;

    if (str1[0] == '-') {

        if (str2[0] == '-') {

            sign = -1;

            str = ADD_INT(str1.erase(0, 1), str2.erase(0, 1));

        }

        else {

            str = SUB_INT(str2, str1.erase(0, 1));

        }

    }

    else {

        if (str2[0] == '-') {

            str = SUB_INT(str1, str2.erase(0, 1));

        }

        else { //把两个整数对齐,短整数前面加0补齐

            string::size_type L1, L2;

            int i;

            L1 = str1.size();

            L2 = str2.size();

            if (L1 < L2) {

                for (i = 1; i <= L2 - L1; i++) str1 = "0" + str1;

            }

            else {

                for (i = 1; i <= L1 - L2; i++) str2 = "0" + str2;

            }

            int int1 = 0, int2 = 0; //int2 记录进位

            for (i = str1.size() - 1; i >= 0; i--) {

                int1 = (int(str1[i]) - '0' + int(str2[i]) - '0' + int2) % 10;

                int2 = (int(str1[i]) - '0' + int(str2[i]) - '0' + int2) / 10;

                str = char(int1 + '0') + str;

            }

            if (int2 != 0) str = char(int2 + '0') + str;

        }

    }

    //运算后处理符号位

    if ((sign == -1) && (str[0] != '0')) str = "-" + str;

    return str;

}

string SUB_INT(string str1, string str2) {//高精度减法

    int sign = 1; //sign 为符号位

    string str;

    int i, j;

    if (str2[0] == '-') {

        str = ADD_INT(str1, str2.erase(0, 1));

    }

    else {

        int res = compare(str1, str2);

        if (res == 0) return "0";

        if (res < 0) {

            sign = -1;

            string temp = str1;

            str1 = str2;

            str2 = temp;

        }

        string::size_type tempint;

        tempint = str1.size() - str2.size();

        for (i = str2.size() - 1; i >= 0; i--) {

            if (str1[i + tempint] < str2[i]) {

                j = 1;

                while (1) {//zhao4zhong1添加

                    if (str1[i + tempint - j] == '0') {

                        str1[i + tempint - j] = '9';

                        j++;

                    }

                    else {

                        str1[i + tempint - j] = char(int(str1[i + tempint - j]) - 1);

                        break;

                    }

         
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值