dongyuan7110 2013-03-14 20:29
浏览 1085

Base64无效的字符串错误

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]);
  • 写回答

1条回答 默认 最新

  • drxvjnx58751 2013-03-14 20:39
    关注

    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]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大