python - 将 Simple3Des(VB.net) 加密-解密代码转换为 python
问题描述
我有一个纯 JSON 文本,它在 Visual Basic 中使用 Triple-DES 加密(下面提供的代码),现在我正在尝试使用 python 语言对其进行解密。当我测试它时,我得到了错误的输出。我认为有任何填充问题,因为我没有得到这一行:
ReDim Preserve hash(length - 1)
VB代码:
Public NotInheritable Class Simple3Des
Private TripleDes As New TripleDESCryptoServiceProvider
Private Function TruncateHash(ByVal key As String, ByVal length As Integer) As Byte()
Dim sha1 As New SHA1CryptoServiceProvider
' Hash the key.
Dim keyBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
End Function
Sub New(ByVal key As String)
' Initialize the crypto provider.
TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
End Sub
Public Function EncryptData(ByVal plaintext As String) As String
' Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte = System.Text.Encoding.Unicode.GetBytes(plaintext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the encoder to write to the stream.
Dim encStream As New CryptoStream(ms,
TripleDes.CreateEncryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
encStream.FlushFinalBlock()
' Convert the encrypted stream to a printable string.
Return Convert.ToBase64String(ms.ToArray)
End Function
Public Function DecryptData(ByVal encryptedtext As String) As String
' Convert the encrypted text string to a byte array.
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms,
TripleDes.CreateDecryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
' Convert the plaintext stream to a string.
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function
End Class
这是我使用不同填充和组合的方法:
from pyDes import *
import hashlib
import base64
key = b'Hi4Q=rLJnyqPj$G_cTqDcwgWo'
sha1_hash = hashlib.sha1(key).digest()
key_ = sha1_hash[:len(key)//8]
# key_ = sha1_hash[:-4]
# key_ = sha1_hash
base64Encrypted = r'''...'''
base64Decrypted = base64.b64decode(base64Encrypted)
print ("output: ", k.decrypt(base64Decrypted))
难道我做错了什么?
解决方案
推荐阅读
- javascript - 我可以使用 Javascript 将 http 请求发送到特定的 IP 地址吗?
- google-apps-script - openByURL 打开第一个值并沿选定列排列
- sql - Oracle 中 all_tab_statistics 的所有者是什么?
- firebase - Firestore 公共收集规则
- typescript - 如何在打字稿中定义一个通用接口
- angular - Angular - 支持最小值和最大值的反应形式的数字掩码
- powershell - Close-SMBOpenFile 抛出错误并且未在 try-catch 中捕获
- java - 以 JSON 正文作为输入在 Endpoint 中生成输出
- java - Bouncy castle no such method error (v1.61) and milo (v0.3.8)
- javascript - 在 Javascript 中加载液体的问题