小程序RSA加密、解密、加签、验签

小程序RSA加密、解密、加签、验签,第1张

npm install  wxapp_rsa

var RSA = require('/wxapp_rsa.js')

// RSA加签

    var sign_rsa = new RSA.RSAKey()

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

    sign_rsa = RSA.KEYUTIL.getKey(privateKey_pkcs1)

    console.log('签名RSA:')

    console.log(sign_rsa)

    var hashAlg = 'MD5withRSA'

    var hSig = sign_rsa.signString("12345678901234567890", hashAlg)

    hSig = RSA.hex2b64(hSig)// hex 转 b64

    console.log("签名结果:" + hSig)

    // RSA 验签

    var verify_rsa = new RSA.RSAKey()

    verify_rsa = RSA.KEYUTIL.getKey(publicKey_pkcs1)

    console.log('验签RSA:')

    console.log(verify_rsa)

    hSig = RSA.b64tohex(hSig)

    var ver = verify_rsa.verifyString("12345678901234567890", hSig)

    console.log('验签结果:' + ver)

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

    var encrypt_rsa = new RSA.RSAKey()

    encrypt_rsa = RSA.KEYUTIL.getKey(rsa_public_key)

    console.log('加密RSA:')

    console.log(encrypt_rsa)

    var encStr = encrypt_rsa.encrypt('1234567890')

    console.log(encStr)

    encStr = RSA.hex2b64(encStr)

    console.log("加密结果:" + encStr)

    // RSA 解密

    var decrypt_rsa = new RSA.RSAKey()

    decrypt_rsa = RSA.KEYUTIL.getKey(rsa_public_key_private)

    console.log('解密RSA:')

    console.log(decrypt_rsa)

    encStr = RSA.b64tohex(encStr)

    var decStr = decrypt_rsa.decrypt(encStr)

    console.log("解密结果:" + decStr)

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

//RSA??????

typedef struct RSA_PARAM_Tag

{

unsigned __int64p, q //????,?????????

unsigned __int64f //f=(p-1)*(q-1),?????????

unsigned __int64n, e //??,n=p*q,gcd(e,f)=1

unsigned __int64d //??,e*d=1 (mod f),gcd(n,d)=1

unsigned __int64s //??,??2^s<=n????s,?log2(n)

} RSA_PARAM

//????

const static long g_PrimeTable[]=

{

3,

5,

7,

11,

13,

17,

19,

23,

29,

31,

37,

41,

43,

47,

53,

59,

61,

67,

71,

73,

79,

83,

89,

97

}

const static long g_PrimeCount=sizeof(g_PrimeTable) / sizeof(long)const unsigned __int64 multiplier=12747293821

const unsigned __int64 adder=1343545677842234541//????

typedef struct _RandNumber

{

/* */

unsigned __int64randSeed/* */

}RandNumber

static voidCreateRandNumber(RandNumber *pRand, unsigned __int64 s)

static unsigned __int64Random(RandNumber *pRand, unsigned __int64 n)

/* */

void CreateRandNumber(RandNumber *pRand, unsigned __int64 s)

{

if(!s)

{

pRand->randSeed= (unsigned __int64)time(NULL)

}

else

{

pRand->randSeed=s

}

}

/* */

unsigned __int64 Random(RandNumber *pRand, unsigned __int64 n)

{

pRand->randSeed=multiplier * pRand->randSeed + adder

return pRand->randSeed % n

}

static RandNumber g_Rnd

/*

????,??? x=a*b mod n

*/

unsigned __int64 MulMod(unsigned __int64 a, unsigned __int64 b, unsigned __int64 n)

{

return a * b % n

}

/*

????,??? x=base^pow mod n

*/

unsigned __int64 PowMod(unsigned __int64 base, unsigned __int64 pow, unsigned __int64 n)

{

unsigned __int64a=base, b=pow, c=1

while(b)

{

while(!(b &1))

{

b>>=1 //a=a * a % n //?????????64????,?????a*a?a>=2^32????????,??????????64?

a=MulMod(a, a, n)

}

b-- //c=a * c % n //??????,??64???????32????????????????

c=MulMod(a, c, n)

}

return c

}

