Current location - Quotes Website - Personality signature - Seek the correct RSA encryption and decryption algorithm in C language, thank you.
Seek the correct RSA encryption and decryption algorithm in C language, thank you.
//rsa.h

#include? < stdio.h>

#define? MAX_NUM? 631

#define? MAX_PRIME? 251

//! ? Return code

#define? OK? 1

#define? ERROR_NOEACHPRIME? 11

#define? ERROR_NOPUBLICKEY? 12

#define? ERROR_GENERROR? 13

unsigned? int? MakePrivatedKeyd(? unsigned? int? uiP,? unsigned? int? uiQ? );

unsigned? int? GetPrivateKeyd(? unsigned? int? iWhich? );

unsigned? int? MakePairkey(? unsigned? int? uiP,? unsigned? int? uiQ,? unsigned? int? uiD? );

unsigned? int? GetPairKey(? unsigned? int? & d,? unsigned? int? & e? );

void? rsa_encrypt(? int? n,? int? e,? char? *mw,? int? iLength,? int? *& cw? );

void? rsa_decrypt(? int? n,? int? d,? int? *& cw,? int? cLength,? char? *mw? );

void? outputkey();

//rsa.c

#include? "rsa.h"

//! Save the private key d set

struct? pKeyset

{

unsigned? int? set[? MAX_NUM? ];

unsigned? int? size;

}pset;

//! ? Save the public and private key pair

struct? pPairkey

{

unsigned? int? d;

unsigned? int? e;

unsigned? int? n;

}pairkey;

//? Name: isPrime

//? Function: judge whether two numbers are coprime

//? Parameter: m:? Number a; ? n:? Number b

//? Return: m, n coprime returns true; ? Otherwise return false

bool? isPrime(? unsigned? int? m,? unsigned? int? n? )

{

unsigned? int? i=;

bool? Flag? =? true;

if(? m< 2? ||? n< 2? )

return? false;

unsigned? int? tem? =? (? m? > ? n? )n? :? m;

for(? i=2; ? i< =tem? & & ? Flag; ? i++? )

{

bool? mFlag? =? true;

bool? nFlag? =? true;

if(? m? %? i? ==? ? )

mFlag? =? false;

if(? n? %? i? ==? ? )

nFlag? =? false;

if(? ! mFlag? & & ? ! nFlag? )

Flag? =? false;

}

if(? Flag? )

return? true;

else

return? false;

}

//? Name: makeprivatedked

//? Function: generate private key d

// from prime numbers q and q? Parameters: uiP:? Prime number p; ? uiQ:? Prime number Q

//? Return: private key d

unsigned? int? MakePrivatedKeyd(? unsigned? int? uiP,? unsigned? int? uiQ? )

{

unsigned? int? i=;

//! ? Get all the numbers that are coprime with z (? A collection of private keys d? )

unsigned? int? z? =? (? uiP? -1? )? *? (? uiQ? -1? );

pset.size? =? ;

for(? i=; ? i< z; ? i++? )

{

if(? isPrime(? i,? z? )? )

{

pset.set[? pset.size++? ]? =? i;

}

}

return? pset.size;

}

//? Name: makepaykey

//? Function: generate RSA public and private key pair

//? Parameters: uiP:? Prime number p; ? uiQ:? Prime number q; ? uiD:? Private key d

//? Return: error code

unsigned? int? MakePairkey(? unsigned? int? uiP,? unsigned? int? uiQ,? unsigned? int? uiD? )

{

bool? bFlag? =? true;

unsigned? int? i? =? ,? e;

unsigned? int? z? =? (? uiP-1? )? *? (? uiQ-1? );

unsigned? int? d? =? pset.set[uiD];

//d=uiD;

if(? ! isPrime(? z,? d? )? )

return? ERROR_NOEACHPRIME;

for(? i=2; ? i< z; ? i++? )

{

if(? (i*d)%z? ==? 1? )

{

e? =? i;

bFlag? =? false;

}

}

if(? bFlag? )

return? ERROR_NOPUBLICKEY;

if(? (d*e)%z? ! =? 1? )

ERROR_GENERROR;

pairkey.d? =? d;

pairkey.e? =? e;

pairkey.n? =? uiP? *? uiQ;

return? OK;

}

//? Name: GetPairKey

//? Function: provide external interface to obtain public and private key pair

