附件解密系统.FormatException: Base-64字符数组或字符串的长度无效

本文关键字:字符串 无效 数组 字符 解密 系统 FormatException Base-64 | 更新日期: 2023-09-27 18:12:01

我有一个c#代码,它采用infopath中附件的加密编码字符串并对其解密。这是下面的代码:

    private int fileSize;
    private int attachmentNameLength;
    private string attachmentName;
    private byte[] decodedAttachment;

    /// <summary>
    /// Accepts the Base64 encoded string
    /// that is the attachment.
    /// </summary>
    public InfoPathAttachmentDecoder(string theBase64EncodedString)
    {
        **byte[] theData =     Convert.FromBase64String(theBase64EncodedString);** //This line throws a   System.FormatException: Invalid length for a Base-64 char array or string.
        using (MemoryStream ms = new MemoryStream(theData))
        {
            BinaryReader theReader = new BinaryReader(ms);
            DecodeAttachment(theReader);
        }
    }
    private void DecodeAttachment(BinaryReader theReader)
    {
        //Position the reader to obtain the file size.
        byte[] headerData = new byte[FIXED_HEADER];
        headerData = theReader.ReadBytes(headerData.Length);
        fileSize = (int)theReader.ReadUInt32();
        attachmentNameLength = (int)theReader.ReadUInt32() * 2;
        byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
        //InfoPath uses UTF8 encoding.
        Encoding enc = Encoding.Unicode;
        attachmentName = enc.GetString(fileNameBytes, 0,  attachmentNameLength - 2);
        decodedAttachment = theReader.ReadBytes(fileSize);
    }
    public void SaveAttachment(string saveLocation)
    {
        string fullFileName = saveLocation;
        if (!fullFileName.EndsWith(Path.DirectorySeparatorChar.ToString()))
        {
            fullFileName += Path.DirectorySeparatorChar;
        }
        fullFileName += attachmentName;
        if (File.Exists(fullFileName))
            File.Delete(fullFileName);
        FileStream fs = new FileStream(fullFileName, FileMode.CreateNew);
        BinaryWriter bw = new BinaryWriter(fs);
        bw.Write(decodedAttachment);
        bw.Close();
        fs.Close();
    }
    public string Filename
    {
        get { return attachmentName; }
    }
    public byte[] DecodedAttachment
    {
        get { return decodedAttachment; }
    }
}

line byte[] theData = Convert.FromBase64String(theBase64EncodedString);抛出系统格式异常,base64数组或字符串的长度无效。

附件解密系统.FormatException: Base-64字符数组或字符串的长度无效

您所拥有的不是Base64编码的,并且似乎受到了两个部分的损害。

提供的"Base64 data:

x0lgqrqaaaaaaaaaaaah8iaaaaaaaaaaah8iaaaaaadablahmadaawadealgb0ahgadaaa、ahvzaw5nifn5c3rlbs5d、b2xsZWN0aW9ucy5HZW5l、cmljow0kdxnpbmcgu3lzdgv2viow0k、dxnpbmcgu3lzdgv1l、y5ndmm7dqoncm5hbwvz、cGFjZSBNb3ZpZXNBcHAu、Q29udHJvbGxlcnM

由于字符重复,这不是加密数据。

如果您查看数据的十六进制值,您将看到:

78306 c4751525141414141424141414141414141414838494141414c414141416441426c41484d4164414177414445414c6742304148674164414141e2808ce2808b4148567a6157356e49464e356333526c6254734ee2808ce2808b436e567a6157356e49464e356333526c62533544e2808ce2808b623278735a574e3061573975637935485a57356ce2808ce2808b636d6c6a4f77304b64584e70626d636755336c7ae2808ce2808b644756744c6b7870626e45374451703163326c75e2808ce2808b5a79425465584e305a573075563256694f77304be2808ce2808b64584e70626d636755336c7a644756744c6c646ce2808ce2808b5969354 e646d4d3744516f4e436d35686257567ae2808ce2808b6347466a5a53424e62335a705a584e4263484175e2808ce2808b5132397564484a766247786c636e4d

请注意Base64字符集之外的字符,例如0xe2, 0x80, 0x8c, 0xe2, 0x80, 0x8b。

此外,似乎有两个部分,有一个60字节的初始部分,似乎是Base64。Base64解码为十六进制的部分是:

EFCDF4E9CE3BE75E76E75E35E35E35E35E36E35E35E35E35E35E35E35E35E35E3CDFCE3DE35E35E35E1CE35E35E35E35EB8E35E36E9CE35E3CE1DE35EB8E35E35EFBE35E38E39E35E1CEBBE36DF4E35E3CEBBE35EB8E35E35E35