我想做一个密码,最后用RSA加密。这是我到目前为止的情况。在
key1 = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVTHc4bPSCRefmW+X4+Rsg11I/JwS1ukQdwKpPu62Nm6H/tB1cFT4q0Efs/AlIIC7IdqzgKjsawnl4C/5v19icQ7QIcXToqwh2Wz9hyrRZqurjaFAYY1HYsk0eCBldQu26OEhqZUKLX6txhiKxx0zYJ64xFkpDbOsX4vYa1uyKowIDAQAB-----END PUBLIC KEY-----"
key2 = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIN2AAg0uDw5UP1w1O4V5vBl43L0eH+n5ZJPZlglaq4Clxxb4MPj1996v9P96MgWwktDI4mByJ1FPdhbigRalx79NgASsE4hkzacHdjNwxFaUo4tGFQZZdkRGUpzs4QdsNwazCZg8HJrRnlHnwd2q8yj/sklp0N2OaqgCY0pGrhQIDAQAB-----END PUBLIC KEY-----"
key3 = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdBKY+qU/TgxZ7Lh1RuRvpcq80NJhONVutSammvvW0zcwHl0tSMmbBwEOC5sOZX25gYEH0alTDrnTFxSDXntDJAq5UU00SZ12RA2+mIxnWW2YPVapWYGtwGLK9bY2jDsAo368PdsUa6xB4U1jLqft59l36ZJEtllaeImWh7oVt+wIDAQAB-----END PUBLIC KEY-----"
key4 = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCDfdeIATXyHi5DUwFL0qi7IApbrocq9eEEAn6KsdIJAMo429lM3k0K94/TtCE5238OEGa9UUYmsm8Opja1qGW6qwqLyw+HEfRcBGE1Py2YSTIeJOitiQScjMfS+9tq6kgoUdySu8MCFup7Umf2y/yYghVBWjOfBH2mGmp6cCyUwIDAQAB-----END PUBLIC KEY-----"
key5 = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCfOyGZU//BMWbHODlUbVgnlcjnGlun9VyAdPSo6AzIcGOxWeiIXZMNsT7HT3Ah+Y7+Q+bKLMahwJhjKkzsTO4pRGPX/OHl6F7c27YFPbUVUypZd9aJUQI5EpQXu+3UHYNR7VQ1tgSh1O8UYTG6wdXZuwDJypPZ/iHPOdN2nRSqKwIDAQAB-----END PUBLIC KEY-----"
import os
import time
import random
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
codedsentince = ""
count = input("how many WORDS? ")
if count == int(count):
for number in range(0, count):
codedword = ""
letters = 0
word = raw_input("enter a word? ")
def fib(number):
if number == 1:
return 1
elif number == 2:
return 1
else:
return fib(number - 1) + fib(number - 2)
for letters in range(0, len(word)):
letter1 = chr(ord(word[letters]) +fib(letters + 1))
codedword = codedword + letter1
for letters in range(0, len(word)):
letter1 = chr(ord(word[letters]) +fib(letters + 1))
codedword = codedword + letter1
for letters in range(0, len(word)):
letter1 = chr(ord(word[letters]) +fib(letters + 1))
codedword = codedword + letter1
codedword = " " + codedword
codedsentince = codedsentince + codedword
print codedsentince
keynumber = [ 1, 2, 3, 4, 5]
firstkeynumber = random.choice(keynumber)
if firstkeynumber == 1:
firstkeynumber = '1'
key = key1
elif firstkeynumber == 2:
firstkeynumber = '2'
key = key2
elif firstkeynumber == 3:
firstkeynumber = "3"
key = key3
elif firstkeynumber == 4:
firstkeynumber = '4'
key = key4
elif firstkeynumber == 5:
firstkeynumber = '5'
key = key5
private_key = RSA.importKey(key)
print(private_key.exportKey())
public_key =private_key.publickey()
with open("rsa.pub", "w") as pub_file:
pub_file.write(public_key.exportKey())
with open("rsa.pvt", "w") as pvt_file:
pvt_file.write(private_key.exportKey())
with open('rsa.pub', 'r') as pub_file:
pub_key = RSA.importKey(pub_file.read())
encrypted = pub_key.encrypt(codedsentince, None)
print(encrypted)
print("there will be a txt file in the same directory for you to read the encrypted string of words. This file will made after pressing enter. Also it will be deleted 1 min after pressing enter")
raw_input()
text_file = open("encryptedfile.txt", "w")
text_file.write(codedsentince)
text_file.close()
for i in xrange(60,0,-1):
time.sleep(1)
print i
os.remove('encryptedfile.txt')
我的错误出现在第57行,错误信息如下:
^{pr2}$但是,当我运行RSA部分时,我没有得到一个错误。RSA部分如下:
import Crypto
from Crypto.PublicKey import RSA
#Quick way to generate a new key
private_key = RSA.generate(1024)
#Show the real content of the private part to console, be careful with this!
print(private_key.exportKey())
#Get the public part
public_key = private_key.publickey()
with open("rsa.pub", "w") as pub_file:
pub_file.write(public_key.exportKey())
with open("rsa.pvt", "w") as pvt_file:
pvt_file.write(private_key.exportKey())
#Load public key back from file and we only need public key for encryption
with open('rsa.pub', 'r') as pub_file:
pub_key = RSA.importKey(pub_file.read())
#Encrypt something with public key and print to console
encrypted = pub_key.encrypt('hello world', None) # the second param None here is useless
print(encrypted)
我使用的是安装在Windows7上的Python2.7.3和Crypto2.6.1。在
目前没有回答
相关问题 更多 >
编程相关推荐