3
Answers

Encryption and decryption

Punit Sharma

Punit Sharma

9y
408
1
Dear All,
 
         I am working with WCF rest Services that are created for mobile app in anroid, I want that when app team send me the password in encrypted mode then how i decrypted  it in wcf method , Is there any common way to encrypted password using anroid and then i decrypted it in wcf service method.
Please provide me a way to achieve this,
 
 
Thanks, 
Answers (3)
1
Ananth G

Ananth G

NA 2.8k 125.1k 8y
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
Bikesh Srivastava

Bikesh Srivastava

NA 19.8k 835.1k 8y


  1. Use this code ,its working fine..

  2. privatestringEncrypt(stringclearText)
  3. {
  4. stringEncryptionKey="MAKV2SPBNI99212";
  5. byte[]clearBytes=Encoding.Unicode.GetBytes(clearText);
  6. using(Aesencryptor=Aes.Create())
  7. {
  8. Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(EncryptionKey,newbyte[]{0x49,0x76,0x61,0x6e,0x20,0x4d,0x65,0x64,0x76,0x65,0x64,0x65,0x76});
  9. encryptor.Key=pdb.GetBytes(32);
  10. encryptor.IV=pdb.GetBytes(16);
  11. using(MemoryStreamms=newMemoryStream())
  12. {
  13. using(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateEncryptor(),CryptoStreamMode.Write))
  14. {
  15. cs.Write(clearBytes,0,clearBytes.Length);
  16. cs.Close();
  17. }
  18. clearText=Convert.ToBase64String(ms.ToArray());
  19. }
  20. }
  21. returnclearText;
  22. }
  23. privatestringDecrypt(stringcipherText)
  24. {
  25. stringEncryptionKey="MAKV2SPBNI99212";
  26. byte[]cipherBytes=Convert.FromBase64String(cipherText);
  27. using(Aesencryptor=Aes.Create())
  28. {
  29. Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(EncryptionKey,newbyte[]{0x49,0x76,0x61,0x6e,0x20,0x4d,0x65,0x64,0x76,0x65,0x64,0x65,0x76});
  30. encryptor.Key=pdb.GetBytes(32);
  31. encryptor.IV=pdb.GetBytes(16);
  32. using(MemoryStreamms=newMemoryStream())
  33. {
  34. using(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateDecryptor(),CryptoStreamMode.Write))
  35. {
  36. cs.Write(cipherBytes,0,cipherBytes.Length);
  37. cs.Close();
  38. }
  39. cipherText=Encoding.Unicode.GetString(ms.ToArray());
  40. }
  41. }
  42. returncipherText;
  43. }
0
K P  Singh Chundawat

K P Singh Chundawat

NA 1.1k 104.1k 9y
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. }