题意:两个素数P,Q。N=P*Q; T=(P-1)*(Q-1); (E*D)mod T = 1; (0<=D<T)。E与T互质,公钥是{E,N},私钥是{D,N}。原始信息M的加密过程为C=(M^E)mod N; 解密过程为 M=(C^D)mod N;("^"表示幂) 现在给出C,E,N(<2^62)。求M。

分析:先通过N分解求P,Q(pollard-rho+Miller-rabin)。通过P,Q求T,通过(E*D)mod T = 1求D(扩展欧几里德),通过M=(C^D)mod N求M。

如何使用扩展欧几里德呢,

(E*D)mod T = 1 <=> (E*D) = 1 + k*T <=> E*(D*g) + T*[(-k)*g] = g(g是T和E的最大公约数gcd(T,E))。

不过这道题好像不用这么麻烦,因为E和T互质,所以g=1。

这样就变成了a*x+b*y=gcd(a,b)的形式了。

pollard-rho和Miller-rabin算法参见poj1811解题报告 http://www.cnblogs.com/rainydays/archive/2011/09/01/2162049.html

扩展欧几里德算法参见poj1061解题报告 http://www.cnblogs.com/rainydays/archive/2013/07/19/3201618.html

#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std; typedef long long LL;
#define maxn 10000
const int S=; LL factor[maxn];
int tot; LL muti_mod(LL a,LL b,LL c){ //返回(a*b) mod c,a,b,c<2^63
a%=c;
b%=c;
LL ret=;
while (b){
if (b&){
ret+=a;
if (ret>=c) ret-=c;
}
a<<=;
if (a>=c) a-=c;
b>>=;
}
return ret;
} LL pow_mod(LL x,LL n,LL mod){ //返回x^n mod c ,非递归版
if (n==) return x%mod;
int bit[],k=;
while (n){
bit[k++]=n&;
n>>=;
}
LL ret=;
for (k=k-;k>=;k--){
ret=muti_mod(ret,ret,mod);
if (bit[k]==) ret=muti_mod(ret,x,mod);
}
return ret;
} bool check(LL a,LL n,LL x,LL t){ //以a为基,n-1=x*2^t,检验n是不是合数
LL ret=pow_mod(a,x,n),last=ret;
for (int i=;i<=t;i++){
ret=muti_mod(ret,ret,n);
if (ret==&& last!=&& last!=n-) return ;
last=ret;
}
if (ret!=) return ;
return ;
} bool Miller_Rabin(LL n){
LL x=n-,t=;
while ((x&)==) x>>=,t++;
bool flag=;
if (t>=&& (x&)==){
for (int k=;k<S;k++){
LL a=rand()%(n-)+;
if (check(a,n,x,t)) {flag=;break;}
flag=;
}
}
if (!flag || n==) return ;
return ;
} LL gcd(LL a,LL b){
if (a==) return ;
if (a<) return gcd(-a,b);
while (b){
LL t=a%b; a=b; b=t;
}
return a;
} LL Pollard_rho(LL x,LL c){
LL i=,x0=rand()%x,y=x0,k=;
while (){
i++;
x0=(muti_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if (d!=&& d!=x){
return d;
}
if (y==x0) return x;
if (i==k){
y=x0;
k+=k;
}
}
} void findfac(LL n){ //递归进行质因数分解N
if (!Miller_Rabin(n)){
factor[tot++] = n;
return;
}
LL p=n;
while (p>=n) p=Pollard_rho(p,rand() % (n-) +);
findfac(p);
findfac(n/p);
} void gcdExtend(long long a,long long b,long long &d,long long &x,long long &y)
{
if(!b) {d=a;x=;y=;return;}
gcdExtend(b,a%b,d,y,x);
y-=a/b*x;
} int main()
{
LL C, E, N, T, M, D;
LL x, y, d;
while (~scanf("%lld%lld%lld", &C, &E, &N))
{
tot = ;
findfac(N);
T = (factor[] - ) * (factor[] - );
gcdExtend(E, T, d, x, y);
D = (x % T + T) % T;
M = pow_mod(C, D, N);
printf("%lld\n", M);
}
return ;
}

poj2447的更多相关文章

随机推荐

  1. Java之字符流操作-复制文件

    package test_demo.fileoper; import java.io.*; /* * 字符输入输出流操作,复制文件 * 使用缓冲流扩展,逐行复制 * */ public class F ...

  2. swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B

    使用Web API并使用swashbuckle生成swagger文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类.当我在浏览器中打开swagger页面时,它说: Conflicting s ...

  3. 基于Python的轻量级RPC的实现

    一 什么是RPC(Remote Procedure Call)远程过程调用 二 RPC or Resful 三 重复造轮子之手撸自己的RPC(基于python语言)

  4. bzoj2152 (点分治)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2152 思路: 要想两点之间距离为3的倍数,那么用t0表示该点距离重心的距离对3取模为0, ...

  5. IDEA中在目录中如何快速指定到当前的类

    类似于myeclipse的 Link with Editor 其实也在IDEA的这个位置,跟狙击镜的图标一样,叫做Scroll from Source 不同的的是,IDEA的这个功能,需要手动点击,才 ...

  6. 学习Spring Boot:(十八)Spring Boot 中session共享

    前言 前面我们将 Redis 集成到工程中来了,现在需要用它来做点实事了.这次为了解决分布式系统中的 session 共享的问题,将 session 托管到 Redis. 正文 引入依赖 除了上篇文章 ...

  7. Nexus Repository Manager 3(CVE-2019-7238) 远程代码执行漏洞分析和复现

    0x00 漏洞背景 Nexus Repository Manager 3是一款软件仓库,可以用来存储和分发Maven,NuGET等软件源仓库.其3.14.0及之前版本中,存在一处基于OrientDB自 ...

  8. Android Service总结05 之IntentService

    Android Service总结05 之IntentService   版本 版本说明 发布时间 发布人 V1.0 添加了IntentService的介绍和示例 2013-03-17 Skywang ...

  9. 洛谷P2480 古代猪文

    这道题把我坑了好久...... 原因竟是CRT忘了取正数! 题意:求 指数太大了,首先用欧拉定理取模. 由于模数是质数所以不用加上phi(p) 然后发现phi(p)过大,不能lucas,但是它是个sq ...

  10. oracle数据库连接池查看

    select username , count(*), machine from v$session where username is not null group by username, mac ...