public void Encrypt() { byte[] data = new byte[1024]; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); try { if (publicxml.Length == 0) { _ispub_xml = false; MessageBox.Show("Файл с открытым ключем пуст..."); return; } else { rsa.FromXmlString(publicxml); } } catch (Exception ex) { MessageBox.Show("Проблема с RSA... \n" + ex.Message.ToString()); } try { data = Encoding.UTF8.GetBytes(txtMessage.Text); } catch (Exception ss) { MessageBox.Show(ss.ToString()); return; } try { EncryptedData = rsa.Encrypt(data, false); } catch (CryptographicException ex) { MessageBox.Show("Ошибка расшифровки... \n" + ex.Message.ToString()); } s2 = Encoding.UTF8.GetString(EncryptedData); s4 = Convert.ToBase64String(EncryptedData); for (int i = 0; i < data.Length - 1; i++) { data.SetValue((byte)0, i); } for (int i = 0; i < EncryptedData.Length - 1; i++) { EncryptedData.SetValue((byte)0, i); } }
public Boolean Initialise() { Boolean retVal = false; try { _state = Common.NodeState.Initialising; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Initialising AESEncryptNode."); //Clear outputs EncryptedData.SetValue(null); retVal = true; } finally { _state = retVal ? Common.NodeState.Ready : Common.NodeState.Error; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Initialised AESEncryptNode."); } return(retVal); }
public Boolean Process() { Boolean retVal = false; try { _state = Common.NodeState.Processing; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Processing AESDecryptNode."); if (IV.Value == null) { //Split IV from the data using (MemoryStream dataWithIV = new MemoryStream(EncryptedData.Value)) { Byte[] iv = new Byte[16]; dataWithIV.Read(iv, 0, iv.Length); Byte[] dataBlock = new Byte[dataWithIV.Length - iv.Length]; dataWithIV.Read(dataBlock, 0, dataBlock.Length); IV.SetValue(iv); EncryptedData.SetValue(dataBlock); } } Byte[] inBlock = (Byte[])EncryptedData.Value; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Creating AES decryptor."); DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Key = {0}.", ((Byte[])Key.Value).ToPrettyString()); DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "IV = {0}.", ((Byte[])IV.Value).ToPrettyString()); DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Input = {0}.", inBlock.ToPrettyString()); Byte[] outBlock = AESUtility.DecryptBytes(inBlock, (Byte[])Key.Value, (Byte[])IV.Value); _unencryptedData.SetValue(UnicodeEncoding.Unicode.GetString(outBlock)); retVal = true; } catch (Exception ex) { _state = Common.NodeState.Error; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.Fail | DFramework.Logging.Interfaces.LoggerMessageType.Exception | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Exception occurred in AESDecryptNode :: {0}", ex.ToString()); retVal = false; } finally { _state = Common.NodeState.Processed; DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Processed AESDecryptNode."); if (retVal) { Processed?.Invoke(this, EventArgs.Empty); } } if (retVal) { if (_next != null && _next.Length > 0) { return(_next[0].Process()); } else { return(true); } } else { return(false); } }