📄 rsa.c
字号:
/*****************************************************************************
* *
* --------------------------------- rsa.c -------------------------------- *
* *
*****************************************************************************/
#include "encrypt.h"
/*****************************************************************************
* *
* -------------------------------- modexp -------------------------------- *
* *
*****************************************************************************/
static Huge modexp(Huge a, Huge b, Huge n) {
Huge y;
/*****************************************************************************
* *
* Compute pow(a, b) % n using the binary square and multiply method. *
* *
*****************************************************************************/
y = 1;
while (b != 0) {
/**************************************************************************
* *
* For each 1 in b, accumulate y. *
* *
**************************************************************************/
if (b & 1)
y = (y * a) % n;
/**************************************************************************
* *
* Square a for each bit in b. *
* *
**************************************************************************/
a = (a * a) % n;
/**************************************************************************
* *
* Prepare for the next bit in b. *
* *
**************************************************************************/
b = b >> 1;
}
return y;
}
/*****************************************************************************
* *
* ----------------------------- rsa_encipher ----------------------------- *
* *
*****************************************************************************/
void rsa_encipher(Huge plaintext, Huge *ciphertext, RsaPubKey pubkey) {
*ciphertext = modexp(plaintext, pubkey.e, pubkey.n);
return;
}
/*****************************************************************************
* *
* ----------------------------- rsa_decipher ----------------------------- *
* *
*****************************************************************************/
void rsa_decipher(Huge ciphertext, Huge *plaintext, RsaPriKey prikey) {
*plaintext = modexp(ciphertext, prikey.d, prikey.n);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -