Skip to content
Loading
Encryption and decryption
  • Ananth G
    Encryption
    • Sometimes we want to encrypt the values that time we use MD5 Encryption and Decryption
    • Here i give the code for Encryption and Decryption.
    passPhrase ,saltValue ,initVector this variables are the key for Encryption and Decryption
    public static string passPhrase = "YOUR KEY1";
    public static string saltValue = "YOUR KE2";
    public static string initVector = "YOUR KE3";
    public static string FunForEncrypt(string objText)
    {
    int keySize = 256;
    int passwordIterations = 03;
    string hashAlgorithm = "MD5";
    byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
    byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
    byte[] plainTextBytes = Encoding.UTF8.GetBytes(objText);
    PasswordDeriveBytes password = new PasswordDeriveBytes
    (
    passPhrase,
    saltValueBytes,
    hashAlgorithm,
    passwordIterations
    );
    byte[] keyBytes = password.GetBytes(keySize / 8);
    RijndaelManaged symmetricKey = new RijndaelManaged();
    symmetricKey.Mode = CipherMode.CBC;
    ICryptoTransform encryptor = symmetricKey.CreateEncryptor
    (
    keyBytes,
    initVectorBytes
    );
    MemoryStream memoryStream = new MemoryStream();
    CryptoStream cryptoStream = new CryptoStream
    (
    memoryStream,
    encryptor,
    CryptoStreamMode.Write
    );
    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
    cryptoStream.FlushFinalBlock();
    byte[] cipherTextBytes = memoryStream.ToArray();
    memoryStream.Close();
    cryptoStream.Close();
    string cipherText = Convert.ToBase64String(cipherTextBytes);
    cipherText = HttpUtility.UrlEncode(cipherText);
    return cipherText;
    }
    Decryption :
    • this below code used to Decrypt the text
    public static string FunForDecrypt(string cipherText)
    {
    string plainText = "";
    int keySize = 256;
    int passwordIterations = 03;
    string hashAlgorithm = "MD5";
    try
    {
    cipherText = HttpUtility.UrlDecode(cipherText);
    byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
    byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
    byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
    PasswordDeriveBytes password = new PasswordDeriveBytes
    (
    passPhrase,
    saltValueBytes,
    hashAlgorithm,
    passwordIterations
    );
    byte[] keyBytes = password.GetBytes(keySize / 8);
    RijndaelManaged symmetricKey = new RijndaelManaged();
    symmetricKey.Mode = CipherMode.CBC;
    ICryptoTransform decryptor = symmetricKey.CreateDecryptor
    (
    keyBytes,
    initVectorBytes
    );
    MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
    CryptoStream cryptoStream = new CryptoStream
    (
    memoryStream,
    decryptor,
    CryptoStreamMode.Read
    );
    byte[] plainTextBytes = new byte[cipherTextBytes.Length];
    int decryptedByteCount = cryptoStream.Read
    (
    plainTextBytes,
    0,
    plainTextBytes.Length
    );
    memoryStream.Close();
    cryptoStream.Close();
    plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
    }
    catch (Exception ex)
    {
    plainText = "";
    }
    return plainText;
    }
    0
  • K P  Singh Chundawat
    Yes . You can . 
     The best way is using your secret key you can do encryption and decryption.Copy and Paste below code into you application try to run it.
     
    1. // This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.  
    2. // This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be  
    3. // 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array.  
    4. private static readonly byte[] initVectorBytes = Encoding.ASCII.GetBytes("tu89geji340t89u2");  
    5. // This constant is used to determine the keysize of the encryption algorithm.  
    6. private const int keysize = 256;  
    7. //Here passPhrase is your secret key. Which can be anything. but should be similar in both ecryption and decryption  
    8. public static string Encrypt(string plainText, string passPhrase)  
    9. {  
    10. try  
    11. {  
    12. byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);  
    13. using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))  
    14. {  
    15. byte[] keyBytes = password.GetBytes(keysize / 8);  
    16. using (RijndaelManaged symmetricKey = new RijndaelManaged())  
    17. {  
    18. symmetricKey.Mode = CipherMode.CBC;  
    19. using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))  
    20. {  
    21. using (MemoryStream memoryStream = new MemoryStream())  
    22. {  
    23. using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))  
    24. {  
    25. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);  
    26. cryptoStream.FlushFinalBlock();  
    27. byte[] cipherTextBytes = memoryStream.ToArray();  
    28. return Convert.ToBase64String(cipherTextBytes);  
    29. }  
    30. }  
    31. }  
    32. }  
    33. }  
    34. }  
    35. catch (Exception)  
    36. {  
    37. return string.Empty;  
    38. }  
    39. }  
    40. public static string Decrypt(string cipherText, string passPhrase)  
    41. {  
    42. try  
    43. {  
    44. byte[] cipherTextBytes = Convert.FromBase64String(cipherText);  
    45. using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))  
    46. {  
    47. byte[] keyBytes = password.GetBytes(keysize / 8);  
    48. using (RijndaelManaged symmetricKey = new RijndaelManaged())  
    49. {  
    50. symmetricKey.Mode = CipherMode.CBC;  
    51. using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))  
    52. {  
    53. using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))  
    54. {  
    55. using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))  
    56. {  
    57. byte[] plainTextBytes = new byte[cipherTextBytes.Length];  
    58. int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);  
    59. return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);  
    60. }  
    61. }  
    62. }  
    63. }  
    64. }  
    65. }  
    66. catch (Exception)  
    67. {  
    68. return string.Empty;  
    69. }  
    70. }  
    71. }  
     
    0