g3x0

[RSTForums.com] Krypton CLI

Mar 8th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.29 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import re
  4. import sys
  5. import base64
  6. import urllib
  7. import hashlib
  8.  
  9. def switch(val, obj):
  10.     return obj.get(val, 'default');
  11.  
  12. def base64_(action, string):
  13.     global encodes
  14.     global decodes
  15.  
  16.     if action in encodes:
  17.         try:
  18.             result = base64.b64encode(string)
  19.         except Exception as e:
  20.             result = 'Cipher error, check the input and the action for the cipher.'
  21.  
  22.     elif action in decodes:
  23.         try:
  24.             result = base64.b64decode(string)
  25.         except Exception as e:
  26.             result = 'Cipher error, check the input and the action for the cipher.'
  27.  
  28.     else:
  29.         return 'Invalid cipher action ('+ action +')'
  30.  
  31.     return result
  32.  
  33. def base32_(action, string):
  34.     global encodes
  35.     global decodes
  36.  
  37.     if action in encodes:
  38.         try:
  39.             result = base64.b32encode(string)
  40.         except Exception as e:
  41.             result = 'Cipher error, check the input and the action for the cipher.'
  42.  
  43.     elif action in decodes:
  44.         try:
  45.             result = base64.b32decode(string)
  46.         except Exception as e:
  47.             result = 'Cipher error, check the input and the action for the cipher.'
  48.  
  49.     else:
  50.         return 'Invalid cipher action ('+ action +')'
  51.  
  52.     return result
  53.  
  54. def base16_(action, string):
  55.     global encodes
  56.     global decodes
  57.  
  58.     if action in encodes:
  59.         try:
  60.             result = base64.b16encode(string)
  61.         except Exception as e:
  62.             result = 'Cipher error, check the input and the action for the cipher.'
  63.  
  64.     elif action in decodes:
  65.         try:
  66.             result = base64.b16decode(string)
  67.         except Exception as e:
  68.             result = 'Cipher error, check the input and the action for the cipher.'
  69.  
  70.     else:
  71.         return 'Invalid cipher action'
  72.  
  73.     return result
  74.  
  75. def binary_(action, string):
  76.     global encodes
  77.     global decodes
  78.  
  79.     if action in encodes:
  80.         try:
  81.             result = ' '.join(format(ord(x), 'b').zfill(8) for x in string)
  82.         except Exception as e:
  83.             result = 'Cipher error, check the input and the action for the cipher.'
  84.  
  85.     elif action in decodes:
  86.         try:
  87.             result = ''.join(chr(int(x, 2)) for x in string.split(' '))
  88.         except Exception as e:
  89.             result = 'Cipher error, check the input and the action for the cipher.'
  90.  
  91.     else:
  92.         return 'Invalid cipher action'
  93.  
  94.     return result
  95.  
  96. def hex_(action, string):
  97.     global encodes
  98.     global decodes
  99.  
  100.     if action in encodes:
  101.         try:
  102.             result = ' '.join(x.encode('hex') for x in string)
  103.         except Exception as e:
  104.             result = 'Cipher error, check the input and the action for the cipher.'
  105.  
  106.     elif action in decodes:
  107.         try:
  108.             result = re.sub(r'[^0-9a-f]', '', string).decode('hex')
  109.         except Exception as e:
  110.             result = 'Cipher error, check the input and the action for the cipher.'
  111.  
  112.     else:
  113.         return 'Invalid cipher action'
  114.  
  115.     return result
  116.  
  117. def ascii_(action, string):
  118.     global encodes
  119.     global decodes
  120.  
  121.     if action in encodes:
  122.         try:
  123.             result = ' '.join(str(ord(x)) for x in string)
  124.         except Exception as e:
  125.             result = 'Cipher error, check the input and the action for the cipher.'
  126.  
  127.     elif action in decodes:
  128.         try:
  129.             result = ''.join(chr(int(x)) for x in string.split(' '))
  130.         except Exception as e:
  131.             result = 'Cipher error, check the input and the action for the cipher.'
  132.  
  133.     else:
  134.         return 'Invalid cipher action ('+ action +')'
  135.  
  136.     return result
  137.  
  138. def rot13_(action, string):
  139.     return string.encode('rot13')
  140.  
  141. def url_(action, string):
  142.     global encodes
  143.     global decodes
  144.  
  145.     if action in encodes:
  146.         try:
  147.             result = urllib.quote(string)
  148.         except Exception as e:
  149.             result = 'Cipher error, check the input and the action for the cipher.'
  150.  
  151.     elif action in decodes:
  152.         try:
  153.             result = urllib.unquote(string)
  154.         except Exception as e:
  155.             result = 'Cipher error, check the input and the action for the cipher.'
  156.  
  157.     else:
  158.         return 'Invalid cipher action ('+ action +')'
  159.  
  160.     return result
  161.  
  162. def crypt_(hasher, string):
  163.     temp = eval('hashlib.'+ hasher +'("""'+ string.replace('"""', '\\"\\"\\"') +'""")');
  164.     return temp.hexdigest()
  165.  
  166. def all_(string, action):
  167.     global encodes
  168.     global decodes
  169.     global hashing
  170.     global ciphers
  171.     global hashes
  172.  
  173.     if action not in decodes:
  174.         for c in ciphers:
  175.             if c not in ['ascii', 'hex', 'url']:
  176.                 print c[0].upper() + c[1:],
  177.             else:
  178.                 print c.upper(),
  179.  
  180.             print '\t: '+ eval(c +'_("""'+ action +'""", """'+ string.replace('"""', '\\"\\"\\"') +'""")')
  181.  
  182.         for c in hashing:
  183.             print c.upper() +' \t: '+ eval('crypt_("""'+ c +'""", """'+ string.replace('"""', '\\"\\"\\"') +'""")')
  184.  
  185.     else:
  186.         for c in ciphers:
  187.             if c not in ['ascii', 'hex', 'url']:
  188.                 print c[0].upper() + c[1:],
  189.             else:
  190.                 print c.upper(),
  191.  
  192.             print '\t: '+ eval(c +'_("""'+ action +'""", """'+ string.replace('"""', '\\"\\"\\"') +'""")')
  193.  
  194. def enc(string, cipher, action):
  195.     global ciphers
  196.  
  197.     if cipher not in ciphers and cipher not in hashing and cipher != 'all':
  198.         print 'Cipher "'+ cipher +'" not available!'
  199.         return None
  200.  
  201.     if cipher == 'all':
  202.         all_(string, action)
  203.     elif cipher in hashing:
  204.         print eval('crypt_("""'+ cipher +'""", """'+ string.replace('"""', '\\"\\"\\"') +'""")')
  205.     else:
  206.         print eval(cipher +'_("""'+ action +'""", """'+ string.replace('"""', '\\"\\"\\"') +'""")')
  207.  
  208. ciphers = ['base64', 'base32', 'base16', 'binary', 'hex', 'ascii', 'rot13', 'url']
  209. hashing = ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']
  210. encodes = ['e', 'en', 'enc', 'encode']
  211. decodes = ['d', 'de', 'dec', 'decode']
  212. hashes  = ['h', 'hash', 'hashes', 'hashing']
  213. cipher_shorts = { 'b64': 'base64', 'b32': 'base32', 'b16': 'base16', 'bin': 'binary', 'r13': 'rot13' }
  214. help_text = '''K R Y P T O N - CLI Encryption Tool
  215. Usage:
  216.  
  217. Available ciphers & hashing algorithms:
  218. '''+ ', '.join(ciphers + hashing) +'''
  219.  
  220. krp <string>
  221. \tThe string encoded using all of the ciphers
  222.  
  223. krp <d|de|dec|decode> <string>
  224. \tThe string decoded using all of the ciphers
  225. \t(could be useful for cipher identification)
  226.  
  227. krp <cipher> [<e|en|enc|encode>] <string>
  228. \tThe string encoded using the specified cipher
  229.  
  230. krp <cipher> <d|de|dec|decode> <string>
  231. \tThe string decoded using the specified cipher
  232.  
  233. The same rules apply for piping, just omit the <string>
  234.  
  235. E.g.:
  236. cat /etc/hosts | krp md5
  237. \tReturns the md5 of the file contents'''
  238.  
  239. if __name__ == '__main__':
  240.     args = sys.argv[1:]
  241.    
  242.     # If there is content being piped as the input string
  243.     if sys.stdin.isatty() == False:
  244.         string = sys.stdin.read()
  245.  
  246.         if len(args) > 1:
  247.             cipher = args[0].lower()
  248.             action = args[1].lower()
  249.         else:
  250.             cipher = 'all'
  251.             action = 'e'
  252.  
  253.             if len(args) > 0:
  254.                 first_arg = args[0].lower()
  255.  
  256.                 if first_arg != 'all' and switch(first_arg, cipher_shorts) != 'default':
  257.                     first_arg = switch(first_arg, cipher_shorts)
  258.                
  259.                 if first_arg in ciphers + hashing:
  260.                     cipher = first_arg
  261.  
  262.     # If the string is passed normally
  263.     else:
  264.         if len(args) < 1 or (len(args) and args[0] == '-h'):
  265.             print help_text
  266.             exit(0)
  267.  
  268.         if len(args) > 2:
  269.             string = args[2]
  270.         else:
  271.             if len(args) > 1:
  272.                 string = args[1]
  273.             else:
  274.                 string = args[0]
  275.    
  276.         if len(args) > 2:
  277.             cipher = args[0].lower()
  278.             action = args[1].lower()
  279.         else:
  280.             cipher = 'all'
  281.             action = 'e'
  282.  
  283.             first_arg = args[0].lower()
  284.  
  285.             if first_arg != 'all' and switch(first_arg, cipher_shorts) != 'default':
  286.                 first_arg = switch(first_arg, cipher_shorts)
  287.            
  288.             if len(args) > 1 and first_arg in ciphers + hashing:
  289.                 cipher = first_arg
  290.  
  291.     if cipher != 'all' and switch(cipher, cipher_shorts) != 'default':
  292.         cipher = switch(cipher, cipher_shorts)
  293.  
  294.     enc(string, cipher, action)
Advertisement
Add Comment
Please, Sign In to add comment