/// <summary> /// Override to alter received data before exposing it via the MessageReceived callback /// </summary> /// <param name="message">The received data</param> /// <returns>The original or modified data</returns> protected override string FormatReceivedMessage(byte[] message) { string msg; if (_remotePublicKey != null) { msg = base.FormatReceivedMessage(message); //Encoded left it base64 encoded, don't unencoded it as Decrypt requires the encoding msg = _rsa.Decrypt(msg); //msg should be base64 encoded going into _rsa.Decrypt } else { try { msg = base.FormatReceivedMessage(message).FromBase64(); //was AsBase64 in FormatMessageToSend _remotePublicKey = Newtonsoft.Json.JsonConvert.DeserializeObject <RSAParameters>(msg); } catch (Exception e) { Log.e(e); System.Diagnostics.Debug.WriteLine(e.ToString()); } Log.v("Received Public Key"); msg = null; //not a real message, so set it to null IsSecuredEvent.Set(); ConnectionSecured?.Invoke(this); } return(msg); }
/// <summary> /// Calls SecureDataReceived with the decrypted data /// </summary> /// <param name="bytes">The secure data received from the remote</param> /// <returns>The decrypted data</returns> protected virtual byte[] OnSecureDataReceived(byte[] bytes) { Logging.Log.v((IsServerSide ? "Server" : "Client") + " decrypting " + bytes.Length + " bytes"); var message = _rsa.Decrypt(bytes.AsString()); //msg should be base64 encoded (by a previous Encrypt call) going into _rsa.Decrypt var msgBytes = message.AsBytes(); SecureDataReceived?.Invoke(this, msgBytes);// FormatReceivedMessage<byte[]>(bytes)); return(msgBytes); }
/// <summary> /// Converts a byte array into a deserialized object /// </summary> /// <param name="msgBytes">The byte array to convert</param> /// <returns>The deserialized object</returns> protected override TDataType FormatReceived(byte[] msgBytes) { //if (UseCompression) // msgBytes = msgBytes.AsDecompressed(); var message = msgBytes.AsString(); message = _rsa.Decrypt(message); //msg should be base64 encoded (by a previous Encrypt call) going into _rsa.Decrypt var obj = Newtonsoft.Json.JsonConvert.DeserializeObject <TDataType>(message); return(obj); }
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); } }
protected override byte[] FormatReceivedMessage(byte[] receivedBytes) { if (_rsa != null) { var cipher = receivedBytes.AsString(); var decrypted = _rsa.Decrypt(cipher); var bytes = decrypted.AsBytes(); return(base.FormatReceivedMessage(bytes)); } else { return(base.FormatMessageToSend(receivedBytes)); } }
private void btnDecrypt_Click(object sender, EventArgs e) { try { IsBusy = true; CreateRSA(); var text = _rsa.Decrypt(txtEncryption.Text); txtEncryption.Text = text; } finally { IsBusy = false; } }
/// <summary> /// Override to alter received data before exposing it via the MessageReceived callback /// </summary> /// <param name="message">The received data</param> /// <returns>The original or modified data</returns> protected override string FormatReceivedMessage(byte[] message) { string msg; if (_remotePublicKey != null) { msg = base.FormatReceivedMessage(message); //Encoded left it base64 encoded, don't unencoded it as Decrypt requires the encoding msg = _rsa.Decrypt(msg); //msg should be base64 encoded going into _rsa.Decrypt } else { msg = base.FormatReceivedMessage(message).FromBase64(); _remotePublicKey = msg.FromXml <RSAParameters>(); Log.v("Received Public Key"); msg = null; //not a real message, so set it to null ConnectionSecured?.Invoke(this); } return(msg); }