File: rsa.5c

package info (click to toggle)
nickle 2.77-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,612 kB
  • ctags: 3,746
  • sloc: ansic: 26,986; yacc: 1,873; sh: 954; lex: 884; makefile: 225
file content (68 lines) | stat: -rw-r--r-- 1,426 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * RSA cryptosystem
 *
 * Copyright © 2001  Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 *
 * Bart Massey 1999/1
 *
 * Must first load numbers.5c
 *
 * [note that encryption and decryption commute]
 *
 * set_private_key(p,q,e)
 *   call with p and q prime, e small random, less than (p-1) * (q-1),
 *   relatively prime to this quantity.  The primes can be
 *   conveniently generated using the supplied Miller-Rabin code:
 *   see the function primebits().
 *
 * set_public_key(n,e)
 *   set just the public key information for encryption
 *
 * encrypt(m)
 *   return the encryption of m
 *
 * decrypt(c)
 *   return the decryption of c
 */

autoimport Numbers

namespace RSA {

  global int e;   /* encryption exponent */
  global int n;   /* public key */
  global int d = 0;   /* decryption exponent (0 for encrypt-only) */

  public int encrypt(int m) {
    return bigpowmod(m, e, n);
  }

  public int decrypt(int c) {
    exception decrypt_public_key();

    if (d == 0)
        raise decrypt_public_key();
    return bigpowmod(c, d, n);
  }

  public void set_private_key(int p, int q, int e0) {
    int phi = (p - 1) * (q - 1);

    n = p * q;
    if (e0 % 2 == 0)
      e0++;
    while (gcd(e0, phi) > 1)
      e0 += 2;
    e = e0;
    d = zminv(e, phi);
  }

  public void set_public_key(int n0, int e0) {
    n = n0;
    e = e0;
    d = 0;
  }

}