I am trying to make an encrypt/decrypt program in C# via AES. Here's my code:
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Security.Cryptography;
namespace encryptingApp
{
public class AES_Crypt
{
public static void Main ()
{
string text = "this-needs-to-be-encrypted";
string IV = "0000000000000000";
int ivBlockSize = 16;
string key = "00000000000000000000000000000000";
int keySize = 32;
string encriptedText = Encrypt(text,key,IV);
string decrypted = Decrypt(encriptedText, key, ivBlockSize);
}
public static string Encrypt(string clearText, string key, string iv )
{
byte[] textBytes=GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
encryptor.IV = GetBytes(iv);
encryptor.Key = GetBytes(key);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(encryptor.Key,encryptor.IV), CryptoStreamMode.Write))
{
cs.Write(textBytes, 0, textBytes.Length);
cs.Close();
}
string rv= iv + ByteToHex(ms.ToArray()).ToLower();
clearText = Base64Encode(rv);
}
}
return clearText;
}
public static string Decrypt(string encriptedText, string key, int ivBlockSize)
{
string decryptedText = null;
string fullText=Base64Decode(encriptedText);
string realIV = fullText.Substring( 0 , ivBlockSize );
string cypherText = fullText.Substring(ivBlockSize, fullText.Length - ivBlockSize - 1);
byte[] cypherTextInBytes = HexToByte(cypherText);
using (Aes decryptor = Aes.Create())
{
decryptor.Key = GetBytes(key);
decryptor.IV = GetBytes(realIV);
decryptor.Mode = CipherMode.CBC;
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, decryptor.CreateDecryptor(decryptor.Key,decryptor.IV), CryptoStreamMode.Read))
{
using (var sr = new StreamReader(cs))
{
decryptedText = sr.ReadToEnd();
}
}
}
}
return decryptedText;
}
static byte[] GetBytes(string str)
{
return System.Text.Encoding.UTF8.GetBytes(str);
}
static string GetString(byte[] bytes)
{
return System.Text.Encoding.UTF8.GetString(bytes);
}
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
public static string ByteToHex(byte[] ba)
{
return BitConverter.ToString(ba).Replace("-", "").ToLower();
}
public static byte[] HexToByte(string hex)
{
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length >> 1; ++i)
{
arr[i] = (byte)((GetHexVal(hex[i << 1]) << 4) + (GetHexVal(hex[(i << 1) + 1])));
}
return arr;
}
public static int GetHexVal(char hex)
{
int val = (int) hex;
return val - (val < 58 ? 48 : 87);
}
}
}
The Encrypt function does his job very well and returns me the correct encrypted text. The problem exists in the Decrypt function, everything goes well (I've been printing my vars on the screen) until the StreamReader uses .ReadToEnd(). I get a CryptographyException (i get the same exception twice in only one exectuion):
Unhandled Exception:
System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. Invalid length 0.
at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (System.Security.Cryptography.PaddingMode padding, System.Int32 length, System.Int32 position) [0x0005c] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00179] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00034] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x00318] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.ReadBuffer () [0x0002b] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.ReadToEnd () [0x00055] in <8f2c484307284b51944a1a13a14c0266>:0
at encryptingApp.AES_Crypt.Decrypt (System.String encriptedText, System.String key, System.Int32 ivBlockSize) [0x000e4] in <f27b48dde1ea4b788e8038439b4bdb55>:0
at encryptingApp.AES_Crypt.Main () [0x000e0] in <f27b48dde1ea4b788e8038439b4bdb55>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. Invalid length 0.
at Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (System.Security.Cryptography.PaddingMode padding, System.Int32 length, System.Int32 position) [0x0005c] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00179] in <8f2c484307284b51944a1a13a14c0266>:0
at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, System.Int32 inputOffset, System.Int32 inputCount) [0x00034] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.FlushFinalBlock () [0x0001b] in <8f2c484307284b51944a1a13a14c0266>:0
at System.Security.Cryptography.CryptoStream.Dispose (System.Boolean disposing) [0x00011] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.Stream.Close () [0x00000] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.StreamReader.Dispose (System.Boolean disposing) [0x0001c] in <8f2c484307284b51944a1a13a14c0266>:0
at System.IO.TextReader.Dispose () [0x00000] in <8f2c484307284b51944a1a13a14c0266>:0
at encryptingApp.AES_Crypt.Decrypt (System.String encriptedText, System.String key, System.Int32 ivBlockSize) [0x000f8] in <f27b48dde1ea4b788e8038439b4bdb55>:0
at encryptingApp.AES_Crypt.Main () [0x000e0] in <f27b48dde1ea4b788e8038439b4bdb55>:0
I think I should fix the Streamreader, but I don't know what to do, I've been here for hours! It seems that the program is trying to read something that has 0-length or something.
I even tried looking for more decrypting functions over the internet but none of them works for me (I'm doing this without RijndaelManaged nor salts). Im compiling in MacOS.