Exemplo n.º 1
0
      //GET-query to http://127.0.0.1:7346/api/run-nbpack/-u|upload%2FMORE-secrets-inside.png|nano3|upload%2Fposts.json
      //or POST-query to http://127.0.0.1:7346/api/run-nbpack/ with four parameters - saving the JSON-file.
      //dataURL of PNG is supporting in containerPath.
      private static void Unpack(string containerPath, string key, string outputPath)
      {
          try
          {
              var encrypted = new PngStegoUtil().ReadHiddenBytesFromPng(containerPath);
              var decrypted = ByteEncryptionUtil.DecryptSalsa20(encrypted, key);
              var nposts    = NanoPostPackUtil.Unpack(decrypted);
              var posts     = nposts.Select(
                  np => new NDB.Post
                {
                    replyto = np.SerializedString().Substring(0, 32),
                    message = Convert.ToBase64String(Encoding.UTF8.GetBytes(np.SerializedString().Substring(32)))
                }).ToArray();
              Validate(posts);

              //from the sources of client 3.1
              for (int i = 0; i < posts.Length; i++)
              {
                  posts[i].message = NDB.PostsValidator.FromB64(posts[i].message);
              }

              var result = JsonConvert.SerializeObject(posts, Formatting.Indented);
              File.WriteAllText(outputPath, result);
          }
          catch
          {
              var posts = new Posts();
              posts.posts = new Post[0];
              var result = JsonConvert.SerializeObject(posts);
              File.WriteAllText(outputPath, result);
          }
          return;
      }
Exemplo n.º 2
0
 public static NDB.Post[] Unpack(Image container, string key)                                      //this Unpack using Image from RAM, as parameter.
 {
     try
     {
         //Console.WriteLine("Start read hidden bytes");
         var encrypted = new PngStegoUtil().ReadHiddenBytesFromPng(container);     //here can be catch
         //Console.WriteLine("readhiddenbytesFromPNG..........");
         var decrypted = ByteEncryptionUtil.DecryptSalsa20(encrypted, key);        //here can be catch
         //Console.WriteLine("decrypted...........");
         var nposts = NanoPostPackUtil.Unpack(decrypted);                          //here can be catch
         //Console.WriteLine("nposts...........");
         var posts = nposts.Select(                                                //here can be catch
             np => new NDB.Post
           {
               replyto = np.SerializedString().Substring(0, 32),
               message = Convert.ToBase64String(Encoding.UTF8.GetBytes(np.SerializedString().Substring(32)))
           }).ToArray();
         //Console.WriteLine("posts...........");
         Validate(posts);                                                                                                                  //AND HERE reason of for lagging containers
         //Console.WriteLine("Validate...........");
         return(posts);
     }
     catch  // (Exception e) fix compile warning
     {
         //Console.WriteLine("Unpack catch");									//here can be catch.
         return(new NDB.Post[0]);
     }
 }
