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.
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
Le cryptage est effectué dans InfoPath un document que je par exemple téléchargé est crypté comme x0lGQRQAAAABAAAAAAAAAH8IAAALAAAAdABlAHMAdAAwADEALgB0AHgAdAAAAHVzaW5nIFN5c3RlbTsNCnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOw0KdXNpbmcgU3lzdGVtLkxpbnE7DQp1c2luZyBTeXN0ZW0uV2ViOw0KdXNpbmcgU3lzdGVtLldlYi5NdmM7DQoNCm5hbWVzcGFjZSBNb3ZpZXNBcHAuQ29udHJvbGxlcnM –
Ajouter le informatiin supplémentaire ** à la question que tout le monde ** wil voir. Puis supprimez ce commentaire. – zaph