Beispiel #1
0
 public static void LogState(this IService service)
 {
     using (var sb = StringBuilderObjectPool.AllocAutoRelease())
     {
         if (service.GetState(sb))
         {
             var info = sb.ToString();
             Task.Run(() =>
             {
                 var type = service.GetType();
                 try
                 {
                     lock (type)
                     {
                         var name = CFiles.ReplaceSpecialChars(service.SelfAddress.FullPath);
                         var file = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "state" + Path.DirectorySeparatorChar + name + ".txt";
                         CFiles.CreateFile(new FileInfo(file));
                         File.WriteAllText(file, info, CUtils.UTF8);
                     }
                 }
                 catch { }
             });
         }
     }
 }
Beispiel #2
0
        static public byte[] Encrypt(byte[] clearText)
        {
            try
            {
                if (clearText.Length > 128)
                {
                    throw new ByteSize("String is too large");
                }
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
                if (ReadWrite.DoesFileExist(@"..\..\..\Files\public_key.txt") && ReadWrite.DoesFileExist(@"..\..\..\Files\private_keyRSA.txt"))
                {
                    TextReader textReader = new StreamReader(@"..\..\..\Files\public_keyRSA.txt");

                    string readXMLString = textReader.ReadToEnd();
                    textReader.Close();
                    rsa.FromXmlString(readXMLString);

                    byte[] encryptedText = rsa.Encrypt(clearText, false);
                    return(encryptedText);
                }
                else
                {
                    TextWriter textWriter = new StreamWriter(@"..\..\..\Files\public_keyRSA.txt");
                    string     publicKey  = rsa.ToXmlString(false);
                    textWriter.Write(publicKey);
                    textWriter.Close();
                    textWriter = new StreamWriter(@"..\..\..\Files\private_keyRSA.txt");
                    string privateKey = rsa.ToXmlString(true);
                    textWriter.Write(privateKey);
                    textWriter.Close();

                    byte[] encryptedText = rsa.Encrypt(clearText, false);
                    CFiles.CreateFile("encrypted_Text_RSA");
                    string encryptedTextInString = System.Convert.ToBase64String(encryptedText);
                    ReadWrite.WriteToFile(encryptedTextInString, "encrypted_Text_RSA");
                    return(encryptedText);
                }
            }
            catch (ByteSize e)
            {
                MessageBox.Show("Lenght of string is to large.");
                return(new byte[0]);
            }
            catch (Exception)
            {
                MessageBox.Show("Generic error!");
                return(new byte[0]);
            }
        }