Python 和 VBScript 对文件编码-解码的方式 (Hex, Base64) 的疑问

本文介绍了一种在Windows平台上使用VBScript将图片转换为二进制码存储到数据库的方法,并在Linux平台上使用Python从数据库中读取并还原为图片的过程。文章详细记录了在转换过程中遇到的问题及解决方案。

本人目前的项目需要在Windows上用VBScript将一文件转换为二进制码存入数据库,再在Linux上用Python从数据库中读取并解码成图片。反复调试,发现微软和Python的转码标准不统一,导致了这种实现方式失败。若有哪位大侠是这方面的高手还请不吝赐教


1. Windows上使用VBScript对图片转码为“base64”格式:

Function ReadBinary(FileName)
	Const adTypeBinary = 1
	Dim stream, xmldom, node
	Set xmldom = CreateObject("Microsoft.XMLDOM")
	Set node = xmldom.CreateElement("binary")
	node.DataType = "bin.base64"
	Set stream = CreateObject("ADODB.Stream")
	stream.Type = adTypeBinary
	stream.Open
	stream.LoadFromFile FileName
	node.NodeTypedValue = stream.Read
	stream.Close
	Set stream = Nothing
	ReadBinary = node.Text
	Set node = Nothing
	Set xmldom = Nothing
End Function

Public Function WriteFile_Append(pathway,words)	
    Dim fileSystemObj,fileSpec,logFile,way
    Set fileSystemObj = CreateObject("Scripting.FileSystemObject")
    fileSpec = pathway 
    Set logFile = fileSystemObj.OpenTextFile(fileSpec, 8, true) 
    logFile.WriteLine (CStr(words))
    logFile.Close
    Set logFile = Nothing
End Function

hexVal = ReadBinary("c:\desk_2.png")
Call WriteFile_Append("c:\ttt.txt", hexVal)

2. 用Python读取文件,并转换回图片格式:

f = open('c:\\ttt.txt')
img = f.read()
f.close()
f1 = open('c:\\desk.png', 'a+')
f1.write(img.decode('base64'))
f1.close()

3. 发现原图desk_2.png和转换完后的图desk.png大小相差1k,但是desk.png无法显示出图片。


注:

1. 用一个小一点的gif试了一下,转换后图片缺失部分像素

2. VBS和Python同时使用“hex”代替“base64”,中间编码的格式更是大相径庭


看来MS和Python对于格式转换的标准不一样...若有哪位大侠是这方面的高手还请不吝赐教,谢谢


---------- 以下于2012年5月17日 ------

注:此问题已解决,之前是因为python的“open”使用了“a+”方式打开文件,经查此方法无法写入二进制文件,只需要将其改为“wb”,一切搞定


from Crypto.Cipher import AES from Crypto.Random import get_random_bytes import os def encrypt_lsp_file(input_path, output_path, key=None): """ 加密.LSP文件生成乱码文件 参数: input_path: 原始.LSP文件路径 output_path: 加密后的乱码文件路径 key: 可选的密钥(16/24/32字节),默认自动生成 """ # 读取原始文件内容 with open(input_path, 'rb') as f: plaintext = f.read() # 生成随机密钥(如果未提供) key = key or get_random_bytes(32) # 生成随机初始化向量(IV) iv = get_random_bytes(16) # 创建AES加密器(CFB模式) cipher = AES.new(key, AES.MODE_CFB, iv) # 加密内容(自动处理填充) ciphertext = cipher.encrypt(plaintext) # 将IV密文写入新文件 with open(output_path, 'wb') as f: f.write(iv + ciphertext) return key # 返回密钥用于后续解密 def decrypt_lsp_file(input_path, output_path, key): """ 解密乱码文件恢复原始内容 参数: input_path: 加密文件路径 output_path: 解密后的文件路径 key: 加密时使用的密钥 """ # 读取加密文件 with open(input_path, 'rb') as f: data = f.read() # 分离IV密文 iv = data[:16] ciphertext = data[16:] # 创建AES解密器 cipher = AES.new(key, AES.MODE_CFB, iv) # 解密内容 plaintext = cipher.decrypt(ciphertext) # 写入解密后的文件 with open(output_path, 'wb') as f: f.write(plaintext) # 使用示例 if __name__ == "__main__": # 原始文件加密文件路径 original_file = "example.lsp" encrypted_file = "encrypted.lsp" decrypted_file = "decrypted.lsp" # 加密文件(保留原始文件example.lsp不变) secret_key = encrypt_lsp_file(original_file, encrypted_file) print(f"加密完成!密钥请妥善保存: {secret_key.hex()}") print(f"原始文件: {original_file} (未修改)") print(f"加密文件: {encrypted_file} (乱码内容)") # 解密示例(需要时使用) # decrypt_lsp_file(encrypted_file, decrypted_file, secret_key) 改成“.lsp文件加密工具.vbs”
10-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值