我有一个加密的密码,是用python加密的。它是使用固定密钥的AES 128加密的base 64编码结果进行加密的。在
现在我的应用程序是一个C应用程序,我正在使用RijndaelManaged对其进行解密。我的代码是
static String Decrypt(string textToDecrypt, string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.Zeros;
rijndaelCipher.KeySize = 0x80; //128
rijndaelCipher.BlockSize = 0x80; //128
byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[0x10]; //16
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.Unicode.GetString(plainText);
}
我知道密码是怎么加密在Python。它像
^{pr2}$\u create_nonce()根据当前时间返回大约16位的值。在
这里的问题是,我得到的前16个是垃圾,其余的我得到正确的。 问题是什么?我想问题可能是rijndaelCipher.四.我们如何计算rijndaelCipher.四当它用其他语言加密时?在
我在我的应用程序中使用.NET2.0,我不能改变它。 Python代码:
DEFAULT_KEY = 'SV5@9raQSV5@9raQ'
aes_encrypt(self.DEFAULT_KEY, password)
def _create_nonce():
+ t1 = time.strftime("%Y%m%d%H%M%S")
+ t2 = time.strftime("%Y%m%d%H%M%S")
+
+ return struct.pack('dd', float(t1), float(t2))
+
+
+def _to_utf16(s, max_len=32, pad='\0'):
+
+ padded = str.ljust(str(s), max_len, pad)
+ utf16_padded, _ = codecs.utf_16_be_encode(padded)
+ buffer = struct.Struct(str(max_len * 2) + 'p')
+ return buffer.pack(utf16_padded)
+
+
+def aes_encrypt(key, s):
+ This will encrypt username and/or password
+ for IP Office Manager application.
+
+ Args:
+
+ key: encryption key
+ s: string to be encrypted
+ """
+ cipher = AES.new(key, AES.MODE_CBC)
+ str_to_encrypt = _create_nonce() + _to_utf16(s)
+ encrypted = base64.b64encode(cipher.encrypt(str_to_encrypt))
+
+ return encrypted
我修复了base64中的无效字符。在
它工作正常
你加密的是nonce+密码。你得到的是nonce+密码。在
你抱怨的前16个字节是nonce。把它们扔掉。在
编辑:
正如@CodeInChaos所指出的,在CBC模式下,如果你把IV弄错了,第一个块将无法正确解密,尽管随后的块会正确解密。但是,在您的例子中,第一个块是加密的nonce,并且您不关心您是否获得了nonce,因为您无论如何都要将其丢弃。因此,在解密时使用什么作为IV并不重要。在
从你的问题中我看到你这样说:
AES是一种对称密码。你不能在没有密钥和IV的情况下解密密码。在
因此,从你上面的引述来看,你不应该计算要解密的IV。您必须使用加密时使用的IV。在
你必须找出植物编码是用于静脉注射的
好的,这意味着您的密码/密钥有效,但您的IV有问题。
您应该知道Python在使用什么,因为代码片段中没有IV,我想可能都是零。在
相关问题 更多 >
编程相关推荐