private static void FilesToText() { var files = Clipboard.GetFileDropList(); if (files.Count == 0) { Console.Error.WriteLine("No Files found on Clipboard"); Environment.Exit(1); } List <FileRepresentation> fileReps = new List <FileRepresentation>(files.Count); foreach (var f in files) { fileReps.Add(new FileRepresentation { Filename = f, Bytes = File.ReadAllBytes(f) }); } var pickler = FsPickler.CreateBinarySerializer(); var bytes = pickler.Pickle <FileRepresentation[]>(fileReps.ToArray()); var str = Convert.ToBase64String(bytes); Clipboard.SetText(str); }
public T Deserialize(string data) { //Security Warning: The following code is intentionally vulnerable to a serialization vulnerability var ser = FsPickler.CreateJsonSerializer(); using (TextReader sr = new StringReader(data)) { return(ser.Deserialize <T>(sr)); } }
private void Test(object value) { Serializer wireSerializer = new Serializer(new SerializerOptions(false, null, true, null)); var pickler = FsPickler.CreateBinarySerializer(); double wireTs; double picklerTs; long wireSize; long picklerSize; const int repeat = 10000; { MemoryStream wireStream = new MemoryStream(); wireSerializer.Serialize(value, wireStream); var sw = Stopwatch.StartNew(); for (int i = 0; i < repeat; i++) { wireStream = new MemoryStream(); WireSerialize(value, wireSerializer, wireStream); } sw.Stop(); wireTs = sw.Elapsed.TotalMilliseconds; wireSize = wireStream.ToArray().Length; } //using (MemoryStream picklerStram = new MemoryStream()) { MemoryStream picklerStram = new MemoryStream(); pickler.Serialize(picklerStram, value); var sw = Stopwatch.StartNew(); for (int i = 0; i < repeat; i++) { picklerStram = new MemoryStream(); pickler.Serialize(picklerStram, value); } sw.Stop(); picklerTs = sw.Elapsed.TotalMilliseconds; picklerSize = picklerStram.ToArray().Length; } Console.WriteLine($"Serializing {value.GetType().Name} {repeat:n0} times"); Console.WriteLine(); Console.WriteLine($"Wire elapsed time {wireTs:n0} MS"); Console.WriteLine($"FsPickler elapsed time {picklerTs:n0} MS"); Console.WriteLine($"Wire is {picklerTs / wireTs :n2} times faster than FsPickler"); Console.WriteLine(); Console.WriteLine($"Wire payload size {wireSize} bytes"); Console.WriteLine($"FsPickler payload size {picklerSize} bytes"); Console.WriteLine($"Wire is {picklerSize / (double)wireSize:n2} times smaller than FsPickler"); //assert that we are in a 10% margin of FsPickler Assert.IsTrue(wireTs <= picklerTs * 1.1, "Wire was slower than FsPickler"); Assert.IsTrue(wireSize <= picklerSize * 1.1, "Wire payload was larger than FsPickler"); }
public string Serialize(T obj) { var ser = FsPickler.CreateJsonSerializer(); var output = new StringBuilder(); using (TextWriter sw = new StringWriter(output)) { ser.Serialize <T>(sw, obj); } return(output.ToString()); }
/* * Insecure FsPickler Deserialize usage */ public void FsPicklerDeserialization(string json) { try { var fsPickler = FsPickler.CreateJsonSerializer(); MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(json)); fsPickler.Deserialize <object>(memoryStream); } catch (Exception e) { Console.WriteLine(e); } }
/// <summary> /// This write the content of the all-knowledge to file on disk /// </summary> /// <param name="knowledgeToSave">knowledge class with content</param> /// <param name="saveOmniscienceBackup">use backup in case of error</param> public void WriteToFile(Omniscience knowledgeToSave, bool saveOmniscienceBackup = false) { var serializer = FsPickler.CreateBinarySerializer(); DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes("ZCR0N0S1"), IV = Encoding.ASCII.GetBytes("ZCR0N0S1") }; try { using (var file = File.Create(GetFilelocation())) { CryptoStream crStream = new CryptoStream(file, cryptic.CreateEncryptor(), CryptoStreamMode.Write); serializer.Serialize(crStream, knowledgeToSave); crStream.Close(); if (!saveOmniscienceBackup) { return; } try { using (var backupFile = File.Create(GetBackupFilelocation())) { CryptoStream crBackupStream = new CryptoStream(backupFile, cryptic.CreateEncryptor(), CryptoStreamMode.Write); serializer.Serialize(crBackupStream, knowledgeToSave); crBackupStream.Close(); } } catch (Exception e) { throw new RnpcFileAccessException("Error when trying to write the **backup** Omniscience to file.", e); } } } catch (Exception e) { throw new RnpcFileAccessException("Error when trying to write Omniscience to file.", e); } }
/// <summary> /// Reads an .rmf file, decrypts and deserializes its content and returns it /// </summary> /// <param name="useOmniscienceBackupAsFailsafe">Indicates whether the backup file shoud be used in case of read error</param> /// <returns>a Omniscience object</returns> public Omniscience ReadFromFile(bool useOmniscienceBackupAsFailsafe = false) { var cryptic = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes("ZCR0N0S1"), IV = Encoding.ASCII.GetBytes("ZCR0N0S1") }; try { FileStream stream = new FileStream(GetFilelocation(), FileMode.Open, FileAccess.Read); var serializer = FsPickler.CreateBinarySerializer(); var crStream = new CryptoStream(stream, cryptic.CreateDecryptor(), CryptoStreamMode.Read); return(serializer.Deserialize <Omniscience>(crStream)); } catch (Exception e) { if (!useOmniscienceBackupAsFailsafe) { throw new RnpcFileAccessException("Error when trying to read Omniscience file.", e); } try { FileStream stream = new FileStream(GetBackupFilelocation(), FileMode.Open, FileAccess.Read); var serializer = FsPickler.CreateBinarySerializer(); var crStream = new CryptoStream(stream, cryptic.CreateDecryptor(), CryptoStreamMode.Read); return(serializer.Deserialize <Omniscience>(crStream)); } catch (Exception ex) { throw new RnpcFileAccessException("Error when trying to read Omniscience file and backup Omniscience file!", ex); } } }
private static void TextToFiles() { var str = Clipboard.GetText(); var bytes = Convert.FromBase64String(str); var pickler = FsPickler.CreateBinarySerializer(); var fileReps = pickler.UnPickle <FileRepresentation[]>(bytes); var files = new StringCollection(); var tmpDir = Path.GetTempFileName(); File.Delete(tmpDir); Directory.CreateDirectory(tmpDir); foreach (var f in fileReps) { var fileName = Path.GetFileName(f.Filename); var fullPath = Path.Combine(tmpDir, fileName); File.WriteAllBytes(fullPath, f.Bytes); files.Add(fullPath); } Clipboard.SetFileDropList(files); }