简体   繁体   English

bash中的RSA算法(私钥计算)

[英]RSA Algorithm (private key calculation) in bash

Looking for help on how to calculate the RSA secret key in a bash script 'd' when p, q & e are given. 当给出p,q和e时,寻求有关如何在bash脚本“ d”中计算RSA密钥的帮助。

Although I know this query has come up before, all I seem to find is theoretical advice on the calculation of the RSA private key when p, q & e are given. 尽管我知道这个查询以前已经提出过,但是我似乎发现的只是在给出p,q和e时计算RSA私钥的理论建议。

I am trying to put a calculation in a bash script together and so would really like some explanation on the calculations possible in bash (with expr or bc) 我正在尝试将计算放在bash脚本中,因此我真的想对bash中的计算(使用expr或bc)进行一些解释。

I found a site that basically advised the following; 我发现了一个基本上可以提供以下建议的网站; (with 't' being the totient of p*q, or rather the totient of 'n') (其中“ t”是p * q的总和,或者更确切地说是“ n”的总和)

d=$(echo "((2 * $t) + 1) / $e" | bc)

But that only seems to work on that specific case and did not provide the correct output when trying on different cases. 但这似乎仅适用于该特定情况,并且在尝试不同情况时无法提供正确的输出。

Any help on how I could write a bash formula for this calculation ? 关于如何为该计算编写bash公式的任何帮助?

From the defition of RSA 从RSA的定义

|e*d| | e * d | = 1 (mod fi(p*q)) = 1(mod fi(p * q))

where fi(n) is the Euler function 其中fi(n)是Euler函数

So you need to calculate the inversion of e in this modulo. 因此,您需要计算此模的e的倒数。 Don't forget to check if GCD(e,fi(p*q))=1 or you may not find an inversion of e in the given modulo. 不要忘记检查GCD(e,fi(p * q))= 1,否则您可能找不到给定模中e的倒数。

You can achieve this by using Extended Euclidean algorithm. 您可以使用扩展欧几里得算法来实现。 There is a lot of pseudocodes on the web. 网络上有很多伪代码。 Do you need more help? 您需要更多帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM