Question

Only part of the string is getting decrypted, i think it has to do with my encoding.

Here is what happens:

        string s = "The brown fox jumped over the green frog";
        string k = "urieurut";
        string enc = EncryptString(s, k);
        string dec = DecryptString(enc, k);

The RESULT is this: The brown fox juϼ㴘裴혽Ή⪻ㆉr th≸ g⟤een frog

public static string EncryptString(string stringToEncrypt, string encryptionKey)
{
    string encrypted = String.Empty;

    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(encryptionKey);

    RijndaelManaged RMCrypto = new RijndaelManaged();
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);

    byte[] encryptedString = UE.GetBytes(stringToEncrypt);
    cs.Write(encryptedString, 0, encryptedString.Length);
    cs.FlushFinalBlock();
    cs.Close();

    encrypted = UE.GetString(ms.ToArray());
    return encrypted;
}

public static string DecryptString(string stringToDecrypt, string encryptionKey)
{
    string decrypted = String.Empty;

    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(encryptionKey);
    byte[] data = UE.GetBytes(stringToDecrypt);

    RijndaelManaged RMCrypto = new RijndaelManaged();
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Write);
    cs.Write(data, 0, data.Length);
    cs.FlushFinalBlock();
    cs.Close();

    decrypted = UE.GetString(ms.ToArray());

    return decrypted;
}
Was it helpful?

Solution 2

I solved my issue by using base64 string for the encryption - i may look at other options but i only needed these methods for a small amount of data, here is the final code:

public static string EncryptString(string stringToEncrypt, string encryptionKey)
{
    string encrypted = String.Empty;
    byte[] key = Encoding.Unicode.GetBytes(encryptionKey);

    RijndaelManaged RMCrypto = new RijndaelManaged();
    RMCrypto.Padding = PaddingMode.PKCS7;
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);

    byte[] encryptedString = Encoding.ASCII.GetBytes(stringToEncrypt);
    cs.Write(encryptedString, 0, encryptedString.Length);
    cs.FlushFinalBlock();
    cs.Close();

    //encrypted = Encoding.ASCII.GetString(ms.ToArray());
    return Convert.ToBase64String(ms.ToArray());
}

public static string DecryptString(string stringToDecrypt, string encryptionKey)
{
    string decrypted = String.Empty;
    byte[] key = Encoding.Unicode.GetBytes(encryptionKey);
    byte[] data = Convert.FromBase64String(stringToDecrypt);

    RijndaelManaged RMCrypto = new RijndaelManaged();
    RMCrypto.Padding = PaddingMode.PKCS7;
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Write);
    cs.Write(data, 0, data.Length);
    cs.FlushFinalBlock();
    cs.Close();

    decrypted = Encoding.ASCII.GetString(ms.ToArray());

    return decrypted;
}

OTHER TIPS

Here you go:

    string s = "The brown fox jumped over the green frog";
    string k = "urieurut";
    byte[] enc = EncryptString(s, k);
    string dec = DecryptString(enc, k);

You can't attempt to interpret an encrypted bunch of bytes as a Unicode string. Keep them as bytes. The decrypted version can be converted back to string.

Also note the disposing of disposable objects below. You could wind up with some resources being held too long or leak if you don't release them properly with using() or Dispose().

public static byte[] EncryptString(string stringToEncrypt, string encryptionKey)
{
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(encryptionKey);

    using (RijndaelManaged RMCrypto = new RijndaelManaged())
    using (MemoryStream ms = new MemoryStream())
    using (ICryptoTransform encryptor = RMCrypto.CreateEncryptor(key, key))
    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
    {
        byte[] encryptedString = UE.GetBytes(stringToEncrypt);
        cs.Write(encryptedString, 0, encryptedString.Length);
        cs.FlushFinalBlock();
        return ms.ToArray();
    }
}

public static string DecryptString(byte[] stringToDecrypt, string encryptionKey)
{
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(encryptionKey);

    using (RijndaelManaged RMCrypto = new RijndaelManaged())
    using (MemoryStream ms = new MemoryStream())
    using (ICryptoTransform decryptor = RMCrypto.CreateDecryptor(key, key))
    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
    {
        cs.Write(stringToDecrypt, 0, stringToDecrypt.Length);
        cs.FlushFinalBlock();
        return UE.GetString(ms.ToArray());
    }
}

Not sure about your specific code chunk, but Jeff Atwood did a nice little library that I've used before:

http://www.codeproject.com/KB/security/SimpleEncryption.aspx

It's worth a look as it simplifies the process of encrypting things a lot, I actually had to port to C# as there wasn't a port available when I saw it. However there is now a C# port (in the comments section).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top