Advertisement
Hellerick_Ferlibay

Simple block-code encryption algorithm by Hellerick

Jun 1st, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. # Simple block-code encryption algorithm by Hellerick, 2013-06-01
  2. # http://codepad.org/SulkRkYi
  3.  
  4. from random import randint
  5.  
  6. code="""z 5 6 7 8 9
  7. 0 a b c d e
  8. 1 f g h i j
  9. 2 k l m n o
  10. 3 p q r s t
  11. 4 u v w x y"""
  12.  
  13. code=code.split('\n')
  14. c2l=[]
  15. for i in code: c2l+=[i.split(' ')]
  16. l2c={}
  17. for i in range(len(c2l)):
  18.     for j in range(len(c2l[i])):
  19.         l2c[c2l[i][j]]=[i,j]
  20.  
  21. def encrypt(text):
  22.     output=cp=c2l[randint(0,len(c2l)-1)][randint(0,len(c2l[0])-1)]
  23.     text=text.lower()
  24.     for i in range(len(text)):
  25.         if text[i] in l2c:
  26.             di,dj=(l2c[text[i]][0]-l2c[cp][0])%6,(l2c[text[i]][1]-l2c[cp][1])%6
  27.             cp=c2l[di][dj]
  28.             output+=cp
  29.         else:
  30.             output=output+text[i]
  31.     return output
  32.  
  33. def decrypt(text):
  34.     cp=text[0]
  35.     text=text[1:]
  36.     output=''
  37.     for i in range(len(text)):
  38.         if text[i] in l2c:
  39.             di,dj=(l2c[text[i]][0]+l2c[cp][0])%6,(l2c[text[i]][1]+l2c[cp][1])%6
  40.             cp=text[i]
  41.             output+=c2l[di][dj]
  42.         else:
  43.             output=output+text[i]
  44.     return output
  45.  
  46. thetext="The worst thing about my sister is she's such a GIRL. Well, I'm a girl too, but I'm not a dinky-pinky, silly-frilly girlie girl. Think cupcakes and cuddly teddies and charm bracelets -- that's Melissa. She leaves a little pink trail around the house -- sparkly slides and ribbons and notebooks. You breathe in her revolting scent long after she's gone off to hang out at her friends' houses. She's not allowed to wear real perfume yet, but she's got this rose hand cream that smells really strongly. She doesn't just rub it on her hands, she smoothes it in all over, so she's always slightly slippery. Her lips shine too, because she's forever smearing on lip gloss. She's not really supposed to wear make-up yet either, only for play, but she's got a big plastic bag patterned with pink kittens, and it's crammed full of eye shadows and mascara and blisher. It used to be just Mum's old stuff, but now Melissa spends half her pocket money in Superdrug."
  47.  
  48. print ' '+thetext
  49. print '\n'
  50. print encrypt(thetext)
  51. print '\n'
  52. print ' '+decrypt(encrypt(thetext))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement