Je veux stocker une chaîne cryptée dans une base de données SQL en tant que tableau d'octets et je ne peux pas comprendre ce que je fais mal. Le code est le suivant:Cryptage et décryptage d'une chaîne à l'aide d'Aes Encryption - C#
private void loginBtn_Click(object sender, EventArgs e)
{
try
{
string password = passwordBox.Text.ToString();
using (Aes algorithm = Aes.Create())
{
byte[] encryptedPassword = EncryptString(password, algorithm.Key, algorithm.IV);
string roundTrip = DecryptString(encryptedPassword, algorithm.Key, algorithm.IV);
MessageBox.Show("Encrypted Password: " + encryptedPassword.ToString() + '\n' + "Round Trip: " + roundTrip.ToString());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Et le code utilisé pour le « EncryptString » et « DecryptString » est celui de Microsoft's Aes Class Reference (l'exemple situé à la fin de la page).
J'ai exécuté mon code et tout cela me donne dans une boîte de message est le suivant:
Encrypted Password: System.Byte[]
Round Trip: (empty space)
static byte[] EncryptString(string str, byte[] key, byte[] IV)
{
if (str == null || str.Length <= 0)
throw new ArgumentNullException("string");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
using (Aes algorithm = Aes.Create())
{
algorithm.Key = key;
algorithm.IV = IV;
ICryptoTransform encryptor = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(str);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
static string DecryptString(byte[] cipher, byte[] key, byte[] IV)
{
if (cipher == null || cipher.Length <= 0)
throw new ArgumentNullException("cipher");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
string decrypted;
using (Aes algorithm = Aes.Create())
{
algorithm.Key = key;
algorithm.IV = IV;
ICryptoTransform decryptor = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV);
using (MemoryStream msDecrypt = new MemoryStream())
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
decrypted = srDecrypt.ReadToEnd();
}
}
}
}
return decrypted;
}
Quelqu'un peut-il me aider à résoudre ce, s'il vous plaît?
P.S. La zone de texte a le mot de passe Char setted à *
Fonctionne bien pour moi. Postez votre code de cryptage/décryptage réel s'il vous plaît. –
Ce programme fonctionne correctement pour moi aussi - avec 'string password =" ABCD "', roundTrip alco contient "ABCD". Qu'avez-vous essayé comme mot de passe? Avez-vous modifié les méthodes 'EncryptString' et' DecryptString' de l'exemple de Microsoft? –
Vous verrez mieux si vous changez votre ligne de message à MessageBox.Show ("Mot de passe crypté:" + Encoding.ASCII.GetString (encryptedPassword) + '\ n' + "Round Trip:" + roundTrip); ou quelque chose comme (peut-être UTF au lieu de ACII, dépend de votre jeu de caractères). –