大战 RSA
qqqaaazzz_qwq · · 科技·工程
全部信息在这里了:
#include <bits/stdc++.h>
#define int long long
#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define TIAO cout << fixed << setprecision(15)
using namespace std;
/*
by qqqaaazzz
*/
string n = "3011995306934585850263488529099908176819796209010113208819181301379538764349170998122690984516837549605542828625122612758655179448613671250670472265463351791205881495595569395800486722377903738788885262922350833121912690485758591604243940152052161311943811467587246568413069869333644642847936448339973223120063869750696037722365951250116525318133344027331148120051552784225520040427309101539865952215783146041064376136249953157576537447993508935416478911862651416636273364200206739568636727206232833957577662231466145119600519578160992263488270217087983598586873171788172199572107506068027832934113560675667137536792737251690599260458917553905037466489503229052315600829171083097118668744269086927794354680713166389283253805505897456162953020823537356862317758780987671200031622738814760513583329415465138608990122306480489978262386565179535386616249208907937867714551782652342623248447275085875678472246926794554083448774992293443359500311836900765226922394289344096935411135526191089699943043045373001725151577882095501353";
string gong = "594833119289633021260364364294867373362059533589144580131478699833172893022859901225509169087732772733366600753559920080219799342196898867991725098465592120663250919440646522788354849480896008910419362486557675682152311871112900701253631699723852331971649766879284681571591281648789850440554483091023252281270519630317960500803929542377971924886883004207997222569456660409004983786196383435049662263166593169414092189000812653915626723103746350173583026643680414814152541038426509395985452862196484587";
mt19937 rd(1);
struct Hua{//小鸟游六花!
int a[1010];
int n;
int mi[20];
int B = 9;
Hua(){
n = 0;
memset(a,0,sizeof(a));
mi[0] = 1;
for (int i=1;i<=B;i++) mi[i] = mi[i-1]*10;
}
void duru(string q){
while(q.length()==0||q.length()%B) q = "0"+q;
n = q.length()/B;
for (int i=0;i<n;i++){
int l = (n-i)*B-B,r = (n-i)*B;
a[i] = 0;
for (int j=l;j<r;j++) a[i] = a[i]*10+q[j]-'0';
}
}
string shuchu(){
bool ok = 0;
string res;
for (int i=n-1;i>=0;i--){
for (int j=B-1;j>=0;j--){
if((a[i]/mi[j])%10||ok){
ok = 1;
res+=(a[i]/mi[j])%10+'0';
}
}
}
if(ok==0) return "0";
return res;
}
Hua operator + (const Hua &x) const{
Hua res;
for (int i=0;i<x.n;i++) res.a[i]+=x.a[i];
for (int i=0;i<n;i++) res.a[i]+=a[i];
res.n = max(n,x.n);
for (int i=0;i<res.n;i++)res.a[i+1]+=res.a[i]/mi[B],res.a[i]%=mi[B];
while(res.a[res.n])++res.n;
return res;
}
Hua operator * (const Hua &x) const{
Hua res;
for (int i=0;i<x.n;i++){
for (int j=0;j<n;j++){
res.a[i+j]+=a[j]*x.a[i];
int cur = i+j;
while(res.a[cur]>=mi[B]) res.a[cur+1]+=res.a[cur]/mi[B],res.a[cur]%=mi[B],++cur;
res.n = max(res.n,cur+1);
}
}
return res;
}
bool operator < (const Hua &x) const{
int Q = max(n,x.n);
for (int i=Q;i>=0;i--){
if(a[i]>x.a[i]) return 0;
if(a[i]<x.a[i]) return 1;
}
return 0;
}
Hua operator - (const Hua &x) const{
Hua res;
for (int i=0;i<x.n;i++) res.a[i]-=x.a[i];
for (int i=0;i<n;i++) res.a[i]+=a[i];
res.n = max(n,x.n);
for (int i=0;i<res.n;i++){
int want = (res.a[i]%mi[B]+mi[B])%mi[B];
res.a[i+1]+=(res.a[i]-want)/mi[B],res.a[i]=want;
}
while(res.n&&res.a[res.n-1]==0)--res.n;
return res;
}
Hua operator / (const Hua &x) const{
Hua base;
Hua ans;
Hua stu;stu.n = 2;//只是一个占位
stu.a[1] = 1;
for (int i=n-1;i>=0;i--){
Hua tt;
tt.n = 1;
tt.a[0] = a[i];
base = (base*stu);
base = (base+tt);
//现在减掉若干个 x 吧
int l = 0,r = 999999999,res = 0;
while(l<=r){
int mid = (l+r)/2;
tt.n = 1;
tt.a[0] = mid;
if(base<(x*tt)){
r = mid-1;
}
else{
res = mid;
l = mid+1;
}
}
tt.n = 1;
tt.a[0] = res;
base = base-(tt*x);
ans.a[i] = res;
if(res&&ans.n==0) ans.n = i+1;
}
return ans;
}
Hua operator % (const Hua &x) const{
Hua pan;
pan.n = n;
for (int i=0;i<n;i++) pan.a[i] = a[i];
return pan-pan/x*x;
}
bool operator == (const Hua &x) const{
for (int i=0;i<max(n,x.n);i++) if(a[i]!=x.a[i]) return 0;
return 1;
}
};
Hua single(int x){
Hua res;
res.n = 1;
res.a[0] = x;
return res;
}
Hua fpm(Hua a,Hua b,Hua p){
Hua res = single(1);
int G = 0;
int Z = b.n;
int lst = 0;
while(b.shuchu()!="0"){
if(b.n!=lst){
cout << (Z-b.n)*1.0/Z*100 << "%\n";
lst = b.n;
}
if(b.n!=G-1)
if(b.a[0]%2) res = (res*a)%p;
a = (a*a)%p;
b = b/single(2);
}
return res;
}
void read(Hua &x){
string c;
cin >> c;
x.duru(c);
}
bool Miller(Hua n){
if(n==single(2)||n==single(3)||n==single(5)) return 1;
if(n==single(1)||n==single(4)||n==single(6)) return 0;
// int s = 0,c = n-1;
// while(c%2==0)c/=2,++s;
int s = 0;
Hua c = n-single(1);
while(c%single(2)==single(0))c=c/single(2),++s;
// cout << c.shuchu();
// return 0;
for (int i=1;i<=11;i++){
// int k = rd()%(n-2)+1;
Hua k = single(rd()%1000000000);
k = k%(n-single(2));
k = k+single(1);
// cout << k.shuchu() << "??\n";
Hua a = fpm(k,c,n);
// cout << a.shuchu() << "))\n";
if(a==single(1)) continue;
for (int j=0;j<s;j++){
// cout << a.shuchu() << "??.\n";
if(a==n-single(1)){
a = (a*a)%n;
break;
}
if((a*a)%n==single(1)) break;
a = (a*a)%n;
}
if(!(a==single(1))) return 0;
}
return 1;
}
//
//int f(int x,int c,int n){
// return ((__int128)x*x+c)%n;
//}
//int ans = 1;
//vector<int> G;
//void attempt(int n){
// if(Miller(n)){
// G.push_back(n);
// return;
// }
// while(1){
// int c = rd()%n+1;
// int slow = rd()%n+1;
// int fast = f(slow,c,n);
// while(slow!=fast){
// if(__gcd((slow-fast+n),n)!=1&&__gcd((slow-fast+n),n)!=n){
// attempt(__gcd((slow-fast+n),n));
// attempt(n/__gcd((slow-fast+n),n));
// return;
// }
// slow = f(slow,c,n);
// fast = f(fast,c,n);fast = f(fast,c,n);
// }
// }
//}
//void solve(int n){
// G.clear();
// if(n==1) return;
// attempt(n);
// sort(G.begin(),G.end());
// for (auto i : G) cout << i << " ";
// cout << "\n";
//}
//Hua test,u;
/*
9283948394928394839492839483949283948394928394839492839483949283948394928394839492839483949283948394
9873891422320544331922029336792374580166405766994397011913159302733352763096696940422513578096985023626738394257885667058150698272285704458851411427634590205421939993950040895787567074239311084480682
96714869858997351725495629394248263363618914911942657873384325715614588655934673194581471849793421641963178758378863691636983135886373478255239966885113334449497799383663381154421599163541439146244428632157849925133157564168911484279795811517117647411864789988959526748922312947927571257951996744637413751656424364264267
*/
bool weak(string x){
if(x.length()<=7) return 0;
for (int i=2;i<=1000000;i++){
int S = 0;
for (auto j : x) S=(S*10+j-'0')%i;
if(S==0) return 1;
}
return 0;
}
string get(int n){
string c;
for (int i=1;i<=n;i++){
c+=(i==1? rd()%9+1:rd()%10)+'0';
}
return c;
}
void suo(int n){
string qwq = get(n);
Hua x;
x.duru(qwq);
for (int i=1;i<=2000;i++){
if(weak((x+single(i)).shuchu()));
else if(Miller(x+single(i))){
cout << "\n"<<(x+single(i)).shuchu() << "\n";
return;
}
else cout << "k";
}
}
signed main()
{
// for (int i=1;i<=4;i++) suo(512);
Hua a,b,p;
p.duru(n);
cout << "0 表示加密,1 表示解密:";
int qwq;
cin >> qwq;
if(qwq==0){
cout << "输入想加密的东西:";
read(a);
b.duru(gong);
cout<< fpm(a,b,p).shuchu() << "\n";
}
else{
cout << "请输入文本:";
read(a);
cout << "输入私钥:";
read(b);
cout<< fpm(a,b,p).shuchu() << "\n";
}
// Hua Y = fpm(a,p-single(2),p);
// cout << Y.shuchu() << "\n";
// cout << ((a*Y)%p).shuchu() << "\n";
// cout << Miller(x);
// string f;
// while(cin>>f){
// Hua x;
// x.duru(f);
// cout << (Miller(x)? "Y":"N") << "\n";
// }
// test.duru(a);
// u.duru(b);
// cout << (test+u).shuchu() << "\n";
// if(test<u){
// cout << "-";
// cout << (u-test).shuchu() << "\n";
// }
// else cout << (test-u).shuchu() << "\n";
// cout << (test*u).shuchu() << "\n";
// cout << (test/u).shuchu() << "\n";
// cout << (test%u).shuchu() << "\n";
return 0;
}
还有
import sys
def exgcd(a, b):
if b == 0:
return a, 1, 0
g, x1, y1 = exgcd(b, a % b)
return g, y1, x1 - (a // b) * y1
def main():
data = sys.stdin.read().strip().split()
if not data:
return
a = int(data[0])
b = int(data[1]) if len(data) > 1 else 0
g, x, _ = exgcd(b, a)
if g != 1:
print(-1)
else:
print(x % a)
if __name__ == "__main__":
main()
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
59483311928963302126036436429486737336205953358914458013147869983317289302285990122550916908773277273336660075355992008021979934219689886799172509846559212066325091944064652278835484948089600891041936248655767568215231187111290070125363169972385233197164976687928468157159128164878985044055448309102325228127051963031796050080392954237797192488688300420799722256945666040900498378619638343504966226316659316941409218900081265391562672310374635017358302664368041481415254103842650939598545286219648458679750917593
kkkkkkkkkkkkkkkkkk
50635971825704629264769242696633635959048826681602129636813791782630896994229725942403007204882172448443787223673168817777434780426660283052077285932952575767659770957603897081822484959001450635945600519345092280300477370025008350065765759576358653917906889319794297520158484534642343124444308106458106853190247696578929845901627607454543999335265168518751682149758526053694256427047493583208509304987900982573513398574777088066218766192868428026380275872158573171390762537085060800647402733276176769982765978321
kkkkkkkkkkkkkkkk
10968024032309164941513544064912085903534648710926754246239593096093271758469145713731514891683605714482172417652501669362391589059132906555038988098170736288793339663551401587191439568720206111817691448120695922196587859395706333273609033144310789643890859772417105244945795908140980248043382538974650630953584920558934079582189584030492170898082828621682542320196115341678794652157224327365431325442189510260270564862381648110376272044618347804200259746472411904747099727167954638340804640871933837849210064371
kkkkkkkkkkkkkkk
51688952561351981317475777799609616312614986179192801670127753887622346950734081598622566029682311096879403787306999888281050576090189199257635151606568922523133568698332584436085795684964043713496561240919396777259926209881257691726678158657035041745696041524128352924434424331224076437372270031228840215951741214872074246207027092819143574069453466014665708166055499036959775213414979247447328646026072240263935532941526777339559261755458337652677039820286046855566819668119155875088331827008066422812322890739
--------------------------------
Process exited after 1120 seconds with return value 0
请按任意键继续. . .
43882290365354291101912663150985626441960236165785120085611754858152927235088941364577648777924656635222485825337554737454394070566518873045080657875343914648304895363925714622720435485901910187032585303613018666566232175604187773663413783026571062599455317748339964917401998522115347610830870025242373664521458521850438206940358278776789874573363393583837711746842905546568562854726302159663489271505787041977166803757279837284264922449333866557655338031990578316864748604041535961757976195298989460929226491845577687280632371986745420211589176203175494972223443803676917787760285762146765105423422422760871060746956009703155437459268342639515924942054800605914060717827483182932133681935403897740684087821286330031995714204563677903800582600639829480966102980182260009869047099284405992799079418099885660370633734800394582618815147244443197199838687050496920784973448010273024972389269928545118762023211575295116079373591478777943900999825597861456717758797385065833542636545546043057880797917709950567106056679241056358