fork download
  1. from Crypto.Cipher import AES
  2. from Crypto.Random import get_random_bytes
  3. from binascii import unhexlify
  4.  
  5.  
  6.  
  7.  
  8. __all__ = [ 'ValueError', 'pad', 'unpad' ]
  9.  
  10. from Crypto.Util.py3compat import *
  11.  
  12.  
  13. def pad(data_to_pad, block_size, style='pkcs7'):
  14. """Apply standard padding.
  15. :Parameters:
  16. data_to_pad : byte string
  17. The data that needs to be padded.
  18. block_size : integer
  19. The block boundary to use for padding. The output length is guaranteed
  20. to be a multiple of ``block_size``.
  21. style : string
  22. Padding algorithm. It can be *'pkcs7'* (default), *'iso7816'* or *'x923'*.
  23. :Return:
  24. The original data with the appropriate padding added at the end.
  25. """
  26.  
  27. padding_len = block_size-len(data_to_pad)%block_size
  28. if style == 'pkcs7':
  29. padding = bchr(padding_len)*padding_len
  30. elif style == 'x923':
  31. padding = bchr(0)*(padding_len-1) + bchr(padding_len)
  32. elif style == 'iso7816':
  33. padding = bchr(128) + bchr(0)*(padding_len-1)
  34. else:
  35. raise ValueError("Unknown padding style")
  36. return data_to_pad + padding
  37.  
  38. def unpad(padded_data, block_size, style='pkcs7'):
  39. """Remove standard padding.
  40. :Parameters:
  41. padded_data : byte string
  42. A piece of data with padding that needs to be stripped.
  43. block_size : integer
  44. The block boundary to use for padding. The input length
  45. must be a multiple of ``block_size``.
  46. style : string
  47. Padding algorithm. It can be *'pkcs7'* (default), *'iso7816'* or *'x923'*.
  48. :Return:
  49. Data without padding.
  50. :Raises ValueError:
  51. if the padding is incorrect.
  52. """
  53.  
  54. pdata_len = len(padded_data)
  55. if pdata_len % block_size:
  56. raise ValueError("Input data is not padded")
  57. if style in ('pkcs7', 'x923'):
  58. padding_len = bord(padded_data[-1])
  59. if padding_len<1 or padding_len>min(block_size, pdata_len):
  60. raise ValueError("Padding is incorrect.")
  61. if style == 'pkcs7':
  62. if padded_data[-padding_len:]!=bchr(padding_len)*padding_len:
  63. raise ValueError("PKCS#7 padding is incorrect.")
  64. else:
  65. if padded_data[-padding_len:-1]!=bchr(0)*(padding_len-1):
  66. raise ValueError("ANSI X.923 padding is incorrect.")
  67. elif style == 'iso7816':
  68. padding_len = pdata_len - padded_data.rfind(bchr(128))
  69. if padding_len<1 or padding_len>min(block_size, pdata_len):
  70. raise ValueError("Padding is incorrect.")
  71. if padding_len>1 and padded_data[1-padding_len:]!=bchr(0)*(padding_len-1):
  72. raise ValueError("ISO 7816-4 padding is incorrect.")
  73. else:
  74. raise ValueError("Unknown padding style")
  75. return padded_data[:-padding_len]
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. def encrypt_data(data,key,iv):
  89. padded = pad(data.encode(),16,style='pkcs7')
  90. cipher = AES.new(key, AES.MODE_CBC,iv)
  91. enc = cipher.encrypt(padded)
  92. return enc
  93.  
  94.  
  95. key = get_random_bytes(16)
  96. iv = get_random_bytes(16)
  97.  
  98. ans = encrypt_data('access_username=admin&password=sUp3rPaSs1', key, iv)
  99. print(ans)
Success #stdin #stdout 0.02s 11272KB
stdin
Standard input is empty
stdout
���Q�֓1���yΪ�
��G�@��@D'�E"���O*��]W�N