Posted By

yarvin on 10/08/08


Tagged

simple easy Encryption


Versions (?)

Letter back encryption


 / Published in: Python
 

simple encryption program written just for fun. Usage: $ python letterback.py -e "alvin" zkuhm $ python letterback.py -d "zkuhm" alvin Or in the python interpreter. In [1]: import letterback In [2]: lb = letterback.LetterBack() In [3]: lb.encrypt("alvin") zkuhm In [4]: lb.decrypt("zkuhm") alvin

  1. import sys
  2.  
  3. alphabet = 'abcdefghijklmnopqrstuvwxyz'
  4.  
  5. class LetterBack:
  6. '''Class for Encrypting a string of letters'''
  7.  
  8. def __init__(self, text='mnm'):
  9. self.text = text
  10.  
  11. def encrypt(self, aSentance):
  12. '''Encrypts a given string by replacing each letter
  13. with the letter before it in the alphabet.'''
  14. output = ''
  15. for element in aSentance:
  16. for i in range(26):
  17. if element == alphabet[i]:
  18. output += alphabet[i-1]
  19. print output
  20.  
  21. def decrypt(self, aSentance):
  22. '''Decrypts a given string encoded by the encrypt
  23. function.'''
  24. output = ''
  25. for element in aSentance:
  26. #print element + ' this is 1'
  27. for i in range(26):
  28. if element == alphabet[i-1]:
  29. output += alphabet[i]
  30. print output
  31.  
  32. def main():
  33. lb = LetterBack()
  34. if sys.argv[1] == '-e':
  35. lb.encrypt(sys.argv[2])
  36. elif sys.argv[1] == '-d':
  37. lb.decrypt(sys.argv[2])
  38.  
  39. if __name__ == '__main__':
  40. main()

Report this snippet  

You need to login to post a comment.