RSA加密算法 - Hoblovski's Blog - 想拿Ag的蒟蒻.已经Ag滚出.
自动命名Python脚本
快速排序的杀手级对手

RSA加密算法

Hoblovski posted @ 2014年12月14日 03:54 in Discussion with tags RSA , 1209 阅读

么么哒回来了.

好吧写下来备忘.

14161583845007633656281910459539904966616101222349706321949852145422139301350711628504380073326615798189286256522069754794761269288220960807342757913832797925312124192528953357134256232622820640098952878638584224677891557598635873163023435090628877344580821373283400119431504504524875066595710555522505698968112770849748886830869002723559462681261840139811494423950696497874436577911879293550663164613
16752143357861209799707574402060505557957355677627645918596266230525005633964044262830799120627843956205875344989741353279550574766978085622947413892966285618872992054877391136502251586549142198350451124372015772092838875513129472526781632016586695265802933807812342680996585339604489882852275155140284549551699313419191779527804998043254644282366735658265957919068647153132078952536404640282660143091
237236882745939242556073673180003407058986393628673550419640983345586366738283276546180439970526289406207914079155866959032472963202645919272614674132648993992488777086628134258994797958731785927724903374631995672583438349035068119951843359260836605658731156098590193568294749654315994382532316905312824593814214200711900582511759902051909820727746417601461912028695117822338065874323503389286713550422327411931055135711382245381201568681594165348470575216248644208805498812438460486930579530643783872041037038263258143901653914062844192874608245005046184466402838606150714143014468930891198082961867276646242210673254345496433044882269689194421901214720810655741480258363517062514559401002911862456624377816564829255212163839486631112610495529039568199449980698279131120162493255247447887453467638783
237236882745939242556073673180003407058986393628673550419640983345586366738283276546180439970526289406207914079155866959032472963202645919272614674132648993992488777086628134258994797958731785927724903374631995672583438349035068119951843359260836605658731156098590193568294749654315994382532316905312824593814214200711900582511759902051909820727746417601461912028695117822338065874323503389286713550391413684728186292255392760519601158157020708448493222975702525832858353877123704595595400336689324117645875436751447035827342070007645146444318073198247100922217722358744369649377961111719235244512463273635642213902523912384667699192464622087206328604337055474645737457935427218385194451554926151793834129296752744986271497480812630345796388565410992401372528355259787469155977724799163953620144331080
1234423529
4551627442088810168637738205353510587210124604181062412669534780178065280024521419684293797715901173230313793625479481582281396543579034966627421069568526301573103604471275167849984268778233842286491033810578952604335661043155179671159552914750048204986236228269304103384701971712570915606587287329211945157083760996047092008002700618898702295724106881039624299970825664080784384383375978258909581460082697345080955164727186206168250644377500335793001451941836773794153150313940959980346251136819607079002858771047225270779278461373831278876581643038241655630159757587416471287626677130076480384718375169621866974490529307725851285181321527037686938053408666761778763764024749601957859090801712529339267850120042583093329801659945915634853129437037607227708380223716175838737094673341157423488800129
p, q, pq, phi(pq)
import random;

'''
    Maximum transferring length at a time : 500 lowercase letters and `.
'''

f = open('RSA.txt','r');
p = int(f.readline());
q = int(f.readline());
N = int(f.readline());
r = int(f.readline());
e = int(f.readline());
d = int(f.readline());

def exp(a,n,mo):
    ans = 1; t = a % mo;
    while n > 0:
        if n & 1 == 1: ans = ans * t % mo;
        t = t * t % mo;
        n >>= 1;
    return ans;

def witness(n,a):
    u,t = n-1,0;
    while u & 1 == 0:
        u >>= 1; t += 1;
    x = exp(a,u,n);
    for i in range(t):
        y = x * x % n;
        if (y==1)and(x<>1)and(x<>n-1): return True;
        x = y;
    return (x<>1);

