Comment crypter et décrypter des données ?
Bonjour à tous,
Voilà, un truc simple, je cherche à crypter et décrypter des données sous forme de tableau de Byte avec c#.
J'ai pu voir par moi même que de nombreux algo de cryptage étaient présent, mais je n'arrive vraiment pas à m'en sortir.
J'ai implémenter ce code (pas mal copié de msdn)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| byte[] Encrypt(byte[] toEncode)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();
// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, DESCryptoServiceProvider.Create().CreateEncryptor(), CryptoStreamMode.Write);
// Create a StreamWriter to write a string
encStream.Write(toEncode, 0, toEncode.Length);
// Close the StreamWriter and CryptoStream.
encStream.Close();
// Get an array of bytes that represents
// the memory stream.
byte[] buffer = ms.ToArray();
// Close the memory stream.
ms.Close();
// Return the encrypted byte array.
return buffer;
}
byte[] Decrypt(byte[] toDecode)
{
// Create a memory stream to the passed buffer.
MemoryStream ms = new MemoryStream(toDecode);
Byte[] decode = new Byte[ms.ToArray().Length];
// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, DESCryptoServiceProvider.Create().CreateDecryptor(), CryptoStreamMode.Read);
encStream.Read(decode, 0, ms.ToArray().Length);
// Close the streams.
encStream.Close();
ms.Close();
return decode;
} |
Seulement à chaque fois que je tente de decrypter, quelque soit l'algo utilisé (ici DES) j'obtiens une "Bad Data" exception qui survient 8 Bytes avant la fin du Read. :aie:
L'exception n'est pas plus explicite que ça et je ne comprends pas d'où ça vient. :(
Si quelqu'un peut m'expliquer...
Merci
:mrgreen: