rsa.c

来自「掌握如何用C来实现各种算法」· C语言 代码 · 共 87 行

C
87
字号
/*****************************************************************************
*                                                                            *
*  --------------------------------- 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 + =
减小字号Ctrl + -
显示快捷键?