解密Python中使用.NET中的字符串加密 [英] Decrypting in Python an string encrypted using .NET
问题描述
我想用C#字符串加密,并使用Python进行解密。加密/解密的部分按预期工作(即我能我原先加密的字符串进行解密)。但是被Python返回的字符串的开头2个额外的字节,每个字符用空格隔开。
**原始的字符串**(前加密 - 使用C#加密)=你想保持私人使用AES东西
**解密的字符串**(使用Python)=S omethingyouwanttokeep privatewith AES
为什么会出现这两个额外的字节的字符串的开头?为什么在解密的字符串所有这些空间?知道为什么吗?
谢谢!
加密用C#
公共静态字符串加密< T>(字符串值,字符串密码,串盐)
其中T:SymmetricAlgorithm,新的()
{
DeriveBytes RGB =新Rfc2898DeriveBytes(密码,Encoding.Uni code.GetBytes(盐));
SymmetricAlgorithm算法=新T();
byte []的rgbKey = rgb.GetBytes(algorithm.KeySize>> 3);
byte []的rgbIV = rgb.GetBytes(algorithm.BlockSize>> 3);
ICryptoTransform的变换= algorithm.CreateEncryptor(rgbKey,rgbIV);
使用(MemoryStream的缓冲区=新的MemoryStream())
{
使用(CryptoStream的流=新的CryptoStream(缓冲区,转换CryptoStreamMode.Write))
{
使用(StreamWriter的作家=新的StreamWriter(流Encoding.Uni code))
{
writer.Write(值);
}
}
返回Convert.ToBase64String(buffer.ToArray());
}
}
字符串平原=你想保持私人使用AES的东西;
加密的字符串= CipherUtility.Encrypt< AesManaged>(平纹,密码,盐);
解密使用Python + pycrypto
进口的base64,SYS
进口Crypto.Cipher.AES
密码= base64.b64de code('PSCIQGfoZidjEuWtJAdn1JGYzKDonk9YblI0uv96O8s =')#查看rgbKey
盐= base64.b64de code('ehjtnMiGhNhoxRuUzfBOXw ==')#查看rgbIV
AES = Crypto.Cipher.AES.new(密码,Crypto.Cipher.AES.MODE_CBC,盐)
文= base64.b64de$c$c('QpHn/fnraLswwI2Znt1xTaBzRtDqO4V5QI78jLOlVsbvaIs0yXMUlqJhQtK+su2hYn28G2vNyLkj0zLOs+RIjElCSqJv1aK/Yu8uY07oAeStqRt4u/DVUzoWlxdrlF0u')
打印aes.decrypt(文本)
的字符串连接codeD使用UTF-16编码字节。前两个字节是一个BOM。然后,每个字符连接coded到两个字节。
这是对文件<一个href="http://msdn.microsoft.com/en-us/library/system.text.encoding.uni$c$c.aspx"><$c$c>Encoding.Uni$c$c$c$c>:
获取使用Little Endian字节序的UTF-16格式的编码。
要得到你需要去code回从UTF-16字节到一个统一code字符串的原始字符串。
打印aes.decrypt(文本).DE code('UTF-16')
I am trying to encrypt a string using C# and decrypt it using Python. The encryption/decryption part works as expected (i.e. I am able to decrypt the string I originally encrypted). However the string returned by Python has 2 extra bytes at the beginning and each character is separated by a space.
**Original string** (before encryption -- encrypted using C#) = "Something you want to keep private with AES"
**Decrypted string** (using Python) = "��S o m e t h i n g y o u w a n t t o k e e p p r i v a t e w i t h A E S"
Why am I getting these two extra bytes at the beginning of the string? Why all those spaces in the decrypted string? Any idea why?
Thanks!
Encryption with C#
public static string Encrypt<T>(string value, string password, string salt)
where T : SymmetricAlgorithm, new()
{
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = new T();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(value);
}
}
return Convert.ToBase64String(buffer.ToArray());
}
}
string plain = "Something you want to keep private with AES";
string encrypted = CipherUtility.Encrypt<AesManaged>(plain, "password", "salt");
Decryption with Python + pycrypto
import base64, sys
import Crypto.Cipher.AES
password = base64.b64decode('PSCIQGfoZidjEuWtJAdn1JGYzKDonk9YblI0uv96O8s=') # See rgbKey
salt = base64.b64decode('ehjtnMiGhNhoxRuUzfBOXw==') # See rgbIV
aes = Crypto.Cipher.AES.new(password, Crypto.Cipher.AES.MODE_CBC, salt)
text = base64.b64decode('QpHn/fnraLswwI2Znt1xTaBzRtDqO4V5QI78jLOlVsbvaIs0yXMUlqJhQtK+su2hYn28G2vNyLkj0zLOs+RIjElCSqJv1aK/Yu8uY07oAeStqRt4u/DVUzoWlxdrlF0u')
print aes.decrypt(text)
The string is encoded to bytes using the UTF-16 encoding. The first two bytes are a BOM. Then each character is encoded to two bytes.
From the documentation for Encoding.Unicode
:
Gets an encoding for the UTF-16 format using the little endian byte order.
To get the original string you need to decode it back from UTF-16 bytes to a Unicode string.
print aes.decrypt(text).decode('utf-16')
这篇关于解密Python中使用.NET中的字符串加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!