def Miller_Rabin(n):
    if n < 2: return False;
    s = 128;
    for i in range(s):
        a = random.randint(1,n-1);
        if witness(n,a): return False;
    return True;

def isprime(n):
    if n < 2: return False;
    for i in range(2,int(n ** 0.5)+1):
        if n % i == 0: return False;
    return True;

def findprime(L,R):
    n = random.randint(L,R);
    while not Miller_Rabin(n):
        n = random.randint(L,R);
    return n;

def exgcd(i,j):
    if j == 0: return (i,1,0);
    k = exgcd(j,i % j);
    return (k[0],k[2],k[1]-(i//j)*k[2]);

def inv(a,n):
    b = exgcd(a,n);
    return b[1];

def intencrypt(n):
    return exp(n,e,N);

def intdecrypt(n):
    return exp(n,d,N);

def strencrypt(S):
    s = 0; t = 1;
    for i in S:
        s += (ord(i)-96)*t; t *= 27;
    return intencrypt(s);

def strdecrypt(s):
    s = intdecrypt(s); S = '';
    while s > 0:
        S = S + chr((s % 27)+96);
        s //= 27;
    return S;

def se(S):
    return strencrypt(S);

def sd(s):
    return strdecrypt(s);
就是有点慢,还有只支持小写字母和`,还有不能用N,p,q,r等等变量名...
一个晚上的产物...
Avatar_small
UP 10th Blueprint 20 说:
2021年10月13日 15:32

Uttar Pradesh Madhyamik Shiksha Parishad (UPMSP) has Recently Upload the UP Board Class 10th Question model paper 2022 Pdf Format model papers for Final Exam 2022. We are Providing here <a href="https://10thmodelquestionpaper.in/up-10th-blueprint-upmsp-10th-exam-pattern/">UP 10th Blueprint 2022</a> the Sample Paper of All Subjects in PDF format. These UPMSP 10th model paper 2022 Represent the Design of Solved model paper Including the type of Questions and Distribution of marks across Different Sample Paper which will be followed in the Upcoming UPMSP Final Exam.

Avatar_small
UP 10th Blueprint 20 说:
2021年10月13日 15:33

Uttar Pradesh Madhyamik Shiksha Parishad (UPMSP) has Recently Upload the UP Board Class 10th Question model paper 2022 Pdf Format model papers for Final Exam 2022. We are Providing here UP 10th Blueprint 2022 the Sample Paper of All Subjects in PDF format. These UPMSP 10th model paper 2022 Represent the Design of Solved model paper Including the type of Questions and Distribution of marks across Different Sample Paper which will be followed in the Upcoming UPMSP Final Exam.

Avatar_small
Bushra 说:
2022年12月29日 21:29

very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! Anupama

Avatar_small
Alyssa 说:
2023年1月07日 21:25

RSA is an algorithm used for public-key cryptography. It is one of the first practical public-key cryptosystems and is widely used in electronic commerce diamond rings protocols. RSA is based on the difficulty of factorizing large integers, a problem for which there is no known efficient general solution.

Avatar_small
Kaevu 说:
2023年3月06日 19:15

The post was well-written, providing an in-depth look at the subject matter. brand identity design agencies

Avatar_small
10thmodelquestionsp 说:
2023年5月23日 17:53

professional writers'collective effort to produce specialised news coverage of the10thmodelquestionspapers.in most recent events in the nation (India). Our team is made up of professional writers and citizen journalists with a wide range of journalism interests who are enthusiastic about disseminating the education updates transparently and in the benefit of the general public.For all age groups,our reporting team plans to publish the Education & Recruitment Update and provide inside coverage

Avatar_small
dpost.in 说:
2023年6月28日 17:30

dpost is a initiative of professional writers who have come together for dedicated news coverage of latest happenings around the country (India). Our team comprises of professional writers & citizen journalists dpost.in with diverse range of interest in Journalism who are passionate about publishing the Education Updates with transparency in general public interest.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter