Skip to content
Browse files

RSA cipher changes.

  • Loading branch information...
1 parent 2ae2da1 commit 12c6589b1b748b362ec2353f71b86c8baa574111 @asweigart committed Feb 23, 2013
Showing with 21 additions and 21 deletions.
  1. +21 −21 rsaCipher.py
View
42 rsaCipher.py
@@ -6,28 +6,28 @@
# IMPORTANT: The block size MUST be less or equal than the key size!
# (Note: The block size is in bytes, the key size is in bits. There
# are 8 bits in 1 byte.)
-DEFAULT_BLOCK_SIZE = 128
+DEFAULT_BLOCK_SIZE = 128 # 128 bytes
BYTE_SIZE = 256 # One byte has 256 different values.
def main():
# Runs a test that encrypts a message to a file or decrypts a message
# from a file.
- filename = 'encrypted_file.txt'
+ filename = 'encrypted_file.txt' # the file to write to/read from
mode = 'encrypt' # set to 'encrypt' or 'decrypt'
if mode == 'encrypt':
message = '''"Journalists belong in the gutter because that is where the ruling classes throw their guilty secrets." -Gerald Priestland "The Founding Fathers gave the free press the protection it must have to bare the secrets of government and inform the people." -Hugo Black'''
- privKeyFilename = 'al_sweigart_privkey.txt'
+ pubKeyFilename = 'al_sweigart_pubkey.txt'
print('Encrypting and writing to %s...' % (filename))
- encryptedText = encryptAndWriteToFile(filename, privKeyFilename, message, 128)
+ encryptedText = encryptAndWriteToFile(filename, pubKeyFilename, message)
print('Encrypted text:')
print(encryptedText)
elif mode == 'decrypt':
- pubKeyFilename = 'al_sweigart_pubkey.txt'
+ privKeyFilename = 'al_sweigart_privkey.txt'
print('Reading from %s and decrypting...' % (filename))
- decryptedText = readFromFileAndDecrypt(filename, pubKeyFilename)
+ decryptedText = readFromFileAndDecrypt(filename, privKeyFilename)
print('Decrypted text:')
print(decryptedText)
@@ -38,12 +38,13 @@ def getBlocksFromText(message, blockSize=DEFAULT_BLOCK_SIZE):
# represents 512 (or whatever blockSize is set to) string characters.
messageBytes = message.encode('ascii') # convert the string to bytes
+
blockInts = []
for blockStart in range(0, len(messageBytes), blockSize):
# Calculate the block integer for this block of text
blockInt = 0
for i in range(blockStart, min(blockStart + blockSize, len(messageBytes))):
- blockInt += int(messageBytes[i]) * (BYTE_SIZE ** (i % blockSize))
+ blockInt += messageBytes[i] * (BYTE_SIZE ** (i % blockSize))
blockInts.append(blockInt)
return blockInts
@@ -53,16 +54,15 @@ def getTextFromBlocks(blockInts, messageLength, blockSize=DEFAULT_BLOCK_SIZE):
# The original message length is needed to properly convert the last
# block integer.
message = []
-
for blockInt in blockInts:
blockMessage = []
- for i in range(blockSize-1, -1, -1):
+ for i in range(blockSize - 1, -1, -1):
if len(message) + i < messageLength:
# Decode the message string for the 512 (or whatever
# blockSize is set to) characters from this block integer.
- charNumber = blockInt // (BYTE_SIZE ** i)
+ asciiNumber = blockInt // (BYTE_SIZE ** i)
blockInt = blockInt % (BYTE_SIZE ** i)
- blockMessage.insert(0, bytes([charNumber]).decode('ascii'))
+ blockMessage.insert(0, chr(asciiNumber))
message.extend(blockMessage)
return ''.join(message)
@@ -104,35 +104,35 @@ def readKeyFile(keyFilename):
def encryptAndWriteToFile(messageFilename, keyFilename, message, blockSize=DEFAULT_BLOCK_SIZE):
# Using a key from a key file, encrypt the message and save it to a
# file. Returns the encrypted message string.
- keySize, N, EorD = readKeyFile(keyFilename)
+ keySize, N, E = readKeyFile(keyFilename)
# Check that key size is greater than block size.
if keySize < blockSize * 8: # * 8 to convert bytes to bits
- sys.exit('ERROR: Block size is %s and key size is %s. The RSA cipher requires the block size to be less than the key size. Either decrease the block size or use different keys.' % (blockSize, keySize))
- key = (N, EorD)
+ sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Either decrease the block size or use different keys.' % (blockSize * 8, keySize))
+
# Encrypt the message
- encryptedBlocks = encryptMessage(message, key, blockSize)
+ encryptedBlocks = encryptMessage(message, (N, E), blockSize)
# Convert the large int values to one string value.
for i in range(len(encryptedBlocks)):
encryptedBlocks[i] = str(encryptedBlocks[i])
encryptedContent = ','.join(encryptedBlocks)
# Write out the encrypted string to the output file.
+ encryptedContent = '%s_%s_%s' % (len(message), blockSize, encryptedContent)
fp = open(messageFilename, 'w')
- fp.write('%s_%s_%s' % (len(message), blockSize, encryptedContent))
+ fp.write(encryptedContent)
fp.close()
-
# Also return the encrypted string.
return encryptedContent
def readFromFileAndDecrypt(messageFilename, keyFilename):
# Using a key from a key file, read an encrypted message from a file
# and then decrypt it. Returns the decrypted message string.
- keySize, N, EorD = readKeyFile(keyFilename)
- key = (N, EorD)
+ keySize, N, D = readKeyFile(keyFilename)
+
# Read in the message length and the encrypted message from the file.
fp = open(messageFilename)
@@ -143,15 +143,15 @@ def readFromFileAndDecrypt(messageFilename, keyFilename):
# Check that key size is greater than block size.
if keySize < blockSize * 8: # * 8 to convert bytes to bits
- sys.exit('ERROR: Block size is %s and key size is %s. The RSA cipher requires the block size to be less than the key size. Did you specify the correct key file and encrypted file?' % (blockSize, keySize))
+ sys.exit('ERROR: Block size is %s bits and key size is %s bits. The RSA cipher requires the block size to be equal to or greater than the key size. Did you specify the correct key file and encrypted file?' % (blockSize * 8, keySize))
# Convert the encrypted message into large int values.
encryptedBlocks = []
for block in message.split(','):
encryptedBlocks.append(int(block))
# Decrypt the large int values.
- return decryptMessage(encryptedBlocks, messageLength, key, blockSize)
+ return decryptMessage(encryptedBlocks, messageLength, (N, D), blockSize)
# If rsaCipher.py is run (instead of imported as a module) call

0 comments on commit 12c6589

Please sign in to comment.
Something went wrong with that request. Please try again.