简体   繁体   中英

Attachment Decryption System.FormatException: Invalid length for a Base-64 char array or string

I have ac# code which takes the encrypted encoded string of an attachment in infopath and decrypts it. This is the code below:

    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; }
    }

}

The line byte[] theData = Convert.FromBase64String(theBase64EncodedString); throws a system format exception invalid length for a base 64 array or string.

What you have is not Base64 encoded and seems to be compromized of tqo sections.

The provided "Base64 data:

x0lGQRQAAAABAAAAAAAAAH8IAAALAAAAdABlAHMAdAAwADEALgB0AHgAdAAA‌​AHVzaW5nIFN5c3RlbTsN‌​CnVzaW5nIFN5c3RlbS5D‌​b2xsZWN0aW9ucy5HZW5l‌​cmljOw0KdXNpbmcgU3lz‌​dGVtLkxpbnE7DQp1c2lu‌​ZyBTeXN0ZW0uV2ViOw0K‌​dXNpbmcgU3lzdGVtLldl‌​Yi5NdmM7DQoNCm5hbWVz‌​cGFjZSBNb3ZpZXNBcHAu‌​Q29udHJvbGxlcnM

This is not encrypted data due to repeated characters.

If you look at the hex values of the data you will see:

Note that there are characters out of the Base64 character set such as 0xe2, 0x80, 0x8c, 0xe2, 0x80, 0x8b.

Additionaly there seem to be two parts, there is an initial section of 60 byte that does seem to be Base64. That section Base64 decoded to hex is:

EFCDF4E9CE3BE75E76E75E35E35E35E35E36E35E35E35E35E35E35E35E35E35E3CDFCE3DE35E35E35E1CE35E35E35E35EB8E35E36E9CE35E3CE1DE35EB8E35E35EFBE35E38E39E35E1CEBBE36DF4E35E3CEBBE35EB8E35E35E35

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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