github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/server/block/util/crypto_rc4.py (about)

     1  #coding:Utf-8
     2  
     3  
     4  from Crypto.Cipher import ARC4
     5  import binascii
     6  
     7  SECRET_KEY = 'd15d67922dd241cdbfc355d1ab44d6757dc759e60b3c4b74b868704c8a37b897'
     8  
     9  def encrypt(plain, key, b2a=binascii.b2a_hex):
    10      """
    11      RC4加密不需要补齐操作
    12      
    13      :param key: rc4密钥
    14      :param b2a: binary转ascii函数
    15      """
    16      assert isinstance(plain, str)
    17      assert key and isinstance(key, str)
    18  
    19      if b2a:
    20          assert callable(b2a)
    21  
    22      _ = ARC4.new(key).encrypt(plain)
    23      return _ if b2a is None else b2a(_)
    24  
    25      
    26  def decrypt(cipher, key, a2b=binascii.a2b_hex):
    27      """
    28      :param a2b: ascii转binary函数
    29      """
    30      if not (isinstance(cipher, str) and key and isinstance(key, str)):
    31          raise ValueError('cipher/key invalid: %r, %r' % (cipher, key))
    32  
    33      if a2b:
    34          assert callable(a2b)
    35          cipher = a2b(cipher)
    36  
    37      return ARC4.new(key).decrypt(cipher)