C++程序 可以执行RSA 加密解密算法 可以给我吗

C++程序 可以执行RSA 加密解密算法 可以给我吗,第1张

#include<cstdio>

#include<iostream>

#include<cstdlib>

#include<cstring>

#include<cmath>

#include<algorithm>

#include<ctime>

#define gcc 10007

#define MAX ((INT)1<<63)-1

using namespace std;

typedef unsigned long long INT;

INT p[10]={2,3,5,7,11,13,17,19,23,29};

inline INT gcd(INT a,INT b)

{

INT m=1;

if(!b) return a;

while(m)

{

m=a%b;

a=b;

b=m;

}

return a;

}

//计算ab%n

inline INT multi_mod(INT a,INT b,INT mod)

{

INT sum=0;

while(b)

{

if(b&1) sum=(sum+a)%mod;

a=(a+a)%mod;

b>>=1;

}

return sum;

}

//计算a^b%n;

inline INT quickmod(INT a,INT b,INT mod)

{

INT sum=1;

while(b)

{

if(b&1) sum=multi_mod(sum,a,mod);

a=multi_mod(a,a,mod);

b>>=1;

}

return sum;

}

bool miller_rabin(INT n)

{

INT i,j,k=0;

INT u,m,buf;

//将n分解为m2^k

if(n==2)

return true;

if(n<2||!(n&1))

return false;

m=n-1;

while(!(m&1))

k++,m>>=1;

for(i=0;i<9;i++)

{

if(p[i]>=n)

return true;

u=quickmod(p[i],m,n);

if(u==1)

continue;

for(j=0;j<k;j++)

{

buf=multi_mod(u,u,n);

if(buf==1&&u!=1&&u!=n-1)

return false;

u=buf;

}

//如果p[i]^(n-1)%n!=1那么n为合数

if(u-1)

return false;

}

return true;

}

INT extended_euclidean(INT n, INT m, INT &x, INT &y) {

if (m == 0) {

x = 1; y = 0; return n;

}

INT g = extended_euclidean(m, n % m, x, y);

INT t = x - n / m y;

x = y;

y = t;

return g;

}

INT invmod(INT a,INT n)//求a对n的乘法逆元

{

INT x,y;

if(extended_euclidean(a,n,x,y)!=1) return -1;

return (x%n+n)%n;

}

void fff(char str,INT x){

INT t=0;

INT i,j=0;

for(i=0;i<strlen(str);i++){

t=t10+str[i]-'0';

if(i%2==1){

x[j]=t;

j++;

t=0;

}

}

if(i%2) x[j]=t;

}

void rsa(INT m,INT e,INT n){

for(INT i=0;i<100;i++)

m[i]=quickmod(m[i],e,n);

}

INT ranprim(){

INT p=0;

while(1){

p=(rand()%1000)+1000;

if(miller_rabin(p))

return p;

}

}

int main(){

srand((unsigned)time(NULL));

INT p,q,e,n,d,x;

do{

p=ranprim();

q=ranprim();

e=ranprim();

n=pq;

d=invmod(e,(p-1)(q-1));

x=(ed)%((p-1)(q-1));

}while(x!=1);

cout<<"p:"<<p<<" q:"<<q<<" e:"<<e<<" d:"<<d<<" n:"<<n<<endl;

char str[100];

int t;

INT m[100];

memset(m,0,sizeof(m));

while(scanf("%s",str)!=EOF){

t=(strlen(str)+1)/2;

fff(str,m);

rsa(m,e,n);

for(int i=0;i<t;i++)

printf("%d ",m[i]);

printf("\n");

rsa(m,d,n);

for(int i=0;i<t;i++)

printf("%d ",m[i]);

printf("\n");

}

}

我昨天刚给老师查的,没问题。输入数字,对数字加密。加密过程是两位一组。

看看这个吧

>

//引入文件

import javasecurity;

import javaxcrypto;

/

RSACryptography

RSACryptography use the privated key to encrypt the plain text and decrypt

the cipher text with the public key

/

