Skip to content

Commit

Permalink
Add RSA support for sending strings. Chops strings into single charac…
Browse files Browse the repository at this point in the history
…ters and encrypts each character, then returns a list of chiffres.
  • Loading branch information
JanEbbing committed Jul 29, 2017
1 parent 5fb6431 commit 762eadf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 14 additions & 1 deletion encryption_algorithms/asymmetric/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ def _random_prime_of_length(k):
while not isPrime(candidate):
candidate = get_odd_random(k)
return candidate


def encrypt_string(public_key, str_to_send):
message = [ord(c) for c in str_to_send]
chiffre = []
for ordc in message:
chiffre.append(RSA.encryption(public_key, ordc))
return chiffre

def decrypt_string(private_key, list_of_chiffres):
cleartext = []
for chiff in list_of_chiffres:
cleartext.append(RSA.decryption(private_key, chiff))
return ''.join(chr(ordc) for ordc in cleartext)

class RSA(object):
"""implements the RSA encryption algorithm. Please do not use this to encrypt anything remotely serious."""
@classmethod
Expand Down
6 changes: 5 additions & 1 deletion encryption_algorithms/test/test_encryption.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from asymmetric.rsa import RSA
from asymmetric.rsa import RSA, encrypt_string, decrypt_string

import random
import os
Expand Down Expand Up @@ -102,6 +102,10 @@ def send_messages(self, scenario):
def test_send_messages_rsa(self):
self.send_messages(self.scenario_rsa)

def test_send_strings_rsa(self):
message = "My super complicated test message.\n I love RSA!"
self.assertEquals(decrypt_string(self.scenario_rsa.sk_bob, encrypt_string(self.scenario_rsa.pk_bob, message)), message, "Error: Decryption of encryption does not match clear text!")

@unittest.skip("not implemented")
def test_send_messages_des(self):
self.send_messages(self.scenario_des)

0 comments on commit 762eadf

Please sign in to comment.