RijndaelManaged's question, how can I encrypt text more than 16bytes



Hi, I'm using RijndaelManaged to encrypt data.

Encode method is

public static string DecodeText(string encryptext, string key, byte[]
machinekey)
{
byte[] bufencrypt = S_Misc.HexStringToByteArray(encryptext);
byte[] bufkey = S_Misc.HexStringToByteArray(key);

RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform decryptor = myRijndael.CreateDecryptor(bufkey,
machinekey);

MemoryStream msDecrypt = new MemoryStream(bufencrypt);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read);

StreamReader sr = new StreamReader(csDecrypt);
return sr.ReadToEnd();


}

public static string EncodeText(string text, string key, byte[]
machinekey)
{
byte[] bufpass = Encoding.ASCII.GetBytes(text);
byte[] bufkey = S_Misc.HexStringToByteArray(key);
byte[] buffer = null;


RijndaelManaged myRijndael = new RijndaelManaged();

ICryptoTransform encryptor = myRijndael.CreateEncryptor(bufkey,
machinekey);

MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor,
CryptoStreamMode.Write);

csEncrypt.Write(bufpass, 0, bufpass.Length);
csEncrypt.FlushFinalBlock();

buffer = msEncrypt.ToArray();

return S_Misc.ByteArrayToHexString(buffer);

}



test method:

string key = "12345678901234567890123456789012";
string mkey = "09876543210987654321098765432101";
string zkey = "00000000000000000000000000000000";

byte[] bmkey = S_Misc.HexStringToByteArray(mkey);
byte[] zmkey = S_Misc.HexStringToByteArray(zkey);


string entxt0 = EncodeText("12345678",key,bmkey);
string entxt1 = EncodeText("123456781234",key,bmkey);
string entxt2 = EncodeText("1234567812345678",key,bmkey);
string entxt3 = EncodeText("123456781234567812345678",key,bmkey);
string entxt4 = EncodeText("12345678123456781234567812345678",key,bmkey);
string entxt5 =
EncodeText("1234567812345678123456781234567812345678",key,bmkey);
string entxt6 =
EncodeText("123456781234567812345678123456781234567812345678",key,bmkey);

string detxt0 = DecodeText(entxt0,key,zmkey);
string detxt1 = DecodeText(entxt1,key,zmkey);
string detxt2 = DecodeText(entxt2,key,zmkey);
string detxt3 = DecodeText(entxt3,key,zmkey);
string detxt4 = DecodeText(entxt4,key,zmkey);
string detxt5 = DecodeText(entxt5,key,zmkey);
string detxt6 = DecodeText(entxt6,key,zmkey);

entxt0 "9A5653C0957DFE7364AB2BC6E4F8CD56" string
entxt1 "D6D0D00A3560A95C2A4F33B764E5B002" string
entxt2 "0E7EC566DE6BC1889A0D81DE518B402DF00DD1E16370C35B19719D8D870EB29F"
string
entxt3 "0E7EC566DE6BC1889A0D81DE518B402D4A85DD6DB6736C47B3F62AD3A425006D"
string
entxt4
"0E7EC566DE6BC1889A0D81DE518B402DC5E87043FBA92D9FB2C88861B451C4B456624F675057E5C8F5A5AA0CC554C878"
string
entxt5
"0E7EC566DE6BC1889A0D81DE518B402DC5E87043FBA92D9FB2C88861B451C4B405550148A876CB65DBE40C71DDDBCFB6"
string
entxt6
"0E7EC566DE6BC1889A0D81DE518B402DC5E87043FBA92D9FB2C88861B451C4B4D3D5E88E9F070699CFE6CED4951BCECAA2703B46DBE4D6A03B2D71C204580886"
string
detxt0 "8Vw?" string
detxt1 "8Vw?]r:" string
detxt2 "8Vw?]r:Pu9" string
detxt3 "8Vw?]r:Pu912345678" string
detxt4 "8Vw?]r:Pu91234567812345678" string
detxt5 "8Vw?]r:Pu9123456781234567812345678" string
detxt6 "8Vw?]r:Pu912345678123456781234567812345678" string

Note: I use deferent IV to EncodeText,DecodeText
The results show if a text's length is more than 16 bytes, only the first 16
bytes are well encrypted.
I want to know , how can I encrypt the rest text?


Thanks in advance for any reply!
Sam




.



Relevant Pages

  • Re: Length of the data to decrypt is invalid
    ... I may be misunderstanding your code, but it looks like your decrypt function ... converting that back to binary use ASCII! ... You would then take the decrypted data and convert that back to a string ... ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Length of the data to decrypt is invalid
    ... In my case it is different, I just store the encrypted string in a Session ... /// Summary description for MyEncryption ... ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, ... ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Padding is invalid and cannot be removed.
    ... you have to use a lossless conversion to a string using Convert.ToBase64String ... public class MyEncryption: System.Web.UI.Page ... ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, ... ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • RE: Padding is invalid and cannot be removed.
    ... private string m_ciphertext; ... ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, ... ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Decryption adding funny characters
    ... and send them through the encryptor using base64 strings ... Even if I trim the decrypted output, the extra characters ... (string Text, byteKey, byteIV) ... CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); ...
    (microsoft.public.dotnet.security)