Devo crittografare/decifrare alcune informazioni sensibili in un file Xml? Sì, posso farlo scrivendo i miei algoritmi personalizzati. Mi chiedo se c'è già un modo integrato in .NET per farlo e anche quali punti ho sempre bisogno di fare attenzione ..Come crittografare una stringa in .NET?
13
A
risposta
24
Ecco un paio di funzioni che utilizzano il framework .NET di cifrare e decifrare una stringa:
public string EncryptString(string plainText)
{
// Instantiate a new RijndaelManaged object to perform string symmetric encryption
RijndaelManaged rijndaelCipher = new RijndaelManaged();
// Set key and IV
rijndaelCipher.Key = Convert.FromBase64String("ABC");
rijndaelCipher.IV = Convert.FromBase64String("123");
// Instantiate a new MemoryStream object to contain the encrypted bytes
MemoryStream memoryStream = new MemoryStream();
// Instantiate a new encryptor from our RijndaelManaged object
ICryptoTransform rijndaelEncryptor = rijndaelCipher.CreateEncryptor();
// Instantiate a new CryptoStream object to process the data and write it to the
// memory stream
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write);
// Convert the plainText string into a byte array
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
// Encrypt the input plaintext string
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
// Complete the encryption process
cryptoStream.FlushFinalBlock();
// Convert the encrypted data from a MemoryStream to a byte array
byte[] cipherBytes = memoryStream.ToArray();
// Close both the MemoryStream and the CryptoStream
memoryStream.Close();
cryptoStream.Close();
// Convert the encrypted byte array to a base64 encoded string
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
// Return the encrypted data as a string
return cipherText;
}
public string DecryptString(string cipherText)
{
// Instantiate a new RijndaelManaged object to perform string symmetric encryption
RijndaelManaged rijndaelCipher = new RijndaelManaged();
// Set key and IV
rijndaelCipher.Key = Convert.FromBase64String("ABC");
rijndaelCipher.IV = Convert.FromBase64String("123");
// Instantiate a new MemoryStream object to contain the encrypted bytes
MemoryStream memoryStream = new MemoryStream();
// Instantiate a new encryptor from our RijndaelManaged object
ICryptoTransform rijndaelDecryptor = rijndaelCipher.CreateDecryptor();
// Instantiate a new CryptoStream object to process the data and write it to the
// memory stream
CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelDecryptor, CryptoStreamMode.Write);
// Will contain decrypted plaintext
string plainText = String.Empty;
try
{
// Convert the ciphertext string into a byte array
byte[] cipherBytes = Convert.FromBase64String(cipherText);
// Decrypt the input ciphertext string
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
// Complete the decryption process
cryptoStream.FlushFinalBlock();
// Convert the decrypted data from a MemoryStream to a byte array
byte[] plainBytes = memoryStream.ToArray();
// Convert the encrypted byte array to a base64 encoded string
plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
}
finally
{
// Close both the MemoryStream and the CryptoStream
memoryStream.Close();
cryptoStream.Close();
}
// Return the encrypted data as a string
return plainText;
}
Naturalmente io non consiglio hardcoding la chiave e vettore di inizializzazione come questo :)
10
Probabilmente vorrai tuffarti nello spazio dei nomi System.Security.Cryptography
. Immagino che gli articoli Cryptography Overview, Encrypting Data e Decrypting Data a MSDN potrebbero essere buoni antipasti.
Problemi correlati
- 1. Come crittografare e decrittografare una stringa in python?
- 2. Come crittografare un file in .net e decifrare in android
- 3. come crittografare l'ID stringa di query in mvc4 ActionLink
- 4. Come convertire una stringa in base64 in .net framework 4
- 5. Come crittografare i dati in una stringa UTF-8 utilizzando OpenSSL :: Cipher?
- 6. Come crittografare ASP.NET 4.0 Web.Config?
- 7. Decodifica in Python di una stringa crittografata utilizzando .NET
- 8. Come crittografare la password
- 9. Come scrivere caratteri speciali ASCII in una stringa .net
- 10. Connection sicurezza stringa in un'applicazione desktop .net
- 11. Come convertire SID in stringa .net
- 12. Come crittografare una stringa/stream con bouncycastle pgp senza iniziare con un file
- 13. È possibile crittografare con chiave privata utilizzando .net RSACryptoServiceProvider?
- 14. Come crittografare i dati utente in Firebase
- 15. Heroku - Come crittografare il database
- 16. Come utilizzare il wrapper OpenSSL.Net C# per crittografare una stringa con AES?
- 17. Come crittografare un file .zip?
- 18. .NET Formatta una stringa con spazi fissi
- 19. Come posso crittografare 128 CFB in Ruby?
- 20. Come posso crittografare la password in log4j.properties?
- 21. Come crittografare le colonne in postgresql?
- 22. Converti schede in spazi in una stringa .NET
- 23. Creazione stringa URL in .NET
- 24. Come crittografare un file .jar
- 25. Reindirizzamento della console a una stringa in un programma .NET
- 26. Come crittografare il token di sicurezza JWT?
- 27. GnuPG: Come crittografare/decrittografare i file utilizzando una determinata chiave?
- 28. Ottieni parametri url da una stringa in .NET
- 29. Come cercare una stringa in un'altra stringa?
- 30. IllegalBlockSizeException quando si tenta di crittografare e decodificare una stringa con AES
" ABC "&" 123 "sono lunghezze non valide per un array di basi Base-64. – JeffO
Era solo un'illustrazione, ma un punto giusto;) – Cocowalla
Solo per aggiungere valore per gli altri visitatori qui - la lunghezza di entrambe le chiavi e IV può essere di 24 caratteri. Ad esempio: "keJhDo9YvJsp01j4JUdVuE ==" –