赞
踩
python 需要安装: PyCryptodome
示例代码:
- from Crypto.Cipher import AES
-
-
- key="1111111111111111"
- iv="000000000000"
- msg="2222222222222222"
-
- #加密
- cipher = AES.new(key, AES.MODE_GCM, iv)
- en_msg=cipher.encrypt(msg)
- #print en_msg,
- print "en_msg(hex)",en_msg.encode('hex')
-
- #解密
- cipher1 = AES.new(key, AES.MODE_GCM, iv)
- de_msg=cipher1.decrypt(en_msg)
- print "msg:",de_msg
这个解密也不需要tag值。
python27\Lib\site-packages\Crypto\Cipher\_mode_gcm.py
- # ===================================================================
- #
- # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
- # All rights reserved.
- #
- # Redistribution and use in source and binary forms, with or without
- # modification, are permitted provided that the following conditions
- # are met:
- #
- # 1. Redistributions of source code must retain the above copyright
- # notice, this list of conditions and the following disclaimer.
- # 2. Redistributions in binary form must reproduce the above copyright
- # notice, this list of conditions and the following disclaimer in
- # the documentation and/or other materials provided with the
- # distribution.
- #
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- # POSSIBILITY OF SUCH DAMAGE.
- # ===================================================================
-
- """
- Galois/Counter Mode (GCM).
- """
-
- __all__ = ['GcmMode']
-
- from binascii import unhexlify
-
- from Crypto.Util.py3compat import bord, _copy_bytes
-
- from Crypto.Util._raw_api import is_buffer
-
- from Crypto.Util.number import long_to_bytes, bytes_to_long
- from Crypto.Hash import BLAKE2s
- from Crypto.Random import get_random_bytes
-
- from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, VoidPointer,
- create_string_buffer, get_raw_buffer,
- SmartPointer, c_size_t, c_uint8_ptr)
-
- from Crypto.Util import _cpu_features
-
-
- # C API by module implementing GHASH
- _ghash_api_template = """
- int ghash_%imp%(uint8_t y_out[16],
- const uint8_t block_data[],
- size_t len,
- const uint8_t y_in[16],
- const void *exp_key);
- int ghash_expand_%imp%(const uint8_t h[16],
- void **ghash_tables);
- int ghash_destroy_%imp%(void *ghash_tables);
- """
-
- def _build_impl(lib, postfix):
- from collections import namedtuple
-
- funcs = ( "ghash", "ghash_expand", "ghash_destroy" )
- GHASH_Imp = namedtuple('_GHash_Imp', funcs)
- try:
- imp_funcs = [ getattr(lib, x + "_" + postfix) for x in funcs ]
- except AttributeError: # Make sphinx stop complaining with its mocklib
- imp_funcs = [ None ] * 3
- params = dict(zip(funcs, imp_funcs))
- return GHASH_Imp(**params)
-
-
- def _get_ghash_portable():
- api = _ghash_api_template.replace("%imp%", "portable")
- lib = load_pycryptodome_raw_lib("Crypto.Hash._ghash_portable", api)
- result = _build_impl(lib, "portable")
- return result
- _ghash_portable = _get_ghash_portable()
-
-
- def _get_ghash_clmul():
- """Return None if CLMUL implementation is not available"""
-
- if not _cpu_features.have_clmul():
- return None
- try:
- api = _ghash_api_template.replace("%imp%", "clmul")
- lib = load_pycryptodome_r
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。