Python的加密算法实现起来不复杂,但是有时,现场环境可能无法挂载AES, DEC这类模块,这里有一个简化的实现,利用hash, xor实现的对称加密。它的最终输出加密字符串比较大,至少有32*2*2个字节。
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import hashlib
- import base64
- import binascii
-
- PRIVATE_KEY = bytearray(("May the force be with U.").encode('utf-8'));
-
- def xor_array_with_key(array, key):
- result = bytearray() # 创建一个空的字节数组,用于存储结果
- for i in range(0, len(array), len(key)): # 按照每 32 字节的步长遍历数组
- for j in range(len(key)): # 遍历每个字节
- if(i+j)>=len(array) or j>=len(key): break;
- result.append(array[i+j] ^ key[j]) # 将当前字节和对应的密钥字节进行异或操作,并附加到结果中
- return result
-
- # 使用 SHA256 哈希算法对字符串进行加密
- def gp_encrypt_string(string):
- global PRIVATE_KEY
- sha256_hash = hashlib.sha256()
- sha256_hash.update(string.encode('utf-8'))
- encrypted_data = sha256_hash.digest();
-
- KEY_SIZE = 256/8;
- outputhex = sha256_hash.hexdigest(); #the very first 256/8 = 32Bytes.
-
- utf8_string = string.encode('utf-8');
- byte_array = len(string).to_bytes(4, 'big'); #有效数据开头是字符串长度
- byte_array = byte_array + bytearray(utf8_string);
- byte_array = bytearray(byte_array);
- offset = len(byte_array)%KEY_SIZE;
- zero_bytes = bytearray(b"\x00" * int(KEY_SIZE - offset)) # 5 个零的字节数组
- byte_array.extend(zero_bytes)
- byte_array = xor_array_with_key(byte_array, encrypted_data)
- byte_array = xor_array_with_key(byte_array, PRIVATE_KEY)
-
- base64_encoded = base64.b64encode(byte_array).decode('utf-8')
- outputhex = outputhex + base64_encoded;
- sha256_hash = hashlib.sha256()
- sha256_hash.update(outputhex.encode('utf-8'));
- outputhex = outputhex + sha256_hash.hexdigest();
- return outputhex;
-
- # 对使用上述方法加密的字符串进行解密
- def gp_decrypt_string(encrypted_string):
- global PRIVATE_KEY
- #no encode string
- KEY_SIZE = 256/8;
- if(len(encrypted_string)<=KEY_SIZE*2*2): return encrypted_string;
-
- last32HexBytes = encrypted_string[int(-1*KEY_SIZE*2):];
- stringRemoveTail = encrypted_string[0:int(-1*KEY_SIZE*2)];
-
- first32HexBytes = stringRemoveTail[0:int(KEY_SIZE*2)];
- binOfKey = binascii.unhexlify(first32HexBytes);
-
-
- sha256_hash = hashlib.sha256()
- sha256_hash.update(stringRemoveTail.encode('utf-8'))
- encrypted_data = sha256_hash.hexdigest();
- if(last32HexBytes != encrypted_data):
- return encrypted_string;
-
- thePasswordBody = stringRemoveTail[int(KEY_SIZE*2):];
- base64_decoded = base64.b64decode(thePasswordBody)
- #print('before xor', base64_decoded);
- base64_decoded = xor_array_with_key(base64_decoded, PRIVATE_KEY)
- byte_array = xor_array_with_key(base64_decoded, binOfKey);
- lenOfStr = int.from_bytes(byte_array[0:4], 'big');
- decrypted_string = byte_array[4:].decode('utf-8')
- decrypted_string = decrypted_string[:lenOfStr]
- return decrypted_string
-
- #is string encrypted?
- def gp_is_encrypt_string(encrypted_string):
- #no encode string
- KEY_SIZE = 256/8;
- if(len(encrypted_string)<=KEY_SIZE*2*2): return False;
-
- last32HexBytes = encrypted_string[int(-1*KEY_SIZE*2):];
- stringRemoveTail = encrypted_string[0:int(-1*KEY_SIZE*2)];
-
- first32HexBytes = stringRemoveTail[0:int(KEY_SIZE*2)];
- binOfKey = binascii.unhexlify(first32HexBytes);
-
-
- sha256_hash = hashlib.sha256()
- sha256_hash.update(stringRemoveTail.encode('utf-8'))
- encrypted_data = sha256_hash.hexdigest();
- if(last32HexBytes != encrypted_data):
- return False;
- else:
- return True;
-
- def test():
- # 测试
- plaintext = "Hello, World!"
- encrypted = gp_encrypt_string(plaintext)
- print("加密后的字符串:", encrypted)
-
- print("加密校验:", gp_is_encrypt_string(encrypted))
-
- decrypted = gp_decrypt_string(encrypted)
- print("解密后的字符串:", decrypted)
-
- #test()