//? Parameters: uiP:? Prime number p; ? uiQ:? Prime number q; ? uiD:? Private key d

//? Return:

unsigned? int? GetPairKey(? unsigned? int? & d,? unsigned? int? & e? )

{

d? =? pairkey.d;

e? =? pairkey.e;

return? pairkey.n;

}

//? Name: getprivatekeyed

//? Function: provide external interface, and the user can select the ID to get the private key d

//? Parameter: iWhich:? User selects the id of private key d

//? Returns: private key d value

unsigned? int? GetPrivateKeyd(? unsigned? int? iWhich? )

{

if(? pset.size? > =? iWhich? )

return? pset.set[? iWhich? ];

else

return? ;

}

//? Name: rsa_encrypt

//? Function: RSA encryption operation

//? Parameter: n:? Public key n; ? e:? Public key e; ? mw:? Encrypted plaintext; ? iLength:? Plaintext length; ? cw:? Ciphertext output

//? Return: no

void? rsa_encrypt(? int? n,? int? e,? char? *mw,? int? mLength,? int? *& cw? )

{

int? i=,? j=;

__int64? temInt? =? ;

for(? i=; ? i< mLength; ? i++? )

{

temInt? =? mw[i];

if(? e! =? )

{

for(? j=1; ? j< e; ? j++? )

{

temInt? =? (? temInt? *? mw[i]? )? %? n;

}

}

else

{

temInt? =? 1;

}

cw[i]? =? (int)temInt;

}

}

//? Name: rsa_decrypt

//? Function: RSA decryption operation

//? Parameter: n:? Private key n; ? d:? Private key d; ? cw:? Ciphertext; ? cLength:? Ciphertext length; ? mw:? Clear text output

//? Return: no

void? rsa_decrypt(? int? n,? int? d,? int? *& cw,? int? cLength,? char? *mw? )

{

int? i=,? j=-1;

__int64? temInt? =? ;

for(? i=; ? i< cLength/4; ? ++i? )

{

mw[i]? =? ;

temInt? =? cw[i];

if(? d? ! =? ? )

{

for(? j=1; ? j< d; ? j++? )

{

temInt? =? (__int64)(? temInt? *? cw[i]? )? %? n;

}

}

else

{

temInt? =? 1;

}

mw[i]? =? (char)temInt;

}

}

void? outputkey()

{

printf("PublicKey(e,n):? (%d,%d)\n",pairkey.e,pairkey.n);

printf("PrivateKey(d,n):? (%d,%d)\n",pairkey.d,pairkey.n);

}

//main.c

//? Engineering: RSA

//? Function: RSA encrypts and decrypts files

//? Author: jlcss|ExpNIS

#include? < stdio.h>

#include? < afxwin.h>

#include? < math.h>

#include? "rsa.h"

#define? DECRYPT_FILE? "RSA encrypted ciphertext. txt"

#define? ENCRYPT_FILE? "RSA decrypts plaintext. txt"

//! ? Maximum 2m of constraint file

# define? MAX_FILE? 124*124*2

//? Name: usage

//? Function: help information

/? Parameters: application name

//? Return: prompt message

void? Usage(? const? char? *appname? )

{

printf(? "\n\tusage:rsa? -k? Prime p? Prime number Q\n "? );

printf(? "\tusage:? rsa? -e? Clear text file? Public key e? Public key n\n "? );

printf(? "\tusage:? rsa? -d? Ciphertext file? Private key d? Private key n\n "? );

}

//? Name: IsNumber

//? Function: judge numeric character array

//? Parameter: strNumber: character array

//? Return: the array of numeric words returns true, otherwise it returns false;

bool? IsNumber(? const? char? *strNumber? )

{

unsigned? int? i;

if(? ! strNumber? )

return? false;

for? (? i? =? ? ; ? i? < ? strlen(strNumber)? ; ? i++? )

{

if? (? strNumber[i]? < ? ''? ||? strNumber[i]? > ? '9'? )

return? false;

}

return? true;

}

//? Name: IsPrimeNumber

//? Function: judging prime numbers

//? Parameter: num:? Enter an integer

//? Return: the prime number returns true, otherwise it returns false;

bool? IsPrimeNumber(? unsigned? int? num? )

{

unsigned? int? i;

if(? num? < =? 1? )

return? false;

unsigned? int? sqr? =? (unsigned? int)sqrt((double)num);

for(? i? =? 2; ? i? < =