ccmny

Szyfr Cezara 2

Jun 23rd, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. def cipher(string, shift):
  2.     output = ""
  3.     for letter in string:
  4.         if ord(letter) >= ord('a') and ord(letter) <= ord('z'):
  5.             code = ((ord(letter) - ord('a'))+shift)%(ord('z')-ord('a')+1) + ord('a')   
  6.             output += chr(code)
  7.         elif ord(letter) >= ord('A') and ord(letter) <= ord('Z'):
  8.             code = ((ord(letter) - ord('A'))+shift)%(ord('Z')-ord('A')+1) + ord('A')   
  9.             output += chr(code)
  10.         else:
  11.             output += letter
  12.     return output;
  13.  
  14. def decipher(string, shift):
  15.     return cipher(string, -shift)
  16.    
  17. def main():
  18.     string = "Ala ma kota, sierotka ma ryzsia"
  19.     shift = 3
  20.     print "Tekst do zaszyfrowania: " + string
  21.     string = cipher(string, shift)
  22.     print "Tekst zaszyfrowany: " + string
  23.     string = decipher(string, shift)
  24.     print "Tekst odszyfrowany: " + string
  25.    
  26. if __name__ == "__main__":
  27.     main()
Advertisement
Add Comment
Please, Sign In to add comment