久久久久久久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 | 精品亚洲一区二区三区四区五区高 | 欧美精品久久久久久 | 国产成人在线观看免费 | 午夜精品一区二区三区在线视频 | 亚洲国产精品一区二区三区 | 免费一级做a爰片久久毛片潮喷 | 中文字幕免费在线 | 欧美在线视频一区二区 | 国产精品久久久久久中文字 | 午夜影视大全 | 四虎影院免费在线播放 | 九一视频在线观看 | 人人人人干 | 久久亚洲春色中文字幕久久久 | 日韩一区二区在线视频 | 国产精品九九九 | 亚洲国产一区二区三区, | 一级黄色录像片子 | 久久网一区二区三区 | 亚洲欧洲小视频 | 精品综合久久久 | 99国内精品久久久久久久 | 日本特黄a级高清免费大片 成年人黄色小视频 | 欧美一级高潮片免费的 | 国产精品美女一区二区 | 亚洲精品一区二区在线观看 | 国产伦精品一区二区三区精品视频 | 一区二区三区国产视频 | 久久精品亚洲精品国产欧美 | 青青草在线视频免费观看 | 成人av片在线观看 | 成人国产精品久久久 | 亚州毛片 | 久久精品国产亚洲一区二区 | 在线观看www视频 | 免费黄色大片 | 国产精品高潮呻吟久久 | 欧洲妇女成人淫片aaa视频 | 精品久久久久久久人人人人传媒 | 日本精品在线一区 |