Encrypting & Decrypting a String in C#

Encrypting & Decrypting a String in C#

Encrypting and decrypting a string in C# can be achieved using various cryptographic algorithms. Here's an example using the Advanced Encryption Standard (AES) algorithm:

 using System; using System.IO; using System.Security.Cryptography; using System.Text; public static class Crypto { private static readonly byte[] Salt = Encoding.ASCII.GetBytes("Replace with your own salt"); public static string EncryptString(string plainText, string sharedSecret) { if (string.IsNullOrEmpty(plainText)) throw new ArgumentNullException(nameof(plainText)); if (string.IsNullOrEmpty(sharedSecret)) throw new ArgumentNullException(nameof(sharedSecret)); string outStr; RijndaelManaged aesAlg = null; try { var key = new Rfc2898DeriveBytes(sharedSecret, Salt); aesAlg = new RijndaelManaged { Key = key.GetBytes(aesAlg.KeySize / 8), IV = key.GetBytes(aesAlg.BlockSize / 8) }; var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); using (var msEncrypt = new MemoryStream()) { using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (var swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } outStr = Convert.ToBase64String(msEncrypt.ToArray()); } } } finally { aesAlg?.Dispose(); } return outStr; } public static string DecryptString(string cipherText, string sharedSecret) { if (string.IsNullOrEmpty(cipherText)) throw new ArgumentNullException(nameof(cipherText)); if (string.IsNullOrEmpty(sharedSecret)) throw new ArgumentNullException(nameof(sharedSecret)); RijndaelManaged aesAlg = null; string plaintext; try { var key = new Rfc2898DeriveBytes(sharedSecret, Salt); var bytes = Convert.FromBase64String(cipherText); aesAlg = new RijndaelManaged { Key = key.GetBytes(aesAlg.KeySize / 8), IV = key.GetBytes(aesAlg.BlockSize / 8) }; var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (var msDecrypt = new MemoryStream(bytes)) { using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (var srDecrypt = new StreamReader(csDecrypt)) { plaintext = srDecrypt.ReadToEnd(); } } } } finally { aesAlg?.Dispose(); } return plaintext; } } 

To encrypt a string, call the EncryptString method with the plain text and a shared secret. The method returns the encrypted string in Base64-encoded format.

 string plainText = "This is a secret message."; string sharedSecret = "Replace with your own shared secret"; string encryptedText = Crypto.EncryptString(plainText, sharedSecret); 

To decrypt the string, call the DecryptString method with the encrypted string and the same shared secret.

 string decryptedText = Crypto.DecryptString(encryptedText, sharedSecret); 

Examples

  1. "Encrypt and Decrypt a String using AES in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string encryptedText = AESHelper.Encrypt(originalText, "YourSecretKey"); string decryptedText = AESHelper.Decrypt(encryptedText, "YourSecretKey"); 
    • Description: Illustrates how to encrypt and decrypt a string using the Advanced Encryption Standard (AES) algorithm in C#.

  2. "Encrypt and Decrypt a String using RSA in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string encryptedText = RSAHelper.Encrypt(originalText, publicKey); string decryptedText = RSAHelper.Decrypt(encryptedText, privateKey); 
    • Description: Demonstrates how to encrypt and decrypt a string using the RSA algorithm in C# for secure communication.

  3. "Encrypt and Decrypt a String using Triple DES in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string encryptedText = TripleDESHelper.Encrypt(originalText, "YourSecretKey"); string decryptedText = TripleDESHelper.Decrypt(encryptedText, "YourSecretKey"); 
    • Description: Shows how to use the Triple Data Encryption Standard (3DES) algorithm to encrypt and decrypt a string in C#.

  4. "Encrypt and Decrypt a String using DES in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string encryptedText = DESHelper.Encrypt(originalText, "YourSecretKey"); string decryptedText = DESHelper.Decrypt(encryptedText, "YourSecretKey"); 
    • Description: Illustrates how to use the Data Encryption Standard (DES) algorithm to encrypt and decrypt a string in C#.

  5. "Encrypt and Decrypt a String using Base64 in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string base64Encoded = Base64Helper.Encode(originalText); string base64Decoded = Base64Helper.Decode(base64Encoded); 
    • Description: Demonstrates how to use Base64 encoding and decoding to represent and retrieve a string in a secure manner.

  6. "Encrypt and Decrypt a String using XOR in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string encryptedText = XORHelper.Encrypt(originalText, "YourKey"); string decryptedText = XORHelper.Decrypt(encryptedText, "YourKey"); 
    • Description: Shows a simple example of using the XOR operation for encrypting and decrypting a string in C#.

  7. "Encrypt and Decrypt a String using MD5 in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string hashedText = MD5Helper.ComputeHash(originalText); 
    • Description: Demonstrates how to use the MD5 hashing algorithm to create a one-way hash of a string for secure storage or verification.

  8. "Encrypt and Decrypt a String using SHA-256 in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string hashedText = SHA256Helper.ComputeHash(originalText); 
    • Description: Illustrates how to use the SHA-256 hashing algorithm to create a one-way hash of a string in C# for enhanced security.

  9. "Encrypt and Decrypt a String using HMAC in C#"

    • Code Implementation:

      string originalText = "Hello, World!"; string hmacKey = "YourHMACKey"; string hmacHash = HMACHelper.ComputeHash(originalText, hmacKey); 
    • Description: Demonstrates how to use the Hash-based Message Authentication Code (HMAC) algorithm to create a hash of a string with a secret key for integrity verification.

  10. "Encrypt and Decrypt a String using SecureString in C#"

    • Code Implementation:

      SecureString originalSecureString = new NetworkCredential("", "Hello, World!").SecurePassword; string encryptedText = SecureStringHelper.Encrypt(originalSecureString, "YourSecretKey"); SecureString decryptedSecureString = SecureStringHelper.Decrypt(encryptedText, "YourSecretKey"); 
    • Description: Shows how to use the SecureString class in C# for more secure handling of sensitive information, such as passwords, by encrypting and decrypting using a key.


More Tags

dependencies resx eigenvector pem prettier python-unicode zpl broadcast pymssql local-variables

More C# Questions

More Electronics Circuits Calculators

More Livestock Calculators

More Dog Calculators

More Weather Calculators