/*

Rabin-Miller????,??????1,????0?

n??????

??:????????????,???????????1/4

*/

long RabinMillerKnl(unsigned __int64 n)

{

unsigned __int64b, m, j, v, i

unsigned __int64 TWO = 2

m=n - 1

j=0 //0?????m?j,??n-1=m*2^j,??m????,j?????

while(!(m &1))

{

++j

m>>=1

}

//1??????b,2<=b<n-1

b=2 + Random(&g_Rnd, n - 3)

//2???v=b^m mod n

v=PowMod(b, m, n)

//3???v==1,????

if(v == 1)

{

return 1

}

//4??i=1

i=1

//5???v=n-1,????

while(v != n - 1)

{

//6???i==l,???,??

if(i == j)

{

return 0

}

//7?v=v^2 mod n,i=i+1

v=PowMod(v, TWO, n)

++i

//8????5

}

return 1

}

/*

Rabin-Miller????,??????loop?

??????1,????0

*/

long RabinMiller(unsigned __int64 n, long loop)

{

long i=0

//?????????,????

for(i=0i <g_PrimeCounti++)

{

if(n % g_PrimeTable[i] == 0)

{

return 0

}

}

//????Rabin-Miller??loop?,??????????????(1/4)^loop

for(i=0i <loopi++)

{

if(!RabinMillerKnl(n))

{

return 0

}

}

return 1

}

/*

??????bits?(????)???,??32?

*/

unsigned __int64 RandomPrime(char bits)

{

unsigned __int64base

do

{

base= (unsigned long)1 <<(bits - 1) //??????1

base+=Random(&g_Rnd, base) //????????

base|=1 //??????1,??????

} while(!RabinMiller(base, 30)) //????-????30?

return base //?????????

}

/*

???????????

*/

unsigned __int64 EuclidGcd(unsigned __int64 p, unsigned __int64 q)

{

unsigned __int64a=p >q ? p : q

unsigned __int64b=p <q ? p : q

unsigned __int64t

if(p == q)

{

return p //????,?????????

}

else

{

while(b)//?????,gcd(a,b)=gcd(b,a-qb)

{

a=a % b

t=a

a=b

b=t

}

return a

}

}

/*

Stein???????

*/

unsigned __int64 SteinGcd(unsigned __int64 p, unsigned __int64 q)

{

unsigned __int64a=p >q ? p : q

unsigned __int64b=p <q ? p : q

unsigned __int64t, r=1

if(p == q)

{

return p //????,?????????

}

else

{

while((!(a &1)) &&(!(b &1)))

{

r<<=1 //a?b?????,gcd(a,b)=2*gcd(a/2,b/2)

a>>=1

b>>=1

}

if(!(a &1))

{

t=a //??a???,??a,b

a=b

b=t

}

do

{

while(!(b &1))

{

b>>=1 //b???,a????,gcd(b,a)=gcd(b/2,a)

}if(b <a)

{

t=a //??b??a,??a,b

a=b

b=t

}b=(b - a) >>1//b?a????,gcd(b,a)=gcd((b-a)/2,a)

} while(b)

return r * a

}

}

/*

??a?b,?x,??a*x =1 (mod b)

?????a*x-b*y=1??????

*/

unsigned __int64 Euclid(unsigned __int64 a, unsigned __int64 b)

{

unsigned __int64m, e, i, j, x, y

longxx, yy

m=b

e=a

x=0

y=1

xx=1

yy=1

while(e)

{

i=m / e

j=m % e

m=e

e=j

j=y

y*=i

if(xx == yy)

{

if(x >y)

{

y=x - y

}

else

{

y-=x

yy=0

}

}

else

{

y+=x

xx=1 - xx

yy=1 - yy

}x=j

}if(xx == 0)

{

x=b - x

}return x

}

/*

??????RSA????

*/

RSA_PARAM RsaGetParam(RandNumber Rnd)

