Je dois crypter/décrypter certaines informations sensibles dans un fichier XML? Oui, je peux le faire en écrivant mes propres algorithmes personnalisés. Je me demande s'il y a déjà une manière intégrée dans. NET pour faire cela et aussi quels points j'ai toujours besoin de prendre soin ..Comment crypter une chaîne dans .NET?
13
A
Répondre
24
est ici quelques fonctions qui utilisent le framework .NET pour crypter et décrypter une chaîne:
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;
}
Bien sûr, je ne conseille pas hardcoding le vecteur clé et initialisation comme celui-ci :)
10
Vous voudrez probablement plonger dans l'espace de noms System.Security.Cryptography
. Je suppose que les articles Cryptography Overview, Encrypting Data et Decrypting Data à MSDN pourraient être de bons démarreurs.
Questions connexes
- 1. iPhone: Comment crypter une chaîne
- 2. Crypter une chaîne en Python
- 3. Comment crypter une chaîne à l'aide d'InstallScript
- 4. Comment je peux crypter une chaîne dans C#?
- 5. Crypter/Décrypter Chaîne
- 6. Comment crypter la chaîne de connexion Sql?
- 7. Comment crypter la chaîne de connexion dans SubSonic 3.0
- 8. Comment formater conditionnellement une chaîne dans .Net?
- 9. Comment crypter la chaîne de connexion sans app.config
- 10. Crypter/Déchiffrer Sql Server 2005 colonne cryptée dans .Net
- 11. Comment crypter une colonne spécifique dans une table MySQL?
- 12. Crypter un fichier avec RSA dans Visual Basic .NET
- 13. Comment crypter/décrypter une chaîne de texte en utilisant 3DES dans java?
- 14. Comment crypter chaîne sans mcrypt bibliothèque en php
- 15. chiffrement chaîne dans un assembly .NET
- 16. Comment crypter pour iphone
- 17. Comment crypter/décrypter l'url en C#
- 18. Comment analyser XML dans une chaîne dans .NET?
- 19. Comment crypter avec JSF
- 20. Comment convertir une chaîne C++ en chaîne .NET ^?
- 21. Comment crypter le texte dans une image en utilisant python
- 22. php crypter et décrypter
- 23. Comment crypter dans VBScript en utilisant AES?
- 24. Crypter PBEWithMD5AndDES dans J2ME
- 25. comment masquer une seule chaîne dans l'assemblage .net
- 26. Comment crypter des données dans le serveur SQL et décrypter dans les applications .net
- 27. comment crypter le dossier dans C#?
- 28. Convertir une chaîne en .net
- 29. vs Chaîne dans .NET
- 30. Comment chaîne/nid Transforme dans .NET XSL
" ABC "&" 123 "sont des longueurs invalides pour un tableau char-base 64. – JeffO
Il était seulement destiné à être une illustration, mais juste point;) – Cocowalla
Juste pour ajouter de la valeur pour les autres visiteurs ici - la longueur de la clé et IV peut être de 24 caractères. Par exemple: "keJhDo9YvJsp01j4JUdVuE ==" –