I have a problem working with Base64 Strings in C#
I have an .exe which is called from php. It gets 2 params and returns an encrypted key. (The .exe is where I have the problem)
In php I do this:
<?php
$key = "AAAA";
$pass=" AAAA";
echo shell_exec("cryptograph.exe generateKey $key $pass");
?>
It should work, becouse those are base 64 strings, or at least, I understand that a String with a length multiple of 4 is a valid base 64 string.
But I get the following(Is in spanish but I will translate it bellow):
Encrypt en System.Convert.FromBase64String(String s)
en cryptograph.Cryptography.Encrypt(String plainStr, String completeEncodedKey, Int32 keySize) en cryptograph.Cryptography.generateKey(String key, String pass)
en cryptograph.cryptograph.Main(String[] args)
La entrada no es una cadena Base 64 v lida porque contiene un car cter que no es Base 64, m s de dos caracteres de relleno o un car cter de relleno que no es un espacio en blanco
Basically it sais that it is no valid Base64 String, becouse it contains a char which is no Base 65, more than too filler(relleno is translate like that?) chars which are no whitespaces.
This is part of the c# code.
public static void generateKey(String key, String pass)
{
String e = Encrypt(pass, key, 256);
Console.WriteLine("Entro generateKey");
System.Console.WriteLine(e);
}
private static string Encrypt(string plainStr, string completeEncodedKey, int keySize)
{
Console.WriteLine("Entro Encrypt");
RijndaelManaged aesEncryption = new RijndaelManaged();
aesEncryption.KeySize = keySize;
aesEncryption.BlockSize = 128;
aesEncryption.Mode = CipherMode.CBC;
aesEncryption.Padding = PaddingMode.PKCS7;
Console.WriteLine(completeEncodedKey);
aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[0]);
aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[1]);
byte[] plainText = ASCIIEncoding.UTF8.GetBytes(plainStr);
ICryptoTransform crypto = aesEncryption.CreateEncryptor();
Console.WriteLine("Abajo de crypto");
// The result of the encryption and decryption
byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherText);
}
The problem, happens in some of these two lines:
aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[0]);
aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[1]);