{

RSA_PARAM Rsa={ 0 }

unsigned __int64t

Rsa.p=RandomPrime(16) //????????

Rsa.q=RandomPrime(16)

Rsa.n=Rsa.p * Rsa.q

Rsa.f=(Rsa.p - 1) * (Rsa.q - 1)

do

{

Rsa.e=Random(&Rnd, 65536) //??2^16,65536=2^16

Rsa.e|=1 //??????1,??????,?f?????,???,?????

} while(SteinGcd(Rsa.e, Rsa.f) != 1)

Rsa.d=Euclid(Rsa.e, Rsa.f)

Rsa.s=0

t=Rsa.n >>1

while(t)

{

Rsa.s++ //s=log2(n)

t>>=1

}

return Rsa

}

/*

??-????

*/

void TestRM(void)

{

unsigned long k=0

unsigned __int64 i = 0

printf(" - Rabin-Miller prime check.\n\n")

for(i=4197900001i <4198000000i+=2)

{

if(RabinMiller(i, 30))

{

k++

printf("%ul\n", i)

}

}

printf("Total: %ul\n", k)

}

void Usage(void)

{

printf("0. exit\n")

printf("1. encryption\n")

printf("2. decrypt\n")

printf("enter your choice:")

}

int GetChoice(void)

{

char s[80] = {0}

fgets(s, sizeof(s), stdin)

if ('0' >s[0] || '9' <s[0] || '\n' != s[1])

{

return -1

}

else

{

return s[0] - '0'

}

}

#define ENCRYPT_CODE 1

#define DECRYPT_CODE 2

#define EXIT_CODE 0

#define MAX_SIZE 1024

int main(void)

{

RSA_PARAM r

charpSrc[MAX_SIZE]={0}

unsigned long n = 0

unsigned char *q = NULL

unsigned char pDec[MAX_SIZE] = {0}

unsigned __int64pEnc[MAX_SIZE] = {0}

unsigned __int64tmp = 0

unsigned long i=0

int choice = 0

unsigned __int64 s = 0

CreateRandNumber(&g_Rnd, s)

r=RsaGetParam(g_Rnd)

/*

printf("p=%ul\n", r.p)

printf("q=%ul\n", r.q)

printf("f=(p-1)*(q-1)=%ul\n", r.f)

printf("n=p*q=%ul\n", r.n)

printf("e=%ul\n", r.e)

printf("d=%ul\n", r.d)

printf("s=%ul\n", r.s)

*/

while (1)

{

memset(pSrc, 0, MAX_SIZE)

memset(pDec, 0, MAX_SIZE)

memset(pEnc, 0, MAX_SIZE)

Usage()

choice = GetChoice()

switch(choice)

{

case ENCRYPT_CODE:

printf("Source:%s", pSrc)

scanf("%s", pSrc)

n = strlen(pSrc)

getchar()

q= (unsigned char *)pSrc

printf("Encode:")

for(i = 0i <ni++)

{

tmp = q[i]

pEnc[i]=PowMod(tmp, r.e, r.n)

printf("%x ", pEnc[i])

}

printf("\n")

break

case DECRYPT_CODE:

printf("Enter length of ciphertext:")

scanf("%d", &n)

getchar()

for(i=0i <ni++)

{

printf("Enter ciphertext(%d):", i+1)

scanf("%x", &pEnc[i])

getchar()

}

printf("Decode:")

for(i=0i <ni++)

{

pDec[i]=PowMod(pEnc[i], r.d, r.n)

printf("%x ", (unsigned long)pDec[i])

}

printf("\n")

printf("%s\n", (char *)pDec)

break

case EXIT_CODE:

break

default:

break

}

if (EXIT_CODE == choice)

{

break

}

else

{

continue

}

}

return 0

}

您好,RSA小程序是一种安全的身份验证方式,它使用公钥加密和私钥解密的方式来确保用户身份的安全性。H5是一种网页开发技术,它可以创建动态、交互式的网页,并且可以在任何设备上运行,而无需安装任何软件。H5技术可以帮助开发者更加灵活地创建出精美的网页,提高用户体验。


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

原文地址: http://outofmemory.cn/yw/11657945.html

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

发表评论

登录后才能评论

评论列表(0条)

保存