pow(a,b,c) operator in python returns (a**b)%c . If I have values of b, c, and the result of this operation (res=pow(a,b,c)), how can I find the value of a?

解决方案

Despite the statements in the comments this is not the discrete logarithm problem. This more closely resembles the RSA problem in which c is the product of two large primes, b is the encrypt exponent, and a is the unknown plaintext. I always like to make x the unknown variable you want to solve for, so you have y= xb mod c where y, b, and c are known, you want to solve for x. Solving it involves the same basic number theory as in RSA, namely you must compute z=b-1 mod λ(c), and then you can solve for x via x = yz mod c. λ is Carmichael's lambda function, but you can also use Euler's phi (totient) function instead. We have reduced the original problem to computing an inverse mod λ(c). This is easy to do if c is easy to factor or we already know the factorization of c, and hard otherwise. If c is small then brute-force is an acceptable technique and you can ignore all the complicated math.

Here is some code showing these steps:

import functools

import math

def egcd(a, b):

"""Extended gcd of a and b. Returns (d, x, y) such that

d = a*x + b*y where d is the greatest common divisor of a and b."""

x0, x1, y0, y1 = 1, 0, 0, 1

while b != 0:

q, a, b = a // b, b, a % b

x0, x1 = x1, x0 - q * x1

y0, y1 = y1, y0 - q * y1

return a, x0, y0

def inverse(a, n):

"""Returns the inverse x of a mod n, i.e. x*a = 1 mod n. Raises a

ZeroDivisionError if gcd(a,n) != 1."""

d, a_inv, n_inv = egcd(a, n)

if d != 1:

raise ZeroDivisionError('{} is not coprime to {}'.format(a, n))

else:

return a_inv % n

def lcm(*x):

"""

Returns the least common multiple of its arguments. At least two arguments must be

supplied.

:param x:

:return:

"""

if not x or len(x) < 2:

raise ValueError("at least two arguments must be supplied to lcm")

lcm_of_2 = lambda x, y: (x * y) // math.gcd(x, y)

return functools.reduce(lcm_of_2, x)

def carmichael_pp(p, e):

phi = pow(p, e - 1) * (p - 1)

if (p % 2 == 1) or (e >= 2):

return phi

else:

return phi // 2

def carmichael_lambda(pp):

"""

pp is a sequence representing the unique prime-power factorization of the

integer whose Carmichael function is to be computed.

:param pp: the prime-power factorization, a sequence of pairs (p,e) where p is prime and e>=1.

:return: Carmichael's function result

"""

return lcm(*[carmichael_pp(p, e) for p, e in pp])

a = 182989423414314437

b = 112388918933488834121

c = 128391911110189182102909037 * 256

y = pow(a, b, c)

lam = carmichael_lambda([(2,8), (128391911110189182102909037, 1)])

z = inverse(b, lam)

x = pow(y, z, c)

print(x)

python中pow_如何在python中找到pow(a,b,c)的反向?相关推荐

  1. python pop() ,如何在Python的列表或数组中移除元素

    python pop() ,如何在Python的列表或数组中移除元素 在本文中,你将学习如何使用Python内置的 pop() 方法,最后,你将知道如何使用 pop() 从 Python 中的列表中删 ...

  2. python多项式回归_如何在Python中实现多项式回归模型

    python多项式回归 Let's start with an example. We want to predict the Price of a home based on the Area an ...

  3. python调用soap_如何在python zeep中调用soap api而不使用wsdl(非wsdl模式)?使用用户和密码身份验证调用位置URL...

    我无法在Zeep python客户端中为SOAP API验证用户身份 . 我有两个网址: 1) http://credotrade.stg-tradingcrm.com:8093/mex - 它指定了 ...

  4. python hadoop streaming_如何在Hadoop中使用Streaming编写MapReduce(转帖)

    作者:马士华 发表于:2008-03-05 12:51 最后更新于:2008-03-25 11:18 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息. http://www ...

  5. python大括号_如何在python字符串中打印文字大括号字符并在其上使用.format?

    如何在python字符串中打印文字大括号字符并在其上使用.format? x = " \{ Hello \} {0} " print x.format(42) 给我:{Hello} ...

  6. python缓冲区_如何在Python中使用Google的协议缓冲区

    python缓冲区 When people who speak different languages get together and talk, they try to use a languag ...

  7. python使用spark_如何在Python中编写简单代码,并且速度超越Spark?

    全文共3482字,预计学习时长7分钟 如今,大家都在Python工具(pandas和Scikit-learn)的简洁性.Spark和Hadoop的可扩展性以及Kubernetes的操作就绪之间做选择. ...

  8. spyder python 使用_如何在spyder中使用vpython?

    我试着用vpython,无论如何,但我失败了...在 首先,我在win8.1上安装了anacondapython2.7.10. 然后,我通过在命令行中输入以下命令来安装Vpython: conda安装 ...

  9. 【Python】Windows如何在cmd中切换python版本

    相信很多小伙伴都会有像我一样经历,在windows中装了很多python版本,那么如果我们正式使用的时候应该如何切换呢? [方法一]从环境变量中切换python 第一步: 打开环境变量 第二步:打开系 ...

最新文章

  1. 谷歌CEO:没有这项能力,再牛的程序员也不要!
  2. 没有精准定位,万物还能实现互联吗?
  3. jQuery如何实现表单的自动提示
  4. 如何快速上手mysql_如何快速上手数据库操作?
  5. python主要应用的几个领域
  6. linux c编程操作数据库(sqlite3应用)
  7. sam格式的结构和意义_各种格式的练字本,对写字真有帮助吗
  8. DNS如何查找IP?
  9. 游戏策划小白笔记——Common Sense(二)
  10. Kali Linux下社工密码字典生成工具Cupp教程
  11. 自我认知测试软件,职业生涯测评系统在线测试
  12. 未过GMS认证和CTS测试的ODM厂商如何使用google play
  13. [iOS]在xcode的iOS虚拟机中对BLE(蓝牙4.0)进行调试
  14. 强化学习——初探强化学习
  15. EasyExcel动态导出-动态头
  16. h标签本身自带间距 去除方法
  17. Uncaught TypeError: Cannot read property ‘length‘ of null解决经验贴
  18. VMware设置虚拟机与物理主机处于同一网段,桥接模式
  19. 【分享】免费的国际一级域名和100M支持asp、cgi空间
  20. vs2019 预览版发行说明--转载自微软官网(不定期转载更新)

热门文章

  1. 分享改进 高性能通用分表归档存储过程测试结果更新
  2. 忙了12周,手机摄像头模组测试PCB板终于画出来了。
  3. (转)计算机视觉CV 圈子
  4. C++中 vector(容器)的用法
  5. TS流解析 二 *****
  6. Private strand flush not complete 说明
  7. Java集合(三、Hashtable)
  8. Docker应用容器引擎_简介---Docker工作笔记003
  9. Netty工作笔记0032---零拷贝AIO内容梳理
  10. Netty工作笔记0002---Netty的应用场景