Exemplo n.º 1
0
        /// <summary>
        /// Converts the object to json and then to a byte array
        /// </summary>
        /// <param name="message">The object to convert</param>
        /// <returns>A byte array containing the serialized object</returns>
        protected override byte[] FormatToSend(TDataType message)
        {
            var bytes    = Newtonsoft.Json.JsonConvert.SerializeObject(message).AsBytes();
            var msgBytes = _rsa.Encrypt(bytes, (RSAParameters)_remotePublicKey).AsBytes(); //Base64 encodes the message while encrypting //msg should be base64 encoded going into _rsa.Encrypt

            //if (UseCompression)
            //    msgBytes = msgBytes.AsCompressed();
            return(msgBytes);
        }
Exemplo n.º 2
0
        public void EncryptAndDecrypt()
        {
            const string value = "Jordan                                                                                                            Duerksen";

            using (var rsa = new W.Encryption.RSA(2048))
            {
                var encrypted = rsa.Encrypt(value, rsa.PublicKey);
                Console.WriteLine("Encrypted = {0}", encrypted);
                var decrypted = rsa.Decrypt(encrypted, rsa.PrivateKey);
                Assert.IsTrue(decrypted == value);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Encrypts the data and sends it to the remote
        /// </summary>
        /// <param name="bytes">The data to encrypt and send</param>
        public override ulong Send(byte[] bytes)
        {
            if (_remotePublicKey == null)
            {
                System.Diagnostics.Debugger.Break();
            }
            //var bytes = base.FormatMessageToSend(message);
            Logging.Log.v((IsServerSide ? "Server" : "Client") + " encrypting " + bytes.Length + " bytes");
            var msgBytes = _rsa.Encrypt(bytes, (RSAParameters)_remotePublicKey).AsBytes(); //Base64 encodes the message while encrypting //msg should be base64 encoded going into _rsa.Encrypt

            return(base.Send(msgBytes));
            //base.Send()
        }
Exemplo n.º 4
0
 protected override byte[] FormatMessageToSend(byte[] bytesToSend)
 {
     if (_remotePublicKey != null && _rsa != null)
     {
         var cipher = _rsa.Encrypt(bytesToSend, (RSAParameters)_remotePublicKey);
         var bytes  = cipher.AsBytes();
         return(base.FormatMessageToSend(bytes));
     }
     else
     {
         return(base.FormatMessageToSend(bytesToSend));
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Override to alter data before it is sent
 /// </summary>
 /// <param name="message">The data to send</param>
 /// <returns>The original or modified data</returns>
 protected override byte[] FormatMessageToSend(string message)
 {
     byte[] msg;
     if (_remotePublicKey != null)                                           //is secure
     {
         msg = base.FormatMessageToSend(message);                            //encrypt will base64 encode it, so don't do it here
         msg = _rsa.Encrypt(msg, (RSAParameters)_remotePublicKey).AsBytes(); //msg should be base64 encoded going into _rsa.Encrypt
     }
     else
     {
         msg = base.FormatMessageToSend(message).AsBase64().AsBytes();
     }
     return(msg);
 }
Exemplo n.º 6
0
 private void btnEncrypt_Click(object sender, EventArgs e)
 {
     try
     {
         IsBusy = true;
         CreateRSA();
         var cipher = _rsa.Encrypt(txtEncryption.Text);
         txtEncryption.Text = cipher;
     }
     finally
     {
         IsBusy = false;
     }
 }