public class RSACryptography {

Cipher cipher;

/

构造函数,就是你每次new这个对象RSACryptography 时候就会执行里面的方法

返回一个Cipher对象(其实他就是用来加密解密的)

/

public RSACryptography() {

try {

cipher = CiphergetInstance("RSA");//返回一个cipher对象,该类

//应该是单例的

} catch (NoSuchAlgorithmException e) {//抛出异常,没什么说的

eprintStackTrace();

} catch (NoSuchPaddingException e) {

eprintStackTrace();

}

}

/

好了,重点来了,你需要加密解密的就调用这个方法encrypt_decrypt(),传入一个byte[]的类型值byteInput,,就是你要加密的东西,在传入一个key,这个key 就像钥匙一样,你根据这个key进行加密,也可以根据这个key进行解密的,boolean 类型的 crypto,如果true就是加密,false就是解密

/

public byte[] encrypt_decrypt(byte[] byteInput, Key key, boolean crypto) {

try {

if(crypto){

cipherinit(CipherENCRYPT_MODE,key);//加密前初始化

}else{

cipherinit(CipherDECRYPT_MODE,key);//解密前初始化

}

byte[] cipherByte = cipherdoFinal(byteInput);//进行加密或解密

return cipherByte;//返回你的加密或者解密值类型为byte[]

} catch (InvalidKeyException e) {//抛出异常

eprintStackTrace();

} catch (IllegalBlockSizeException e) {

eprintStackTrace();

} catch (BadPaddingException e) {

eprintStackTrace();

}

return null;

}

}

npm install  wxapp_rsa

var RSA = require('/wxapp_rsajs')

// RSA加签

    var sign_rsa = new RSARSAKey();

//privateKey_pkcs1需要是-----BEGIN PRIVATE KEY-----开头的私钥

    sign_rsa = RSAKEYUTILgetKey(privateKey_pkcs1);

    consolelog('签名RSA:')

    consolelog(sign_rsa)

    var hashAlg = 'MD5withRSA';

    var hSig = sign_rsasignString("12345678901234567890", hashAlg);

    hSig = RSAhex2b64(hSig); // hex 转 b64

    consolelog("签名结果:" + hSig)

    // RSA 验签

    var verify_rsa = new RSARSAKey();

    verify_rsa = RSAKEYUTILgetKey(publicKey_pkcs1);

    consolelog('验签RSA:')

    consolelog(verify_rsa)

    hSig = RSAb64tohex(hSig)

    var ver = verify_rsaverifyString("12345678901234567890", hSig)

    consolelog('验签结果:' + ver)

//  RSA加密 加密字段长度不大于117

    var encrypt_rsa = new RSARSAKey();

    encrypt_rsa = RSAKEYUTILgetKey(rsa_public_key);

    consolelog('加密RSA:')

    consolelog(encrypt_rsa)

    var encStr = encrypt_rsaencrypt('1234567890')

    consolelog(encStr)

    encStr = RSAhex2b64(encStr);

    consolelog("加密结果:" + encStr)

    // RSA 解密

    var decrypt_rsa = new RSARSAKey();

    decrypt_rsa = RSAKEYUTILgetKey(rsa_public_key_private);

    consolelog('解密RSA:')

    consolelog(decrypt_rsa)

    encStr = RSAb64tohex(encStr)

    var decStr = decrypt_rsadecrypt(encStr)

    consolelog("解密结果:" + decStr)

这种著名加密算法,都有公开现成的代码,哪用得着自己来写?

问题是给了具体的公钥如何改写源代码啊

不好意思,我看错了。这个给出的是公钥,要求解私钥。这相当于要破解RSA加密。但由于公钥的数字极小,所以破解很简单,不用计算机用笔算都可以很快完成。77=11×7,知道了这个,照着算法来,其它都不是问题了。

以上就是关于C++程序 可以执行RSA 加密解密算法 可以给我吗全部的内容,包括:C++程序 可以执行RSA 加密解密算法 可以给我吗、哪位大神能写一个RSA算法的文件加密解密的C程序给我,可以对文件进行加密和解密,在线等,急!!!!、有一段用java实现rsa加解密的程序看不懂,希望高手帮我做下注释,详细些,谢谢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9618261.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存