Mauvais déchiffrement AES lorsque vous essayez de déchiffrer dans une autre langue

Lorsque j’essaie de chiffrer en C ++ et de déchiffrer en C #, cela me donne une erreur

Les données d’entrée ne sont pas un bloc complet

Mais cela ne me dit rien, car si je tente de déchiffrer le message en C ++, le même langage que celui utilisé pour le chiffrement fonctionne correctement.

Donc, du code de la partie C ++:

int main(int argc, char* argv[]) { std::ssortingng szEncryptionKey = "Sixteen byte key"; std::ssortingng szEncryptionIV = "Sixteen byte key"; std::ssortingng str = "I do not like green eggs and ham I do not like them Sam-I-Am."; std::ssortingng str_encrypted = encrypt(str, szEncryptionKey, szEncryptionIV); std::ssortingng str_decrypted = decrypt(str_encrypted, szEncryptionKey, szEncryptionIV); std::cout << "str encrypted: " << str_encrypted << std::endl; std::cout << "str decrypted: " << str_decrypted << std::endl; return 0; } std::string encrypt(const std::string& str_in, const std::string& key, const std::string& iv) { std::string str_out; CryptoPP::CFB_Mode::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str()); CryptoPP::SsortingngSource encryptor(str_in, true, new CryptoPP::StreamTransformationFilter(encryption, new CryptoPP::Base64Encoder( new CryptoPP::SsortingngSink(str_out), BlockPaddingSchemeDef::NO_PADDING ) ) ); return str_out; } 

Je prends la chaîne cryptée et encodée en base64, ainsi que les clés KEY et IV au C #:

  static void Main(ssortingng[] args) { byte[] bytes = Encoding.UTF8.GetBytes("Sixteen byte key"); Ssortingng msg2 = "cboiiqATFAU9KyK49BJMmkLLwiAyaP6yYjyM0oP09xbITLGaxRuLCsTYYzUKANydhO+JO7N00aVz0tmFTg=="; byte[] msg = Convert.FromBase64Ssortingng(msg2); DecryptSsortingngFromBytes_Aes(msg, bytes, bytes); static ssortingng DecryptSsortingngFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV"); // Declare the string used to hold // the decrypted text. using (Aes aesAlg = Aes.Create()) { aesAlg.Mode = CipherMode.CFB; aesAlg.Padding = PaddingMode.None; aesAlg.Key = Key; aesAlg.IV = IV; // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV"); byte[] encrypted = null; // Create an Aes object // with the specified key and IV. using (ICryptoTransform decrypt = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)) { byte[] decryptedText = decrypt.TransformFinalBlock(cipherText, 0, cipherText.Length); String s = Encoding.UTF8.GetString(decryptedText); Console.Write(s); return s; } } } 

Les messages d’erreur viennent à cette ligne:

 byte[] decryptedText = decrypt.TransformFinalBlock(cipherText, 0, cipherText.Length); 

Cette ligne :

 byte[] bytes = Encoding.UTF8.GetBytes("Sixteen byte key"); 

C’est juste une supposition avec l’UTF8, mais je n’ai aucune idée si c’est le bon encodage.

Quelqu’un peut-il m’aider avec ce problème?

Les données d’entrée ne sont pas un multiple exact de la taille du bloc, 16 octets pour AES. msg2 lors du décodage est 61 octets, ce qui n’est pas une longueur chiffrée AES valide, donc invalide et produit le message d’erreur: “Les données saisies ne sont pas un bloc complet”.

AES étant basé sur des blocs, si les données à chiffrer ne sont pas un multiple exact de la taille du bloc, elles doivent être complétées. La méthode de remplissage générale utilisée avec AES est PKCS # 7, parfois appelée PKCS # 5.

Le mode CFB nécessite un remplissage, pas le mode CFB8.