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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记