Je travaille avec SagePay Forms et convertit actuellement les exemples de VB qu'ils ont en C#. J'ai bien progressé et la partie chiffrement de mon projet fonctionne bien (SagePay peut le décrypter).C# AES Decryption
Le problème que je rencontre est que lorsque je tente de décrypter la chaîne, elle se transforme en ordures. Si quelqu'un l'a fait avant, j'apprécierais vraiment de l'aide avec mon code de décryptage. J'ai inclus le code de cryptage qui fonctionne et les deux premières lignes sont la configuration et l'appel d'une autre méthode.
Je n'ai pas ajouté le code VB mais si cela est nécessaire je pourrais l'ajouter. Je ne voulais pas un poste énorme si ce n'est pas nécessaire.
Méthodes utilitaires:
public string byteArrayToHexString(byte[] ba)
{
return BitConverter.ToString(ba).Replace("-", "");
}
public static byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
cryptage principale méthode avec deux premières lignes étant l'appel de celui-ci extrait d'une méthode plus grande.
string crypt = "blahblahblah"
string EncryptAndEncode = "@" + byteArrayToHexString(aesEncrypt(crypt));
private byte[] aesEncrypt(string inputText)
{
RijndaelManaged AES = new RijndaelManaged();
//set the mode, padding and block size for the key
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CBC;
AES.KeySize = 128;
AES.BlockSize = 128;
//convert key and plain text input into byte arrays
Byte[] keyAndIvBytes = UTF8Encoding.UTF8.GetBytes("tR7nR6wZHGjYMCuV");
Byte[] inputBytes = UTF8Encoding.UTF8.GetBytes(inputText);//AbHLlc5uLone0D1q
//create streams and encryptor object
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, AES.CreateEncryptor(keyAndIvBytes, keyAndIvBytes), CryptoStreamMode.Write);
//perform encryption
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
//get encrypted stream into byte array
Byte[] outBytes = memoryStream.ToArray();
//close streams
memoryStream.Close();
cryptoStream.Close();
AES.Clear();
return outBytes;
}
Décodage et méthodes Déchiffrement
public string DecodeAndDecrypt(string strIn)
{
//** HEX decoding then AES decryption, CBC blocking with PKCS5 padding - DEFAULT **
string DecodeAndDecrypt = aesDecrypt(StringToByteArray(strIn.Substring(1)));
return (DecodeAndDecrypt);
}
private string aesDecrypt(Byte[] inputBytes)
{
RijndaelManaged AES = new RijndaelManaged();
Byte[] keyAndIvBytes = UTF8Encoding.UTF8.GetBytes("tR7nR6wZHGjYMCuV");
Byte[] outputBytes = inputBytes;//Convert.FromBase64String(inputBytes);
//set the mode, padding and block size
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CBC;
AES.KeySize = 128;
AES.BlockSize = 128;
//create streams and decryptor object
MemoryStream memoryStream = new MemoryStream(outputBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, AES.CreateEncryptor(keyAndIvBytes, keyAndIvBytes), CryptoStreamMode.Read);
//perform decryption
cryptoStream.Read(outputBytes, 0, outputBytes.Length);
Trace.WriteLine(outputBytes);
//close streams
memoryStream.Close();
cryptoStream.Close();
AES.Clear();
//return System.Text.Encoding.UTF8.GetString(outputBytes);
string plainText = Encoding.UTF8.GetString(outputBytes,
0,
outputBytes.Length);
return plainText;
}
Où avez-vous trouvé les spécifications? Peut-être que vous pouvez inclure un lien vers eux. –
Si vous faites le décryptage peut-être vous devriez utiliser 'AES.CreateDecryptor' au lieu de' AES.CreateEncryptor' –
Merci Greg, votre dernier commentaire a maintenant abouti à une chaîne lisible, mais encore quelques conneries à la fin pour une raison quelconque. Sorte d'évident maintenant je pense à ce sujet mais je ne pouvais pas le voir pour regarder. Je vais donner un autre coup pour analyser la ficelle et voir comment je m'entends. Merci beaucoup, Steve. –