2011-11-02 4 views
3

파이어 폭스를 사용하여 한 번에 2 글자의 단어를 RSA 암호화하려고합니다.하지만 어떻게해야할지 모르겠습니다.RSA 암호화 (파이썬 사용)

예를 들어 암호화 지수가 8이고 모듈러스가 37329이고 단어가 '파운드'인 경우 어떻게 처리합니까? 나는 pow (ord ('P'))로 시작할 필요가 있고 그 단어가 5 자임을 고려할 필요가 있다는 것을 알고 있으며 나는 한 번에 2자를 공백으로 채워야 할 필요가있다. ? 또한

당신이

+0

기존 라이브러리를 사용하는 대신 수작업으로 수행하는 이유를 지정하십시오. – jfs

+0

@ J.F.Sebastian 왜냐하면 나는 파이썬 언어를 순수하게 암호화에 사용하는 것을 배우지 않고 있기 때문에 암호화에만 초점을 맞추기 때문입니다. 내가 성취하고자하는 기존 도서관은 어디에 있습니까? – Smush

+0

일반적으로 암호화 라이브러리를 작성하지 않은 경우 즉, 암호화를 수행하는 앱을 작성하는 경우 NIH 증후군과 싸우고 기존 라이브러리를 암호화와 같이 민감한 영역에서 사용해야합니다. 다음은 암호화와 관련된 일부 Python 모듈의 간단한 비교입니다. [python-crypto.pdf (2009)] (http://webcache.googleusercontent.com/search?q=cache:BfEDT74QTawJ:mikeivanov.com/pc/python-crypto .pdf) – jfs

답변

3

여기에 기본 예제 감사 < < 8 곳 사용해야 :

>>> msg = 2495247524 
>>> code = pow(msg, 65537, 5551201688147)    # encrypt 
>>> code 
4548920924688L 

>>> plaintext = pow(code, 109182490673, 5551201688147) # decrypt 
>>> plaintext 
2495247524 

는 RSA 스타일 공개 키 암호화의 수학 부분 작업을위한 더 많은 도구에 대한 ASPN cookbook recipe를 참조하십시오.

문자가 블록으로 압축되고 풀린 방법과 숫자가 인코딩되는 방법에 대한 세부 정보는 약간의 비밀입니다. 여기에 완전한 RSA module in pure Python이 있습니다.

특정 포장 패턴

(한 번에 두 문자가 공백으로 채워)이 작동합니다 :

>>> plaintext = 'Pound'  
>>> plaintext += ' '      # this will get thrown away for even lengths  
>>> for i in range(0, len(plaintext), 2): 
     group = plaintext[i: i+2] 
     plain_number = ord(group[0]) * 256 + ord(group[1]) 
     encrypted = pow(plain_number, 8, 37329) 
     print group, '-->', plain_number, '-->', encrypted 

Po --> 20591 --> 12139 
un --> 30062 --> 2899 
d --> 25632 --> 23784 
+1

고맙습니다.하지만 터미널에서 코드를 작성한 다음 편집기에서 코드를 작성한 다음 실행하면 어떻게됩니까? 흥미로운 점은 그 안에 256이 무엇입니까? – Smush

+1

256을 곱하면 원래 고려한''<< 8'과 같습니다. 2 바이트를 32536 미만의 단일 숫자로 결합하는 방법입니다. 위의 스 니펫은 터미널에서 수행되며 편집기로 작성된 스크립트에서는 수행되지 않습니다. –

+0

나는 실제적으로 제곱을 사용하고 지수 연산에 알고리즘을 곱합니다. http://en.wikipedia.org/wiki/Exponentiation_by_squaring – Nishant

1

을 효율적으로 파이썬을 사용하여 RSA 암호화를 코딩하려면, 내 GitHub의 저장소는 것이 확실히에 파이썬에서 RSA의 수학적 정의를 이해하고 해석

Cryptogrphic Algoritms Implementation Using Python

RSA 키 생성

def keyGen(): ''' Generate Keypair ''' i_p=randint(0,20) i_q=randint(0,20) # Instead of Asking the user for the prime Number which in case is not feasible, # generate two numbers which is much highly secure as it chooses higher primes while i_p==i_q: continue primes=PrimeGen(100) p=primes[i_p] q=primes[i_q] #computing n=p*q as a part of the RSA Algorithm n=p*q #Computing lamda(n), the Carmichael's totient Function. # In this case, the totient function is the LCM(lamda(p),lamda(q))=lamda(p-1,q-1) # On the Contrary We can also apply the Euler's totient's Function phi(n) # which sometimes may result larger than expected lamda_n=int(lcm(p-1,q-1)) e=randint(1,lamda_n) #checking the Following : whether e and lamda(n) are co-prime while math.gcd(e,lamda_n)!=1: e=randint(1,lamda_n) #Determine the modular Multiplicative Inverse d=modinv(e,lamda_n) #return the Key Pairs # Public Key pair : (e,n), private key pair:(d,n) return ((e,n),(d,n))