[英]BitConverter.ToString(hash).Replace(“-”, string.Empty) Equivalent in python?
How can I write the following line in python? 如何在python中编写以下行?
BitConverter.ToString(hash).Replace("-", string.Empty)
This is what I'm trying: 这是我正在尝试的:
import random
import uuid
import base64
from Crypto.Cipher import AES
import hashlib
from Crypto import Random
import binascii
key = hashlib.sha256(b'SOmeKEyy').digest()
key1 = base64.b64encode(key)
iv_value = b'RandomValuie'
iv = base64.b64decode(iv_value)
cipher = AES.new(key, AES.MODE_CFB, iv)
message = Mac_AddressBytes + ip_address_Bytes + DeviceIdBytes
msg = iv + cipher.encrypt(message)
s =encrypt(message, key)
print(s)
sa = bytearray(s)
DeviceIdentity1 = binascii.b2a_hex(s)
DeviceIdentityDecoded = "0x" + DeviceIdentity1.decode('utf-8')
print (str(DeviceIdentityDecoded))
Mac_AddressBytes + ip_address_Bytes + DeviceIdBytes
is a valid String. Mac_AddressBytes + ip_address_Bytes + DeviceIdBytes
是有效的字符串。
The ouput I am getting here is in hexadecimal which I want but If i run the same string in C# I am getting a diffrent string the part that I am missing in python is the BitConverter.ToString(hash).Replace("-", string.Empty)
. 我要到达的输出是我想要的十六进制,但是如果我在C#中运行相同的字符串,我得到的字符串有所不同,那么我在python中缺少的部分就是BitConverter.ToString(hash).Replace("-", string.Empty)
。
I have tried to following: 我试图遵循以下条件:
DeviceIdentity2 = "0x" + DeviceIdentity1.decode('utf-8').replace("-", "")
but it gives me back the same value as: 但这给了我以下相同的价值:
DeviceIdentityDecoded = "0x" + DeviceIdentity1.decode('utf-8')
When I Decrpyt the string I am getting the following: 当我Decrpyt字符串时,我得到以下内容:
Îí5ó/½û.qõX9D&Ç:eXM»Bñj2µ\ëÁ§ÓËÔ¤ý ¼t®@Z9)Àåñr¹ Ör¾hÅåéÙ|¶nÙZÆÃï,¡WÀj©r{ÆR¥f,|^W¯C
Ù1¾+MöB;Sô«¹næk0ú·7e,atMìÆ¿Kfí
Is there an equivalent in python the c# code above? 上面的C#代码在python中是否具有等效功能?
For anyone interested I have found out that i was missing the PKCS7Encoder() 对于任何有兴趣的人,我发现我都缺少PKCS7Encoder()
This is the updated answer that works 这是更新的答案
key = base64.b64decode("SOmeKEyy")
iv_value = 'RandomValue'
iv = base64.b64decode(iv_value)
encoder = PKCS7Encoder()
padded_text = encoder.encode(secret_text)
padded_text1 = padded_text.encode()
print(padded_text1)
e = AES.new(key, AES.MODE_CBC, iv)
cipher_text = e.encrypt(padded_text1)
Finally = binascii.b2a_hex(cipher_text)
DeviceIdentity = "0x" + Finally.decode('utf-8').replace("-", "")
print(DeviceIdentity)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.