久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

計(jì)算 pow(a,b) mod n

Calculating pow(a,b) mod n(計(jì)算 pow(a,b) mod n)
本文介紹了計(jì)算 pow(a,b) mod n的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想計(jì)算 ab mod n 以用于 RSA 解密.我的代碼(如下)返回不正確的答案.有什么問題嗎?

I want to calculate ab mod n for use in RSA decryption. My code (below) returns incorrect answers. What is wrong with it?

unsigned long int decrypt2(int a,int b,int n)
{
    unsigned long int res = 1;

    for (int i = 0; i < (b / 2); i++)
    {
        res *= ((a * a) % n);
        res %= n;
    }

    if (b % n == 1)
        res *=a;

    res %=n;
    return res;
}

推薦答案

你可以試試這個(gè) C++ 代碼.我已經(jīng)將它用于 32 位和 64 位整數(shù).我確定我是從 SO 那里得到的.

You can try this C++ code. I've used it with 32 and 64-bit integers. I'm sure I got this from SO.

template <typename T>
T modpow(T base, T exp, T modulus) {
  base %= modulus;
  T result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}

你可以在 p. 的文獻(xiàn)中找到這個(gè)算法和相關(guān)的討論.244 的

You can find this algorithm and related discussion in the literature on p. 244 of

施奈爾,布魯斯 (1996).應(yīng)用密碼學(xué):C 語(yǔ)言中的協(xié)議、算法和源代碼,第二版(第 2 版).威利.ISBN 978-0-471-11709-4.

Schneier, Bruce (1996). Applied Cryptography: Protocols, Algorithms, and Source Code in C, Second Edition (2nd ed.). Wiley. ISBN 978-0-471-11709-4.

<小時(shí)>

請(qǐng)注意,在此簡(jiǎn)化版本中,乘法 result * basebase * base 可能會(huì)溢出.如果模數(shù)大于 T 寬度的一半(即大于最大 T 值的平方根),則應(yīng)改用合適的模乘算法 -請(qǐng)參閱使用原始類型進(jìn)行模乘的方法的答案.


Note that the multiplications result * base and base * base are subject to overflow in this simplified version. If the modulus is more than half the width of T (i.e. more than the square root of the maximum T value), then one should use a suitable modular multiplication algorithm instead - see the answers to Ways to do modulo multiplication with primitive types.

這篇關(guān)于計(jì)算 pow(a,b) mod n的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
主站蜘蛛池模板: 人人干天天干 | 亚洲一区 中文字幕 | 国产精品视频在线观看 | 精品国产一区二区三区性色 | 午夜激情影院 | 精品一区久久 | 欧美日韩久 | 免费看啪啪网站 | 中文字幕日韩欧美一区二区三区 | 久久精品国产久精国产 | 羞羞视频在线观免费观看 | 日韩aⅴ在线观看 | 色综合一区 | 欧美精品一区二区三区在线 | 日韩在线大片 | 羞羞视频网站 | 在线综合视频 | 国产午夜精品久久久 | 久久久久久久一区 | 一级一级毛片免费看 | 国产电影精品久久 | 成人午夜视频在线观看 | 91精品国产综合久久久动漫日韩 | 国产精品1区2区3区 欧美 中文字幕 | 久久激情av | 91精品久久久久久久久久入口 | 精品久久久久久一区二区 | 精品二区视频 | 亚洲一区二区视频 | 国产欧美日韩精品一区 | 亚洲男人网| 国产精品一区二区视频 | 国产精品视频久久久久久 | 毛片一区二区三区 | 免费99视频 | 欧美色视频免费 | 免费久久99精品国产婷婷六月 | 亚洲电影一区二区三区 | 精品久久久久久久久久久院品网 | av一区二区三区四区 | 激情欧美一区二区三区中文字幕 |