Write UTF-8 File From VB6

本文介绍了一个VB6环境下用于创建UTF-8编码文件的模块。该模块包括了打开、关闭文件及写入字符串到文件的功能,并详细展示了如何将Unicode编码转换为UTF-8编码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hi, I am Arren.
Maybe you need output file as UTF-8 encoding, but VB6 is nativly support ansi encoding, it's depend on your machine.  I write this module, it can help you write utf-8 encoding files from vb6.
Option Base 0

Public Function OpenFile(path As StringAs Integer
    
Kill path
    
Dim fid As Integer
    fid 
= FreeFile()
    Open path 
For Binary As fid
    OpenFile 
= fid
End Function

Public Sub CloseFile(fid As Integer)
    Close fid
End Sub


Public Function WriteFile(fid As Integer, s As StringAs Integer
    
Dim bs() As Byte, buf(0 To 6As Byte
    
Dim c As Integer, l As Integer, i As Integer, ch As Long
    
    bs 
= s
    c 
= UBound(bs)
    
    
For i = 0 To c Step 2
        ch 
= bs(i + 1* 2 ^ 8 Or bs(i)
        l 
= UnicodeToUTF8(ch, buf)
        
Call WriteBytes(buf, l, fid)
    
Next i
End Function

Private Sub WriteBytes(buf() As Byte, cnt As Integer, fid As Integer)
    
Dim i As Integer
    
Dim b(0 To 0As Byte
    
For i = 0 To cnt
        b(
0= buf(i)
        Put fid, , b
    
Next i
End Sub

Private Function UnicodeToUTF8(ch As Long, buf() As ByteAs Integer
    
Dim i As Integer: i = 0
    
If (ch < &H80) Then
        buf(i) 
= ch
    
ElseIf (ch < &H800) Then
        buf(i) 
= &HC0 Or ((ch And &H7C00) / 2 ^ 6): i = i + 1
        buf(i) 
= &H80 Or (ch And &H3F)
    
ElseIf (ch < &H10000) Then
        buf(i) 
= &HE0 Or ((ch And &HF000) / 2 ^ 12): i = i + 1
        buf(i) 
= &H80 Or ((ch And &HFC0) / 2 ^ 6): i = i + 1
        buf(i) 
= &H80 Or (ch And &H3F)
    
End If
    UnicodeToUTF8 
= i
End Function

def aes_cbc_encrypt(plaintext): """ 执行AES-CBC加密(PKCS7填充), 获取body乱码 :param plaintext: 明文, {"id":"159e0cdda95844cebd9dd96ea33d9574","type":"jigsaw","data":"{\"xPos\":117}"} :return: 明文通过aes_cbc加密后的结果 """ iv = "6wiqkc42rdyv4mya" key = "t51s70wm3vb26gx1" iv_bytes = iv.encode('latin1') key_bytes = key.encode('latin1') # 传入的明文数据必须经过两次json.dumps, 少一次缺一个引号都不行 plaintext = json.dumps(plaintext, separators=(',', ':'), ensure_ascii=False) # 转换为字节数据 plaintext_bytes = plaintext.encode('utf-8') # 创建填充器 padder = padding.PKCS7(algorithms.AES.block_size).padder() padded_data = padder.update(plaintext_bytes) + padder.finalize() # 创建加密器并执行加密 cipher = Cipher( algorithms.AES(key_bytes), modes.CBC(iv_bytes), backend=default_backend() ) encryptor = cipher.encryptor() ciphertext = encryptor.update(padded_data) + encryptor.finalize() # 返回Base64编码结果 # print(base64.b64encode(ciphertext).decode('utf-8')) return base64.b64encode(ciphertext).decode('utf-8') # plaintext = json.dumps(plaintext, separators=(',', ":")) # cipher = AES.new(key_bytes, AES.MODE_CBC, iv_bytes) # padding_data = pad(plaintext.encode("utf-8"), AES.block_size) # encrypted = cipher.encrypt(padding_data) # return base64.b64encode(encrypted).decode('utf-8') def aes_cbc_decryption(data): # 就只看解密这块有点问题了, iv = "6wiqkc42rdyv4mya" key = "t51s70wm3vb26gx1" iv_bytes = iv.encode('utf-8') key_bytes = key.encode('utf-8') # 处理不同类型输入数据 # if isinstance(data, str): # print("走的1") # # 如果是Base64字符串,解码为字节 # ciphertext = base64.b64decode(data) # elif isinstance(data, bytes): # print('走的2') # ciphertext = arraybuffer_to_base64(data) # else: # print('走的3') # raise TypeError("数据必须是bytes或Base64字符串") # print("解密类型:", type(ciphertext)) # 创建解密器并执行解密 cipher = Cipher( algorithms.AES(key_bytes), modes.CBC(iv_bytes), backend=default_backend() ) print('需要解密的数据:', data) plaintext_bytes = arraybuffer_to_base64(data) print("plain_b64_str:", plaintext_bytes) decryptord = cipher.decryptor() decrypted = decryptord.update(plaintext_bytes.encode('utf-8')) + decryptord.finalize() print("decrypted:", decrypted) time.sleep(1) # def validate_padding(decrypted_data): # if len(decrypted_data) == 0: # raise ValueError("无法验证填充:数据为空") # # 最后一个字节的值即为填充长度 # padding_length = decrypted_data[-1] # print("paddinglength:", padding_length) # if padding_length > 16 or padding_length < 1: # raise ValueError(f"填充长度无效: {padding_length}") # # 检查最后padding_length个字节是否都等于padding_length # expected_padding = bytes([padding_length]) * padding_length # actual_padding = decrypted_data[-padding_length:] # if actual_padding != expected_padding: # raise ValueError(f"填充字节不匹配:应为{expected_padding},实际为{actual_padding}") # return padding_length # # try: # # 尝试使用库的unpadder # unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder() # plaintext_bytes = unpadder.update(decrypted) + unpadder.finalize() # except ValueError: # # 如果库方法失败,则尝试手动验证(用于调试) # print("使用库的unpadder失败,尝试手动验证") # # 手动验证并去除填充 # padding_length = validate_padding(decrypted) # plaintext_bytes = decrypted[:-padding_length] # print("注意:手动去除了填充,请检查数据完整性") # 转换为字符串 # unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder() plaintext_bytes = unpadder.update(decrypted) + unpadder.finalize() # 在解密后验证数据长度 if len(plaintext_bytes) == 0: raise ValueError("解密后得到空数据") print('plain_bytes:', plaintext_bytes) plaintext = plaintext_bytes.decode('utf-8') print('最终:', plaintext)帮我把这段代码改成用PyCryptodome库实现
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值