Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 968 characters in body
Source Link
keitn
  • 1.3k
  • 2
  • 21
  • 45
public static string Encrypt(string plaintext) { byte[] rgbIV = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 };rgbIV; byte[] key = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 };key;   //Specify the algorithms key & IV var rijndael = new RijndaelManaged  {BlockSizerijndael = 128, IV =BuildRigndaelCommon(out rgbIV, KeySize = 128, Key =out key, Padding = PaddingMode.None}); //convert plaintext into a byte array byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); int BlockSize; BlockSize = 16 * (1 + (plaintext.Length / 16)); Array.Resize(ref plaintextBytes, BlockSize); // fill the remaining space with 0 for (int i = plaintext.Length; i < BlockSize; i++) { plaintextBytes[i] = 0; }   byte[] cipherTextBytes = null;  //create uninitialized Rijndael encryption obj   using (RijndaelManaged symmetricKey = new RijndaelManaged();)  {   //Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj   var transform = rijndael.CreateEncryptor();   //Chaining mode   symmetricKey.Mode = CipherMode.CFB;   //create encryptor from the key and the IV value   ICryptoTransform encryptor = symmetricKey.CreateEncryptor(key, rgbIV);   //define memory stream to hold encrypted data   using (MemoryStream ms = new MemoryStream();)  {   //define cryptographic stream - contains the transformation key to be used and the mode   using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);)  {   //encrypt contents of cryptostream   cs.Write(plaintextBytes, 0, BlockSize);     cs.FlushFinalBlock(); //finish encryting   //convert encrypted data from a memory stream into a byte array   byte[]  cipherTextBytes = ms.ToArray();   //close all streams } ms.Close(); } cs.Close();} //store result as a hex value string hexOutput = BitConverter.ToString(cipherTextBytes).Replace("-", ""); hexOutput = hexOutput.Substring(0, plaintext.Length * 2); //finially return encrypted string return hexOutput; } 
 public static string Decrypt(string disguisedtext) { byte[] rgbIVrgbIV;  = new  byte[] {key;  0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8,BuildRigndaelCommon(out 0xArgbIV, 0xB,out 0xC,key);  0xD, 0xF, 0x10, 0x11, 0x12 };  byte[] keydisguishedtextBytes = newFromHexString(disguisedtext);  byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6,   0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10,string 0x11,visiabletext 0x12= }; ""; //Specify thecreate algorithmsuninitialized keyRijndael &encryption IVobj   using (var rijndaelsymmetricKey = new RijndaelManaged{BlockSize())  = 128, IV = rgbIV, KeySize = 128,{  Key = key, Padding = PaddingMode.PKCS7 };  //Call SymmetricAlgorithm.CreateEncryptor to byte[]create disguishedtextBytesthe =Encryptor FromHexString(disguisedtext); obj //create uninitialized Rijndael encryption objsymmetricKey.Mode = CipherMode.CFB; RijndaelManaged symmetricKey = new RijndaelManaged(); //create encryptor from the key and the IV //Callvalue  SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj  // ICryptoTransform symmetricKey.Modeencryptor = CipherModesymmetricKey.CFB; CreateEncryptor(key, rgbIV);     ICryptoTransform decryptor = symmetricKey.CreateDecryptor(key, rgbIV);   //define memory stream to hold encrypted data using (MemoryStream ms = new MemoryStream(disguishedtextBytes);)  {   //define cryptographic stream - contains the transformation to be used and the mode using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);)  {    byte[] plaintextBytes = new Byte[disguishedtextBytes.Length*2]; Length];   cs.Write(disguishedtextBytes, 0, disguishedtextBytes.Length);     cs.FlushFinalBlock(); int decryptedByteCount = cs.Read(plaintextBytes, 0, plaintextBytes.Length);  //convert decrypted data from a memory stream into a byte array   byte[] visiabletextBytes = ms.ToArray(); //close all streams  ms visiabletext = Encoding.CloseUTF8.GetString(visiabletextBytes); }  } cs.Close} return visiabletext; } 

