Hellerick_Ferlibay

Encryption-decryption algorithm by Hellerick

Mar 1st, 2013
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. # Encryption-decryption algorithm by Hellerick, 2012-05-28
  2.  
  3. from random import randint
  4.  
  5. minascii=ord(' ')
  6. maxascii=ord('~')
  7. asciirange=maxascii-minascii+1
  8.  
  9. def shuffle(w,p,enc_dec):
  10.     result = ''
  11.     for i in range(len(w)):
  12.         for j in range(len(p)):
  13.             for k in range(len(p)):
  14.                 p[j]=(p[j]+p[j]%(p[k]+1))%asciirange
  15.         char=ord(w[i])
  16.         for j in range(len(p)): char+=p[j]*(j%4*2+1)*enc_dec
  17.         result+=chr((char-minascii)%asciirange+minascii)
  18.     return result
  19.  
  20. def encrypt(w,password='Rule, Britannia!'):
  21.     p, addcode = [], ''
  22.     for i in range(4):
  23.         p+=[randint(minascii,maxascii)]
  24.         addcode+=chr(p[i])
  25.     for i in range(len(password)): p+=[ord(password[i])]
  26.     return addcode+shuffle(w,p,+1)
  27.  
  28. def decrypt(w,password='Rule, Britannia!'):
  29.     p = []
  30.     for i in range(4): p+=[ord(w[i])]
  31.     for i in range(len(password)): p+=[ord(password[i])]
  32.     return shuffle(w[4:],p,-1)
  33.  
  34. text='Lingvoforum~'
  35. enctext=encrypt(text,'Password')
  36. dectext=decrypt(enctext,'Password')
  37. incdectext=decrypt(enctext,'Passwort')
  38. print 'Original text:                  "'+text+'"'
  39. print 'Encrypted:                      "'+enctext+'"'
  40. print 'Decrypted (correct password):   "'+dectext+'"'
  41. print 'Decrypted (incorrect password): "'+incdectext+'"'
Advertisement
Add Comment
Please, Sign In to add comment