解密C#中的AES密码,该密码用Python加密

2024-09-26 17:42:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个加密的密码,是用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

Tags: tokey应用程序lenreturnbytelengthencrypt
3条回答

我修复了base64中的无效字符。在

  • 它工作正常

       string textToDecrypt = 
            "wDkvBlzgoRCz749u3OjL8/uXXc4CfdEg"
          + "qP7lk3okP104HxAxQaadVdCWgzE4uUNO"
          + "9B+RYnstFmDf21CSZ89GxnzBJtiirXi0"
          + "N+/IIocPjwg=";
    

你加密的是nonce+密码。你得到的是nonce+密码。在

你抱怨的前16个字节是nonce。把它们扔掉。在

编辑:

正如@CodeInChaos所指出的,在CBC模式下,如果你把IV弄错了,第一个块将无法正确解密,尽管随后的块会正确解密。但是,在您的例子中,第一个块是加密的nonce,并且您不关心您是否获得了nonce,因为您无论如何都要将其丢弃。因此,在解密时使用什么作为IV并不重要。在

从你的问题中我看到你这样说:

How do we calculate the rijndaelCipher.IV when its encrypted in other language?

AES是一种对称密码。你不能在没有密钥和IV的情况下解密密码。在

因此,从你上面的引述来看,你不应该计算要解密的IV。您必须使用加密时使用的IV。在

你必须找出植物编码是用于静脉注射的

I am getting first 16 as garbage and rest I am getting correctly.

好的,这意味着您的密码/密钥有效,但您的IV有问题。
您应该知道Python在使用什么,因为代码片段中没有IV,我想可能都是零。在

//rijndaelCipher.IV = keyBytes;
rijndaelCipher.IV = new byte[keyBytes.Lenght];  // an array defaults to all 0x00 bytes

相关问题 更多 >

    热门问题