Reputation: 1186
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]);
Upvotes: 0
Views: 4583
Reputation: 1499760
Firstly, it's really not clear what completeEncodedKey
is meant to really represent. If it's the result of cryptograph.exe which is meant to return an encrypted key, then surely you need to decrypt it - which isn't what you're actually doing here.
Anyway, I'm sure this is the problem (twice, once for IV and once for the key):
aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString
(Convert.FromBase64String(completeEncodedKey)).Split(',')[0]);
Let's split this up a bit for sanity:
byte[] completeBinaryKey = Convert.FromBase64String(completeEncodedKey);
string asciiKey = ASCIIEncoding.UTF8.GetString(completeBinaryKey);
string[] parts = asciiKey.Split(',');
string ivBase64 = parts[0];
aesEncryption.IV = Convert.FromBase64String(ivBase64);
For a start, ASCIIEncoding.UTF8
is extremely confusing. Why bring ASCII into it if you really just want UTF-8? You should use Encoding.UTF8
to be clearer. However, I don't think you actually want this at all.
Why are you converting from base64 twice? If "overall" value is UTF-8-encoded text, why is it converted to base64?
I strongly suspect your text is actually of the form:
<base64-encoded-iv>,<base64-encoded-key>
In that case, you just need to split then to the base64-conversion:
string[] parts = completeEncodedKey.Split(',');
aesEncryption.IV = Convert.FromBase64String(parts[0]);
aesEncryption.Key = Convert.FromBase64String(parts[1]);
Upvotes: 2