Exemplo n.º 3
0
      private static void Pack(string postsPath, string templatePath, string key, string outputPath)
      {
          var json  = File.ReadAllText(postsPath);
          var posts = JsonConvert.DeserializeObject <NDB.Post[]>(json);

          Validate(posts);
          var nposts = new List <NanoPost>();

          foreach (var p in posts)
          {
              nposts.Add(new NanoPost(p.replyto + Encoding.UTF8.GetString(Convert.FromBase64String(p.message))));
          }
          var packed    = NanoPostPackUtil.Pack(nposts.ToArray());
          var encrypted = ByteEncryptionUtil.EncryptSalsa20(packed, key);

          Image bmp;

          if (
              templatePath.IndexOf("data:") != -1 &&
              templatePath.IndexOf("base64,") != -1 &&
              nbpack.NBPackMain.IsBase64Encoded(templatePath.Split(',')[1])
              )
          {
              Console.WriteLine("Image uploaded and dataURL found. Create bitmap from dataURL.");

              //create bitmap from dataURL, and save this as PNG-file to Upload folder.
              var base64Data = Regex.Match(templatePath, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
              var binData    = Convert.FromBase64String(base64Data);

              using (var stream = new MemoryStream(binData))
              {
                  bmp = new Bitmap(stream);                                     //create image from dataURL

                  //save this image as PNG-file to the folder "upload"
                  //Console.WriteLine(bmp);
                  //bmp.Save("upload/" + Guid.NewGuid().ToString() + ".png", ImageFormat.Png);
                  //bmp.Dispose();
                  //Console.WriteLine("saved to \"upload\"");
              }
              //working...
          }
          else
          {
              Console.WriteLine("DataURL not found. Create bitmap from templatePath = {0}", templatePath);
              bmp = Bitmap.FromFile(templatePath);
          }

          var capacity = (bmp.Width * bmp.Height * 3) / 8 - 32;

          if (encrypted.Length > capacity)
          {
              float scale = (encrypted.Length / (float)capacity);
              Console.WriteLine("Warning: scaling image to increase capacity: " + scale.ToString("n2") + "x");
              scale = (float)Math.Sqrt(scale);
              bmp   = new Bitmap(bmp, (int)(bmp.Width * scale + 1), (int)(bmp.Height * scale + 1));
          }

          new PngStegoUtil().HideBytesInPng(bmp, outputPath, encrypted);
          return;
      }
Exemplo n.º 4
0
        public string AddSignatureToThePost(string post, string guess)
        {
            var dec_seed   = ByteEncryptionUtil.WrappedXor(_encryptedSeed, guess + _publicKey.Stringify());
            var privateKey = Ed25519.ExpandedPrivateKeyFromSeed(dec_seed);
            var signature  = Ed25519.Sign(Encoding.UTF8.GetBytes(post), privateKey);

            return(post + "[" + SignatureTag + "=" + signature.Stringify() + "]");
        }
Exemplo n.º 5
0
        public bool CheckGuess(string guess)
        {
            var dec_seed     = ByteEncryptionUtil.WrappedXor(_encryptedSeed, guess + _publicKey.Stringify());
            var privateKey   = Ed25519.ExpandedPrivateKeyFromSeed(dec_seed);
            var dummyMessage = new byte[] { (byte)0 };
            var signature    = Ed25519.Sign(dummyMessage, privateKey);

            return(Ed25519.Verify(signature, dummyMessage, _publicKey));
        }
Exemplo n.º 6
0
 //GET-query to http://127.0.0.1:7346/api/run-nbpack/-u|upload%2FMORE-secrets-inside.png|nano3
 //or POST-query to http://127.0.0.1:7346/api/run-nbpack/ with three two parameters - return JSON.
 //dataURL of PNG is supporting in containerPath.
 public static NDB.Post[] Unpack(string containerPath, string key)                                 //containerPath - pathway to container, as file
 {
     try
     {
         //Console.WriteLine("Start read hidden bytes");
         var encrypted = new PngStegoUtil().ReadHiddenBytesFromPng(containerPath);
         //Console.WriteLine("readhiddenbytesFromPNG..........");
         var decrypted = ByteEncryptionUtil.DecryptSalsa20(encrypted, key);                //here is reason of catch for some files
         //Console.WriteLine("decrypted...........");
         var nposts = NanoPostPackUtil.Unpack(decrypted);                                  //or here
         //Console.WriteLine("nposts...........");
         var posts = nposts.Select(                                                        //or here
             np => new NDB.Post
           {
               replyto = np.SerializedString().Substring(0, 32),
               message = Convert.ToBase64String(Encoding.UTF8.GetBytes(np.SerializedString().Substring(32)))
           }).ToArray();
         //Console.WriteLine("posts...........");
         Validate(posts);                                                                                                                  //in the most cases this not working, then catch
         //Console.WriteLine("Validate...........");
         return(posts);
     }
     catch  // (Exception e) //fix compile warning
     {
         Console.WriteLine("Unpack catch: " +
                           (
                               (containerPath.IndexOf("data:") != -1 &&
                                containerPath.IndexOf("base64,") != -1 &&
                                nbpack.NBPackMain.IsBase64Encoded(containerPath.Split(',')[1])
                               )
                                                   ? containerPath.Substring(0, 40) + "..."
                                                   : containerPath
                           )
                           );                                                                              //here catch for some files.
         return(new NDB.Post[0]);
     }
 }
Exemplo n.º 7
0
      private static void Pack(NDB.Post[] posts, string templatePath, string key, string outputPath, string address = "")
      {
          var @set = new HashSet <string>();

          Validate(posts);
          var    nposts    = new List <NanoPost>();
          string add_notif = ((address == "CreatePNG_on_lite_server")?address + ". ":"") + "Showing posts that will go to the container:";

          //NServer.NotificationHandler.Instance.Messages.Enqueue(add_notif);	//do not add notif, and add this from response.
          NServer.DbApiHandler.notifications_with_filename += add_notif + "|||";
          foreach (var p in posts)
          {
              var mess = Encoding.UTF8.GetString(Convert.FromBase64String(p.message));
              var hash = p.hash;

              if ([email protected](hash))
              {
                  @set.Add(hash);   /*add_notif = ((address == "CreatePNG_on_lite_server")?address+". ":"")+mess; //do not define this, because nothing to do with this.
                                     * NServer.NotificationHandler.Instance.Messages.Enqueue(add_notif); //do not add notif, and add this from response*/			/* NServer.DbApiHandler.notifications_with_filename += add_notif + "|||";// don't add posts-content - to returned string */
              }

              nposts.Add(new NanoPost(p.replyto + mess));
          }
          var packed    = NanoPostPackUtil.Pack(nposts.ToArray());
          var encrypted = ByteEncryptionUtil.EncryptSalsa20(packed, key);

          Image bmp;

          if (
              templatePath.IndexOf("data:") != -1 &&
              templatePath.IndexOf("base64,") != -1 &&
              nbpack.NBPackMain.IsBase64Encoded(templatePath.Split(',')[1])
              )
          {
              Console.WriteLine("Image uploaded and dataURL found. Create bitmap from dataURL.");

              //create bitmap from dataURL, and save this as PNG-file to Upload folder.
              var base64Data = Regex.Match(templatePath, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
              var binData    = Convert.FromBase64String(base64Data);

              using (var stream = new MemoryStream(binData))
              {
                  bmp = new Bitmap(stream);                                     //create image from dataURL

                  //save this image as PNG-file to the folder "upload"
                  //Console.WriteLine(bmp);
                  //bmp.Save("upload/" + Guid.NewGuid().ToString() + ".png", ImageFormat.Png);
                  //bmp.Dispose();
                  //Console.WriteLine("saved to \"upload\"");
              }
              //working...
          }
          else
          {
              //Console.WriteLine("DataURL not found. Create bitmap from templatePath = {0}", templatePath);
              bmp = Bitmap.FromFile(templatePath);
          }
          var capacity = (bmp.Width * bmp.Height * 3) / 8 - 32;

          if (encrypted.Length > capacity)
          {
              float scale = (encrypted.Length / (float)capacity);
              Console.WriteLine("Warning: scaling image to increase capacity: " + scale.ToString("n2") + "x");
              scale = (float)Math.Sqrt(scale);
              bmp   = new Bitmap(bmp, (int)(bmp.Width * scale + 1), (int)(bmp.Height * scale + 1));
          }

          new PngStegoUtil().HideBytesInPng(bmp, outputPath, encrypted);
          return;
      }