using System.Security.Cryptography;
public sealed class MyCryptoClass
public static void Main(string[] args)
{ Console.WriteLine("a");
MyCryptoClass mcc = MyCryptoClass.Instance;
string encryptedText = mcc.EncryptText("This is a plain text which needs to be encrypted...");
Console.WriteLine("Encrypted text (base64): " + encryptedText);
string decryptedText = mcc.DecryptText(encryptedText);
Console.WriteLine("Decrypted text: " + decryptedText);
string javaEncryptedText = "mL4ajZtdRgD8CtGSfJGkT24Ebw4SrGUGKQI6bvBw1ziCO/J7SeLiyIw41zumTHMMD9GOYK+kR79CVcpoaHT9TQ==";
Console.WriteLine("Encrypted text from Java (base64): " + javaEncryptedText);
string javaDecryptedText = mcc.DecryptText(javaEncryptedText);
Console.WriteLine("Decrypted text from Java: " + javaDecryptedText);
public RijndaelManaged myRijndael;
private static string encryptionKey = "A7zb534OPq59gU7q";
private static string salt = "JV5k9GoH";
private static byte[] initialisationVector = Encoding.UTF8.GetBytes("l4iG63jN9Dcg6537");
private static byte[] secretKey = GetSecretKey();
private static byte[] GetSecretKey()
string hashedKey = GetHashedKey();
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
using (Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(hashedKey, saltBytes, iterations))
secretKey = rfc2898.GetBytes(16);
private static string GetHashedKey()
string hashBase64 = String.Empty;
using (SHA1Managed sha1 = new SHA1Managed())
byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(encryptionKey));
hashBase64 = Base64ThirdPartAndroid(hash, true);
private static string Base64ThirdPartAndroid(byte[] arr, bool withoutPadding)
string base64String = System.Convert.ToBase64String(arr);
if (withoutPadding) base64String = base64String.TrimEnd('=');
public static readonly MyCryptoClass _instance = new MyCryptoClass();
public static MyCryptoClass Instance
get { return _instance; }
public string DecryptText(string encryptedString)
using (myRijndael = new RijndaelManaged())
myRijndael.Key = Convert.FromBase64String(encryptionKey);
myRijndael.IV = new byte[16];
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
Byte[] ourEnc = Convert.FromBase64String(encryptedString);
string ourDec = DecryptStringFromBytes(ourEnc, myRijndael.Key, myRijndael.IV);
public string EncryptText(string plainText)
using (myRijndael = new RijndaelManaged())
myRijndael.Key = HexStringToByte(encryptionKey);
Console.WriteLine(initialisationVector);
myRijndael.IV = HexStringToByte("l4iG63jN9Dcg6537");
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
byte[] encrypted = EncryptStringToBytes(plainText, myRijndael.Key, myRijndael.IV);
string encString = Convert.ToBase64String(encrypted);
public byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
using (RijndaelManaged rijAlg = new RijndaelManaged())
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
swEncrypt.Write(plainText);
encrypted = msEncrypt.ToArray();
public string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
using (RijndaelManaged rijAlg = new RijndaelManaged())
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
public static void GenerateKeyAndIV()
RijndaelManaged myRijndaelManaged = new RijndaelManaged();
myRijndaelManaged.Mode = CipherMode.CBC;
myRijndaelManaged.Padding = PaddingMode.PKCS7;
myRijndaelManaged.GenerateIV();
myRijndaelManaged.GenerateKey();
string newKey = ByteArrayToHexString(myRijndaelManaged.Key);
string newinitVector = ByteArrayToHexString(myRijndaelManaged.IV);
public static byte[] HexStringToByte(string hexString)
{Console.WriteLine(hexString);
int bytesCount = (hexString.Length) / 2;
byte[] bytes = new byte[bytesCount];
Console.WriteLine(bytesCount);
for (int x = 0; x < bytesCount; ++x)
Console.WriteLine(x+" --- "+ (Convert.ToByte(hexString.Substring(x * 2, 2), 16)));
bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
public static string ByteArrayToHexString(byte[] ba)
StringBuilder hex = new StringBuilder(ba.Length * 2);
hex.AppendFormat("{0:x2}", b);