rijndael encryption - only part of the string is decrypted

后端 未结 3 454
温柔的废话
温柔的废话 2021-01-14 21:06

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          


        
相关标签:
3条回答
  • 2021-01-14 21:35

    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).

    0 讨论(0)
  • 2021-01-14 21:54

    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());
        }
    }
    
    0 讨论(0)
  • 2021-01-14 21:57

    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;
    }
    
    0 讨论(0)
提交回复
热议问题