简体   繁体   English

Base64无效的字符串错误

[英]Base64 Invalid String error

I have a problem working with Base64 Strings in C# 我在使用C#中的Base64字符串时遇到问题

I have an .exe which is called from php. 我有一个从php调用的.exe。 It gets 2 params and returns an encrypted key. 它获得2个参数并返回一个加密密钥。 (The .exe is where I have the problem) (.exe是我遇到问题的地方)

In php I do this: 在PHP中我这样做:

<?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. 它应该工作,因为那些是基本的64字符串,或者至少,我理解长度倍数为4的字符串是有效的基本64字符串。

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. 基本上它是没有有效的Base64字符串,因为它包含一个不是Base 65的字符,更多的填充(relleno就像那样翻译?)字符没有空格。

This is part of the c# code. 这是c#代码的一部分。

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

Firstly, it's really not clear what completeEncodedKey is meant to really represent. 首先,我们还不清楚completeEncodedKey究竟代表什么。 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. 如果它是cryptograph.exe的结果,它意味着返回一个加密密钥,那么你肯定需要解密它 - 这不是你在这里实际做的。

Anyway, I'm sure this is the problem (twice, once for IV and once for the key): 无论如何,我确定这是问题(两次,一次用于IV,一次用于键):

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. 首先, ASCIIEncoding.UTF8非常令人困惑。 Why bring ASCII into it if you really just want UTF-8? 如果你真的只想要UTF-8,为什么要把ASCII带进去? You should use Encoding.UTF8 to be clearer. 您应该使用Encoding.UTF8更清晰。 However, I don't think you actually want this at all. 不过,我不认为你真的想这样的。

Why are you converting from base64 twice ? 你为什么要从base64转换两次 If "overall" value is UTF-8-encoded text, why is it converted to base64? 如果“整体”值是UTF-8编码的文本,为什么它转换为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: 在这种情况下,你只需要分割到的base64转换:

string[] parts = completeEncodedKey.Split(',');
aesEncryption.IV = Convert.FromBase64String(parts[0]);
aesEncryption.Key = Convert.FromBase64String(parts[1]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM