2016-10-06 1 views
-1

J'ai un code C# qui prend la chaîne codée cryptée d'une pièce jointe dans infopath et la déchiffre. Ceci est le code ci-dessous:Déchiffrement de pièces jointes System.FormatException: longueur non valide pour un tableau ou une chaîne char Base-64

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

} 

L'octet de ligne [] = theData Convert.FromBase64String (theBase64EncodedString); Lance une exception de format système d'une longueur non valide pour un tableau ou une chaîne de base 64.

+0

1. Où est le cryptage dans le code. 2. Les chaînes codées en Base64 doivent être un multiple de 4 octets. 3. Où est la chaîne Base64 avec laquelle vous rencontrez des problèmes? – zaph

+0

Le cryptage est effectué dans InfoPath un document que je par exemple téléchargé est crypté comme x0lGQRQAAAABAAAAAAAAAH8IAAALAAAAdABlAHMAdAAwADEALgB0AHgAdAAAAHVzaW5nIFN5c3RlbTsNCnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOw0KdXNpbmcgU3lzdGVtLkxpbnE7DQp1c2luZyBTeXN0ZW0uV2ViOw0KdXNpbmcgU3lzdGVtLldlYi5NdmM7DQoNCm5hbWVzcGFjZSBNb3ZpZXNBcHAuQ29udHJvbGxlcnM –

+0

Ajouter le informatiin supplémentaire ** à la question que tout le monde ** wil voir. Puis supprimez ce commentaire. – zaph

Répondre

0

Ce que vous avez n'est pas codé en Base64 et semble être compromis par les sections tqo.

Le fourni « données base64:

x0lGQRQAAAABAAAAAAAAAH8IAAALAAAAdABlAHMAdAAwADEALgB0AHgAdAAA AHVzaW5nIFN5c3RlbTsN CnVzaW5nIFN5c3RlbS5D b2xsZWN0aW9ucy5HZW5l cmljOw0KdXNpbmcgU3lz dGVtLkxpbnE7DQp1c2lu ZyBTeXN0ZW0uV2ViOw0K dXNpbmcgU3lzdGVtLldl Yi5NdmM7DQoNCm5hbWVz cGFjZSBNb3ZpZXNBcHAu Q29udHJvbGxlcnM

Ce n'est pas données cryptées en raison de caractères répétés

Si vous regardez les valeurs hexadécimal des données que vous verrez:

78306c4751525141414141424141414141414141414838494141414c414141416441426c41484d4164414177414445414c6742304148674164414141e2808ce2808b4148567a6157356e49464e356333526c6254734ee2808ce2808b436e567a6157356e49464e356333526c62533544e2808ce2808b623278735a574e3061573975637935485a57356ce2808ce2808b636d6c6a4f77304b64584e70626d636755336c7ae2808ce2808b644756744c6b7870626e45374451703163326c75e2808ce2808b5a79425465584e305a573075563256694f77304be2808ce2808b64584e70626d636755336c7a644756744c6c646ce2808ce2808b5969354e646d4d3744516f4e436d35686257567ae2808ce2808b6347466a5a53424e62335a705a584e4263484175e2808ce2808b5132397564484a766247786c636e4d

Notez qu'il existe des caractères du jeu de caractères Base64 tels que 0xe2, 0x80, 0x8c, 0xe2, 0x80, 0x8b. En outre, il semble y avoir deux parties, il y a une section initiale de 60 octets qui semble être Base64. Cette section base64 décodé à six pans est:

EFCDF4E9CE3BE75E76E75E35E35E35E35E36E35E35E35E35E35E35E35E35E35E3CDFCE3DE35E35E35E1CE35E35E35E35EB8E35E36E9CE35E3CE1DE35EB8E35E35EFBE35E38E39E35E1CEBBE36DF4E35E3CEBBE35EB8E35E35E35