我有一個c#代碼,它將infopath中附件的加密編碼字符串解密。這是下面的代碼:Attachment Decryption System.FormatException: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; }
}
}
行字節[]海圖= Convert.FromBase64String(theBase64EncodedString);拋出基本64數組或字符串的系統格式異常無效長度。
1.哪裏有代碼加密。 2. Base64編碼的字符串必須是4字節的倍數。 3.您遇到問題的Base64字符串在哪裏? – zaph
加密是在InfoPath e.g做我上傳的文檔進行加密爲x0lGQRQAAAABAAAAAAAAAH8IAAALAAAAdABlAHMAdAAwADEALgB0AHgAdAAAAHVzaW5nIFN5c3RlbTsNCnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOw0KdXNpbmcgU3lzdGVtLkxpbnE7DQp1c2luZyBTeXN0ZW0uV2ViOw0KdXNpbmcgU3lzdGVtLldlYi5NdmM7DQoNCm5hbWVzcGFjZSBNb3ZpZXNBcHAuQ29udHJvbGxlcnM –
添加額外informatiin **的**問題,所以每個人都西港島線看到它。然後刪除此評論。 – zaph