一个只使用了hashlib和base64的对称加密实现

编程入门 行业动态 更新时间:2024-10-27 18:27:47

一个只使用了hashlib和base64的<a href=https://www.elefans.com/category/jswz/34/1763015.html style=对称加密实现"/>

一个只使用了hashlib和base64的对称加密实现

Python的加密算法实现起来不复杂,但是有时,现场环境可能无法挂载AES, DEC这类模块,这里有一个简化的实现,利用hash, xor实现的对称加密。它的最终输出加密字符串比较大,至少有32*2*2个字节。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
import base64
import binasciiPRIVATE_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_KEYsha256_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 stringKEY_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 stringKEY_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()

更多推荐

一个只使用了hashlib和base64的对称加密实现

本文发布于:2023-12-04 18:39:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1661864.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对称   使用了   hashlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!