• RSA Encoder


      1//------------------------------------
      2// RSA Encoder
      3// 创建于 2004年10月11日
      4// 创建人 luoluo
      5// 说明 一个说明RSA原理的简单程序
      6//     达不到Encoder的要求,且由于Jscript
      7//     的整型精度不够,不适合做RSA加密解密
      8//     的程序
      9//------------------------------------
     10
     11
     12/////////////////////////////////////////////////////////////////
     13
     14//------------------------------------
     15// 函数名: isNumber
     16// 参数: int n
     17// 返回值: boolean
     18// 作用: 判断变量是否是数字
     19//------------------------------------
     20function isNumber(n) {
     21    return ! isNaN(n);
     22}

     23
     24
     25//------------------------------------
     26// 函数名: isInt
     27// 参数: int n
     28// 返回值: boolean
     29// 作用: 判断变量是否是整数
     30//------------------------------------
     31function isInt(n) {
     32    if (! isNumber(n))
     33        throw n + " is not a number";
     34 
     35    var re = /\./i;
     36    return (n.toString().search(re) == -1);
     37}

     38
     39//------------------------------------
     40// 函数名: gcd
     41// 参数一: x int
     42// 参数二: y int
     43// 返回值: int
     44// 作用: 求两个数的最大公因数(公约数)
     45//------------------------------------
     46function gcd(x, y) {
     47    // 验证参数的类型
     48    if (! isInt(x))
     49        throw x + " is not a integer";
     50    if (! isInt(y))
     51        throw y + " is not a integer";
     52        
     53    x = parseInt(x);
     54    y = parseInt(y);
     55            
     56    var ret;    // 存放返回值
     57    
     58    // 取参数的绝对值
     59    if (x < 0
     60        x = -x;
     61    if (y < 0)
     62        y = -y;
     63        
     64    // 判断是否为0
     65    if (! (x + y))
     66        throw "x and y can't be zero";
     67    
     68    // 计算最大公因数
     69    ret = y;
     70    
     71    while (x > 0{
     72        ret = x;
     73        x = y % x;
     74        y = ret;
     75    }

     76    
     77    // 返回
     78    return ret;
     79}

     80
     81//------------------------------------
     82// 函数名: isEven
     83// 参数: int n
     84// 返回值: boolean
     85// 作用: 判断变量是否是偶数
     86//------------------------------------
     87function isEven(n) {
     88    if (! isInt(n))
     89        throw n + " is not a integer";
     90        
     91    return (n & 0x01 == 0);
     92}

     93
     94//------------------------------------
     95// 函数名: isEven
     96// 参数: int n
     97// 返回值: boolean
     98// 作用: 判断变量是否是奇数
     99//------------------------------------
    100function isOdd(n) {
    101    if (! isInt(n))
    102        throw n + " is not ainteger";
    103        
    104    return (n & 0x01 != 0);
    105}

    106
    107//------------------------------------
    108// 函数名: inverse
    109// 参数: int u
    110// 参数: int v
    111//       v * d = 1 (mod u)
    112// 返回值: int
    113// 作用: 返回v关于u的乘法逆元素
    114// 说明: 修改自课本上的程序
    115//------------------------------------
    116function inverse(u, v) {
    117    if (! isInt(u))
    118        throw u + " is not a integer";
    119    if (! isInt(v))
    120        throw v + " is not a integer";
    121    
    122    u = parseInt(u);
    123    v = parseInt(v);
    124    
    125    var t1, t2, t3;
    126    var u1, u2, u3;
    127    
    128    if (isEven(u) && isEven(v))
    129        return 0;
    130        
    131    u1 = 1;
    132    u2 = 0;
    133    u3 = u;
    134    t1 = v;
    135    t2 = u - 1;
    136    t3 = v;
    137    
    138    do {
    139        do {
    140            if (isEven(u3)) {
    141                if (isOdd(u1) || isOdd(u2)) {
    142                    u1 += v;
    143                    u2 += u;
    144                }

    145                
    146                u1 >>= 1;
    147                u2 >>= 1;
    148                u3 >>= 1;
    149            }

    150            
    151            if (isEven(t3) || u3 < t3) {
    152                u1^=t1, t1^=u1, u1^=t1;
    153                u2^=t2, t2^=u2, u2^=t2;
    154                u3^=t3, t3^=u3, u3^=t3;
    155            }

    156        }
     while (isEven(u3));
    157        
    158        while ((u1 < t1) || (u2 < t2)) {
    159            u1 += v;
    160            u2 += u;
    161        }

    162        
    163        u1 -= t1;
    164        u2 -= t2;
    165        u3 -= t3;
    166    }
     while (t3 > 0);
    167    
    168    while (u1 > v && u2 >= u) {
    169        u1 -= v;
    170        u2 -= u;
    171    }

    172    
    173    return (u - u2);
    174}

    175
    176//------------------------------------
    177// 函数名: isPrime
    178// 参数: int n
    179// 返回值: boolean
    180// 作用: 判断变量是否是素数
    181//------------------------------------
    182function isPrime(n) {
    183    if (! isInt(n))
    184        throw n + " is not a integer";
    185    
    186    var ret = true;    
    187    
    188    for (var i = 2; i <= n - 1; i ++{
    189        if (! (n % i))
    190        {
    191            ret = false;
    192            break;
    193        }

    194    }

    195    
    196    return ret;
    197}

    198
    199//------------------------------------
    200// 函数名: randomPrime
    201// 参数: int n
    202// 返回值: int
    203// 作用: 产生随机十进制n位素数
    204//------------------------------------
    205function randomPrime(n) {
    206    var x;
    207    
    208    do {
    209        x = Math.random();
    210        x = parseInt(x * Math.pow(10, n));
    211    }
     while (! isPrime(x) || x.toString().length != n);
    212    
    213    return x;
    214}

    215
    216//------------------------------------
    217// 函数名: randomPrimeOfX
    218// 参数: int n
    219// 返回值: int
    220// 作用: 产生随机十进制n位与X互素的数
    221//------------------------------------
    222function randomPrimeOfX(x, n) {
    223    var y;
    224    
    225    do {
    226        y = Math.random();
    227        y = parseInt(y * Math.pow(10, n));
    228    }
     while (gcd(x, y) != 1 || y.toString().length != n);
    229    
    230    return y;
    231}

    232
    233
    234//------------------------------------
    235// 类名: RSAEncoder
    236// 作用: RSA加密解密
    237//------------------------------------
    238function RSAEncoder() {
    239    this.p = randomPrime(2);
    240    this.q = randomPrime(2);
    241    
    242    this.n = 0;
    243    this.e = 0;
    244    this.d = 0;
    245    
    246    this.init = init;
    247    
    248    this.encode = encode;
    249    
    250    this.decode = decode;
    251}

    252
    253//------------------------------------
    254// 函数名: init
    255// 作用: RSAEncoder初始化
    256//------------------------------------
    257function init() {
    258    this.n = this.p * this.q;
    259    this.e = randomPrimeOfX((this.p - 1* (this.q - 1), 2);
    260    this.d = inverse((this.p - 1* (this.q - 1), this.e);
    261}

    262
    263//------------------------------------
    264// 函数名: encode
    265// 参数: int m
    266// 返回值: int
    267// 作用: 加密信息
    268//------------------------------------
    269function encode(m) {
    270    return (Math.pow(m, this.e) % this.n);
    271}

    272
    273
    274//------------------------------------
    275// 函数名: decode
    276// 参数: int m
    277// 返回值: int
    278// 作用: 解密信息
    279//------------------------------------
    280function decode(c) {
    281    return (Math.pow(c, this.d) % this.n);
    282}

    283
  • 相关阅读:
    MFC-窗口指针
    MFC-EditControl文本控件(多行输入)一行一行读取
    MFC-文件对话框
    MFC-ToolBar工具栏
    MFC-CMenu菜单
    MFC-访问对话框控件的七种方法
    CC++:scanf的用法
    C/C++:printf的用法
    C/C++:创建第一个程序helloworld!
    NX二次开发-NX客户机与服务器FTP上传下载文件
  • 原文地址:https://www.cnblogs.com/luoluo/p/267903.html
Copyright © 2020-2023  润新知