uso questo codice per crittografare una stringa (sostanzialmente, questo è l'esempio dato sulla Rijndael class on MSDN):codifica da utilizzare per convertire byte array String e viceversa
public static String AESEncrypt(String str2Encrypt, Byte[] encryptionKey, Byte[] IV)
{
Byte[] encryptedText;
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
// Use the provided key and IV
rijAlg.Key = encryptionKey;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption
using (MemoryStream msEncrypt = new MemoryStream())
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
// Write all data to the stream
swEncrypt.Write(str2Encrypt);
}
encryptedText = msEncrypt.ToArray();
}
}
return Encoding.Default.GetString(encryptedText);
}
uso Encoding.Default
convertire un array di byte in una stringa ma non sono sicuro che sia una buona soluzione. Il mio obiettivo è archiviare il testo crittografato (come le password ...) nei file. Devo continuare con Encoding.Default
o utilizzare Encoding.UTF8Encoding
o qualcos'altro?
Può avere conseguenze negative sui valori memorizzati quando provo a crittografarli e decrittografarli se i file vengono spostati su sistemi operativi diversi?
Grazie Jon. Mi salva da bug gravi. – Otiel
Vedi anche [Post di Phil Haack che espande su questo] (http://feeds.haacked.com/~r/haacked/~3/5hyXGYRO0Bo/hazards-of-converting-binary-data-to-a-string.aspx) –
Vedere anche http://stackoverflow.com/questions/646974/is-there-a-standard-technique-for-packing-binary-data-into-a-utf-16-string –