任何相信Python和VB 2008的人的问题:
我正在构建一个覆盆子pi板的系统,需要由具有Windows操作系统的远程计算机操作,但不能使python中的代码与vb.net代码兼容。
我想知道如何为vb生成一个代码,该代码的结果与使用AES的Python代码兼容。
如何在python&中加密和解密vb.net与下面的AES代码兼容?
使用AES的python代码:
from Crypto.Cipher import AES
import base64
def EncodeAES(secret, s):
BLOCK_SIZE = 32
PADDING = '{'
pad = BLOCK_SIZE - len(s)
pad = pad % BLOCK_SIZE
pad = pad * PADDING
pad = s + pad
cipher = AES.new(secret)
c = cipher.encrypt(pad)
enc = base64.b64encode(c)
return enc
def DecodeAES(secret, e):
PADDING = '{'
cipher = AES.new(secret)
b64 = base64.b64decode(e)
c = cipher.decrypt(b64)
dec = c.rstrip(PADDING)
return dec
secret2 = 'x\x90\xd4~\x18\x8a\xb6\x9f\xc7\x82/DI\x1d\xa7\xb9\xdf\xad\x80S\x95\x90\x13}T,\x89/\xcf\x1a\xe9\xc2'
# create a cipher object using the random secret
# generate a random secret key
#BLOCK_SIZE1 = 32
#secret2 = os.urandom(BLOCK_SIZE1)
print (('secret string:', secret2))
# encode a string
encoded = EncodeAES(secret2, 'password')
print (('Encrypted string:', encoded))
# decode the encoded string
decoded = DecodeAES(secret2, encoded)
print (('Decrypted string:', decoded))
我找到了一个DES代码(vb.net& python兼容,仅用于示例):
vb.net DES:
Imports Microsoft.VisualBasic.CompilerServices
Imports System
Imports System.Diagnostics
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim temp As String = ""
Dim from_pyton As String = "rhxUtKPuxriRaPd/MDgliw=="
temp = Crypto.Encrypt("I love security")
MsgBox(temp)
temp = Crypto.Decrypt(from_pyton)
MsgBox(temp)
End
End Sub
End Class
Public Class Crypto
Private Shared DES As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
Private Shared MD5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
Public Shared Function MD5Hash(ByVal value As String) As Byte()
Return Crypto.MD5.ComputeHash(Encoding.ASCII.GetBytes(value))
End Function
Public Shared Function Encrypt(ByVal stringToEncrypt As String) As String
Crypto.DES.Key = Crypto.MD5Hash("L6#F&,q2$xLx")
Crypto.DES.Mode = CipherMode.ECB
Dim bytes As Byte() = Encoding.ASCII.GetBytes(stringToEncrypt)
Return Convert.ToBase64String(Crypto.DES.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length))
End Function
Public Shared Function Decrypt(ByVal encryptedString As String) As String
Dim result As String
Try
Crypto.DES.Key = Crypto.MD5Hash("L6#F&,q2$xLx")
Crypto.DES.Mode = CipherMode.ECB
Dim array As Byte() = Convert.FromBase64String(encryptedString)
result = Encoding.ASCII.GetString(Crypto.DES.CreateDecryptor().TransformFinalBlock(array, 0, array.Length))
Return result
Catch expr_4D As Exception
ProjectData.SetProjectError(expr_4D)
ProjectData.ClearProjectError()
End Try
result = Nothing
Return result
End Function
End Class
python DES兼容:(需要安装pyDes lib:http://twhiteman.netfirms.com/des.html)
from pyDes import *
import hashlib
import base64
key = hashlib.md5("L6#F&,q2$xLx").digest()
data = "I love security"
k = triple_des(key, ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
base64Encrypted = base64.b64encode(d)
print ("Encrypted: %r" % base64Encrypted)
base64Decrypted = base64.b64decode(base64Encrypted)
print ("Decrypted: %r" % k.decrypt(base64Decrypted))