当前位置:   article > 正文

AES MODE_GCM_aes.mode_gcm

aes.mode_gcm

python  需要安装: PyCryptodome

示例代码:
 

  1. from Crypto.Cipher import AES
  2. key="1111111111111111"
  3. iv="000000000000"
  4. msg="2222222222222222"
  5. #加密
  6. cipher = AES.new(key, AES.MODE_GCM, iv)
  7. en_msg=cipher.encrypt(msg)
  8. #print en_msg,
  9. print "en_msg(hex)",en_msg.encode('hex')
  10. #解密
  11. cipher1 = AES.new(key, AES.MODE_GCM, iv)
  12. de_msg=cipher1.decrypt(en_msg)
  13. print "msg:",de_msg

这个解密也不需要tag值。

python27\Lib\site-packages\Crypto\Cipher\_mode_gcm.py

  1. # ===================================================================
  2. #
  3. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. """
  31. Galois/Counter Mode (GCM).
  32. """
  33. __all__ = ['GcmMode']
  34. from binascii import unhexlify
  35. from Crypto.Util.py3compat import bord, _copy_bytes
  36. from Crypto.Util._raw_api import is_buffer
  37. from Crypto.Util.number import long_to_bytes, bytes_to_long
  38. from Crypto.Hash import BLAKE2s
  39. from Crypto.Random import get_random_bytes
  40. from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, VoidPointer,
  41. create_string_buffer, get_raw_buffer,
  42. SmartPointer, c_size_t, c_uint8_ptr)
  43. from Crypto.Util import _cpu_features
  44. # C API by module implementing GHASH
  45. _ghash_api_template = """
  46. int ghash_%imp%(uint8_t y_out[16],
  47. const uint8_t block_data[],
  48. size_t len,
  49. const uint8_t y_in[16],
  50. const void *exp_key);
  51. int ghash_expand_%imp%(const uint8_t h[16],
  52. void **ghash_tables);
  53. int ghash_destroy_%imp%(void *ghash_tables);
  54. """
  55. def _build_impl(lib, postfix):
  56. from collections import namedtuple
  57. funcs = ( "ghash", "ghash_expand", "ghash_destroy" )
  58. GHASH_Imp = namedtuple('_GHash_Imp', funcs)
  59. try:
  60. imp_funcs = [ getattr(lib, x + "_" + postfix) for x in funcs ]
  61. except AttributeError: # Make sphinx stop complaining with its mocklib
  62. imp_funcs = [ None ] * 3
  63. params = dict(zip(funcs, imp_funcs))
  64. return GHASH_Imp(**params)
  65. def _get_ghash_portable():
  66. api = _ghash_api_template.replace("%imp%", "portable")
  67. lib = load_pycryptodome_raw_lib("Crypto.Hash._ghash_portable", api)
  68. result = _build_impl(lib, "portable")
  69. return result
  70. _ghash_portable = _get_ghash_portable()
  71. def _get_ghash_clmul():
  72. """Return None if CLMUL implementation is not available"""
  73. if not _cpu_features.have_clmul():
  74. return None
  75. try:
  76. api = _ghash_api_template.replace("%imp%", "clmul")
  77. lib = load_pycryptodome_r
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/176565
推荐阅读
  

闽ICP备14008679号