Helper Methods:

 private static RijndaelManaged BuildRigndaelCommon(out byte[] rgbIV, out byte[] key) { rgbIV = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; stringkey plaintext= new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; //Specify the algorithms key & IV RijndaelManaged rijndael = Encoding.UTF8.GetString(plaintextBytesnew RijndaelManaged{BlockSize = 128, 0IV = rgbIV, decryptedByteCount)KeySize = 128, Key = key, Padding = PaddingMode.None};   return rijndael;   }  public static byte[] FromHexString(string visiabletexthexString) { if (hexString == null) { return new byte[0];  } var numberChars = Encoding.UTF8hexString.GetStringLength; var bytes = new byte[numberChars / 2]; for (visiabletextBytesvar i = 0; i < numberChars; i += 2);  { / bytes[i / Return2] decrypted= stringConvert.ToByte(hexString.Substring(i, 2), 16);   }  return plaintext;bytes; } 
public static string Encrypt(string plaintext) { byte[] rgbIV = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; byte[] key = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 };   //Specify the algorithms key & IV var rijndael = new RijndaelManaged  {BlockSize = 128, IV = rgbIV, KeySize = 128, Key = key, Padding = PaddingMode.None}; //convert plaintext into a byte array byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); int BlockSize; BlockSize = 16 * (1 + (plaintext.Length / 16)); Array.Resize(ref plaintextBytes, BlockSize); // fill the remaining space with 0 for (int i = plaintext.Length; i < BlockSize; i++) { plaintextBytes[i] = 0; } //create uninitialized Rijndael encryption obj RijndaelManaged symmetricKey = new RijndaelManaged(); //Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj var transform = rijndael.CreateEncryptor(); //Chaining mode symmetricKey.Mode = CipherMode.CFB; //create encryptor from the key and the IV value ICryptoTransform encryptor = symmetricKey.CreateEncryptor(key, rgbIV); //define memory stream to hold encrypted data MemoryStream ms = new MemoryStream(); //define cryptographic stream - contains the transformation key to be used and the mode CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write); //encrypt contents of cryptostream cs.Write(plaintextBytes, 0, BlockSize);   cs.FlushFinalBlock(); //finish encryting  //convert encrypted data from a memory stream into a byte array   byte[] cipherTextBytes = ms.ToArray();   //close all streams ms.Close(); cs.Close(); //store result as a hex value string hexOutput = BitConverter.ToString(cipherTextBytes).Replace("-", ""); hexOutput = hexOutput.Substring(0, plaintext.Length * 2); //finially return encrypted string return hexOutput; } 
 public static string Decrypt(string disguisedtext) { byte[] rgbIV = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; byte[] key = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 };  //Specify the algorithms key & IV var rijndael = new RijndaelManaged{BlockSize = 128, IV = rgbIV, KeySize = 128, Key = key, Padding = PaddingMode.PKCS7 };  byte[] disguishedtextBytes = FromHexString(disguisedtext);  //create uninitialized Rijndael encryption obj RijndaelManaged symmetricKey = new RijndaelManaged();  //Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj  symmetricKey.Mode = CipherMode.CFB;     ICryptoTransform decryptor = symmetricKey.CreateDecryptor(key, rgbIV); MemoryStream ms = new MemoryStream(disguishedtextBytes); CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write); byte[] plaintextBytes = new Byte[disguishedtextBytes.Length*2];  cs.Write(disguishedtextBytes, 0, disguishedtextBytes.Length);   cs.FlushFinalBlock(); int decryptedByteCount = cs.Read(plaintextBytes, 0, plaintextBytes.Length);  byte[] visiabletextBytes = ms.ToArray(); //close all streams  ms.Close(); cs.Close(); string plaintext = Encoding.UTF8.GetString(plaintextBytes, 0, decryptedByteCount); string visiabletext = Encoding.UTF8.GetString(visiabletextBytes); // Return decrypted string. return plaintext; } 
public static string Encrypt(string plaintext) { byte[] rgbIV; byte[] key; RijndaelManaged rijndael = BuildRigndaelCommon(out rgbIV, out key); //convert plaintext into a byte array byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); int BlockSize; BlockSize = 16 * (1 + (plaintext.Length / 16)); Array.Resize(ref plaintextBytes, BlockSize); // fill the remaining space with 0 for (int i = plaintext.Length; i < BlockSize; i++) { plaintextBytes[i] = 0; }   byte[] cipherTextBytes = null;  //create uninitialized Rijndael encryption obj   using (RijndaelManaged symmetricKey = new RijndaelManaged())  {   //Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj   var transform = rijndael.CreateEncryptor();   //Chaining mode   symmetricKey.Mode = CipherMode.CFB;   //create encryptor from the key and the IV value   ICryptoTransform encryptor = symmetricKey.CreateEncryptor(key, rgbIV);   //define memory stream to hold encrypted data   using (MemoryStream ms = new MemoryStream())  {   //define cryptographic stream - contains the transformation key to be used and the mode   using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))  {   //encrypt contents of cryptostream   cs.Write(plaintextBytes, 0, BlockSize);   cs.FlushFinalBlock();   //convert encrypted data from a memory stream into a byte array   cipherTextBytes = ms.ToArray();  }  } } //store result as a hex value string hexOutput = BitConverter.ToString(cipherTextBytes).Replace("-", ""); hexOutput = hexOutput.Substring(0, plaintext.Length * 2); //finially return encrypted string return hexOutput; } 
 public static string Decrypt(string disguisedtext) { byte[] rgbIV;    byte[] key;  BuildRigndaelCommon(out rgbIV, out key);    byte[] disguishedtextBytes = FromHexString(disguisedtext);     string visiabletext = ""; //create uninitialized Rijndael encryption obj   using (var symmetricKey = new RijndaelManaged())  {  //Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj symmetricKey.Mode = CipherMode.CFB; //create encryptor from the key and the IV value  // ICryptoTransform encryptor = symmetricKey.CreateEncryptor(key, rgbIV);  ICryptoTransform decryptor = symmetricKey.CreateDecryptor(key, rgbIV);   //define memory stream to hold encrypted data using (MemoryStream ms = new MemoryStream(disguishedtextBytes))  {   //define cryptographic stream - contains the transformation to be used and the mode using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))  {    byte[] plaintextBytes = new Byte[disguishedtextBytes.Length];   cs.Write(disguishedtextBytes, 0, disguishedtextBytes.Length);   cs.FlushFinalBlock();  //convert decrypted data from a memory stream into a byte array   byte[] visiabletextBytes = ms.ToArray();  visiabletext = Encoding.UTF8.GetString(visiabletextBytes); }  } } return visiabletext; } 

Helper Methods:

 private static RijndaelManaged BuildRigndaelCommon(out byte[] rgbIV, out byte[] key) { rgbIV = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; key = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; //Specify the algorithms key & IV RijndaelManaged rijndael = new RijndaelManaged{BlockSize = 128, IV = rgbIV, KeySize = 128, Key = key, Padding = PaddingMode.None};   return rijndael;   }  public static byte[] FromHexString(string hexString) { if (hexString == null) { return new byte[0];  } var numberChars = hexString.Length; var bytes = new byte[numberChars / 2]; for (var i = 0; i < numberChars; i += 2)  {  bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);   }  return bytes; } 
Source Link
keitn
  • 1.3k
  • 2
  • 21
  • 45

Decryption using Rijndael in C#

I have the following encryption method. I am not able to decrypt it. I have inherited the encryption algorithm so it cannot be changed.

public static string Encrypt(string plaintext) { byte[] rgbIV = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; byte[] key = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; //Specify the algorithms key & IV var rijndael = new RijndaelManaged {BlockSize = 128, IV = rgbIV, KeySize = 128, Key = key, Padding = PaddingMode.None}; //convert plaintext into a byte array byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); int BlockSize; BlockSize = 16 * (1 + (plaintext.Length / 16)); Array.Resize(ref plaintextBytes, BlockSize); // fill the remaining space with 0 for (int i = plaintext.Length; i < BlockSize; i++) { plaintextBytes[i] = 0; } //create uninitialized Rijndael encryption obj RijndaelManaged symmetricKey = new RijndaelManaged(); //Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj var transform = rijndael.CreateEncryptor(); //Chaining mode symmetricKey.Mode = CipherMode.CFB; //create encryptor from the key and the IV value ICryptoTransform encryptor = symmetricKey.CreateEncryptor(key, rgbIV); //define memory stream to hold encrypted data MemoryStream ms = new MemoryStream(); //define cryptographic stream - contains the transformation key to be used and the mode CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write); //encrypt contents of cryptostream cs.Write(plaintextBytes, 0, BlockSize); cs.FlushFinalBlock(); //finish encryting //convert encrypted data from a memory stream into a byte array byte[] cipherTextBytes = ms.ToArray(); //close all streams ms.Close(); cs.Close(); //store result as a hex value string hexOutput = BitConverter.ToString(cipherTextBytes).Replace("-", ""); hexOutput = hexOutput.Substring(0, plaintext.Length * 2); //finially return encrypted string return hexOutput; } 

As you can see it's pretty standard except at the end it's converted to hex and substring is performed. I'm having great difficulty doing the opposite.

My decrypt method is like:

 public static string Decrypt(string disguisedtext) { byte[] rgbIV = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; byte[] key = new byte[] { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xA, 0xB, 0xC, 0xD, 0xF, 0x10, 0x11, 0x12 }; //Specify the algorithms key & IV var rijndael = new RijndaelManaged{BlockSize = 128, IV = rgbIV, KeySize = 128, Key = key, Padding = PaddingMode.PKCS7 }; byte[] disguishedtextBytes = FromHexString(disguisedtext); //create uninitialized Rijndael encryption obj RijndaelManaged symmetricKey = new RijndaelManaged(); //Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj symmetricKey.Mode = CipherMode.CFB; ICryptoTransform decryptor = symmetricKey.CreateDecryptor(key, rgbIV); MemoryStream ms = new MemoryStream(disguishedtextBytes); CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write); byte[] plaintextBytes = new Byte[disguishedtextBytes.Length*2]; cs.Write(disguishedtextBytes, 0, disguishedtextBytes.Length); cs.FlushFinalBlock(); int decryptedByteCount = cs.Read(plaintextBytes, 0, plaintextBytes.Length); byte[] visiabletextBytes = ms.ToArray(); //close all streams ms.Close(); cs.Close(); string plaintext = Encoding.UTF8.GetString(plaintextBytes, 0, decryptedByteCount); string visiabletext = Encoding.UTF8.GetString(visiabletextBytes); // Return decrypted string. return plaintext; } 

I'm getting various errors regarding the length of the string and that the padding is invalid. Has anybody any ideas to get the decryption working. I've tried padding out the input string back to 32 bytes but no avail.