private void buttonSend_Click(object sender, RoutedEventArgs e) { if (!(textBoxMessage.Text.Length == 0)) { Message message = new Message(); message.Date = DateTime.Now; //message.EncryptedText = textBoxMessage.Text; message.Receiver = _receiver; message.Sender = _user; using (Aes myAes = Aes.Create()) { byte[] encrypted = AESUtility.EncryptStringToBytes_Aes(textBoxMessage.Text, myAes.Key, myAes.IV); message.EncryptedText = encrypted; message.IV = myAes.IV; message.EncryptedAesKey = RSAUtility.RSAEncrypt(_receiver.PublicKey, myAes.Key); } //Generate hash from original message & encrypted with RSA for signing message.RSAEncryptedHashedMessage = RSAUtility.RSASign(_user.PrivateKey, Encoding.ASCII.GetBytes(textBoxMessage.Text)); _messageRepository.InsertMessage(message); textBoxMessage.Text = ""; LoadConversations(); } }
private void LoadConversations() { List <Message> messages = _messageRepository.GetAConversation(_user, _receiver); textBlockMessages.Text = ""; foreach (Message message in messages) { string decryptedText = AESUtility.DecryptStringFromBytes_Aes(message.EncryptedText, RSAUtility.RSADecrypt(message.Receiver.PrivateKey, message.EncryptedAesKey), message.IV); if (RSAUtility.RSAVerifySignedHash(Encoding.ASCII.GetBytes(decryptedText), message.RSAEncryptedHashedMessage, message.Sender.PublicKey)) { textBlockMessages.Text += message.Date.ToString("F") + "\n" + message.Sender.Username + ": " + decryptedText + "\n\n"; } else { MessageBox.Show("Error, someone tries to mess with the keys.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } scrollviewerConversation.ScrollToEnd(); }
protected PersonComplexAES(SerializationInfo info, StreamingContext context) { Id = info.GetInt32("Id"); Name = info.GetString("Name"); // Decrypt using utility class SSNEncrypt = (byte[])info.GetValue("SSNEncrypt", typeof(byte[])); SSN = AESUtility.Decrypt(SSNEncrypt, cnxString); isDirty = info.GetBoolean("isDirty"); }
/// <summary> /// 创建数据表头 /// </summary> /// <param name="header"></param> /// <param name="accessToken"></param> /// <returns></returns> public TableInfoIdParam CreateReportTableInfoHeader(TableInfoHeaderVo header, string accessToken) { TableInfoIdParam tableInfoIdParam = null; string url = URLConstant.CREATETABLEINFOHEADER + string.Format("?accessToken={0}", accessToken); ResponseContext <string> result = PostGetAction(url, header); string decryptData = AESUtility.Decrypt(result.data, m_AESKey); tableInfoIdParam = JsonConvert.DeserializeObject <TableInfoIdParam>(decryptData); return(tableInfoIdParam); }
public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Id", Id); info.AddValue("Name", Name); // Encrypt using utility class. //info.AddValue("SSN", SSN); byte[] data = AESUtility.Encrypt(SSN, cnxString); info.AddValue("SSNEncrypt", data); info.AddValue("isDirty", isDirty); }
/// <summary> /// 获取轻应用管理员权限内所有报表分类及OpenApi创建的数据表信息 /// </summary> /// <param name="eid"></param> /// <param name="accessToken"></param> /// <returns></returns> public List <CategoryWithReportTableVo> GetAdminTableInfos(string eid, string accessToken) { string url = URLConstant.GETADMINTABLEINFOS + string.Format("?accessToken={0}", accessToken); dynamic dyParams = new ExpandoObject(); dyParams.eid = eid; ResponseContext <string> result = PostGetAction(url, dyParams); string decryptData = AESUtility.Decrypt(result.data, m_AESKey); List <CategoryWithReportTableVo> categoryWithReportTableVos = JsonConvert.DeserializeObject <List <CategoryWithReportTableVo> >(decryptData); return(categoryWithReportTableVos); }
/// <summary> /// 查询上传数据表数据上传结果 /// </summary> /// <param name="eid"></param> /// <param name="batchId"></param> /// <param name="accessToken"></param> /// <returns></returns> public DataSaveStatus GetReportTableInfoDataSaveStatus(string eid, string batchId, string accessToken) { string url = URLConstant.GETTABLEINFODATASAVESTATUS + string.Format("?accessToken={0}", accessToken); dynamic dyParams = new ExpandoObject(); dyParams.eid = eid; dyParams.batchId = batchId; ResponseContext <string> result = PostGetAction(url, dyParams); string decryptData = AESUtility.Decrypt(result.data, m_AESKey); return(JsonConvert.DeserializeObject <DataSaveStatus>(decryptData)); }
public void EncryptDecryptSimpleStringTest() { string plainText = "Hello World!"; byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(plainText); byte[] iv = SimpleRandomGenerator.QuickGetRandomBytes(16); byte[] key = SimpleRandomGenerator.QuickGetRandomBytes(32); byte[] cipherBytes = AESUtility.EncryptBytes(plainBytes, key, iv); byte[] decryptedBytes = AESUtility.DecryptBytes(cipherBytes, key, iv); string decryptedPlainText = System.Text.Encoding.UTF8.GetString(decryptedBytes); Assert.AreEqual(plainText, decryptedPlainText); }
private void buttonDecode_Click(object sender, RoutedEventArgs e) { if (checkBoxEncrypted.IsChecked == true) { try { textBoxText.Text = AESUtility.DecryptStringAES(SteganographyUtility.extractText(_image), passwordBox.Password); } catch (Exception) { MessageBox.Show("Wrong password!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else { textBoxText.Text = SteganographyUtility.extractText(_image); } }
private void buttonEncode_Click(object sender, RoutedEventArgs e) { //Check if text fits in image and if not empty if (textBoxText.Text.Length == 0) { MessageBox.Show("The text you want to hide can't be empty", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } else if (textBoxText.Text.Length > _image.Height * _image.Width * 0.375) { MessageBox.Show("This text is too long (" + textBoxText.Text.Length + " characters) for the image.\n" + "The maximum number of characters you can use: " + Math.Floor(_image.Height * _image.Width * 0.375), "Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { if (checkBoxEncrypted.IsChecked == true) { if (passwordBox.Password.Length < 6) { MessageBox.Show("Password must have at least 6 characters.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } else { _image = SteganographyUtility.embedText( AESUtility.EncryptStringAES(textBoxText.Text, passwordBox.Password), _image); textBoxText.Text = ""; } } else { _image = SteganographyUtility.embedText( textBoxText.Text, _image); textBoxText.Text = ""; } } }
/// <summary> /// 上报数据通用方法 /// </summary> /// <param name="url"></param> /// <param name="dyData"></param> /// <returns></returns> private ResponseContext <string> PostGetAction(string url, dynamic dyData) { string jsonData = JsonConvert.SerializeObject(dyData, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }); string data = AESUtility.Encrypt(jsonData, m_AESKey); var model = new { data = data }; ResponseContext <string> result = new ResponseContext <string>() { success = false }; return(HttpUtility.PostData <ResponseContext <string> >(url, model)); }
private static void GetPAKsFileInfos() { if (PAKEntries.PAKEntriesList != null && PAKEntries.PAKEntriesList.Any()) { new UpdateMyProcessEvents($"Writing {Path.GetFileName(BACKUP_FILE_PATH)}", "Waiting").Update(); Directory.CreateDirectory(Path.GetDirectoryName(BACKUP_FILE_PATH)); using (FileStream fileStream = new FileStream(BACKUP_FILE_PATH, FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(fileStream)) { DebugHelper.WriteLine("Backup: Gathering info about local .PAK files"); foreach (PAKInfosEntry Pak in PAKEntries.PAKEntriesList) { byte[] AESKey = null; if (Pak.bTheDynamicPAK) { if (AESEntries.AESEntriesList != null && AESEntries.AESEntriesList.Any()) { string AESFromManager = AESEntries.AESEntriesList.Where(x => string.Equals(x.ThePAKName, Path.GetFileNameWithoutExtension(Pak.ThePAKPath))).Select(x => x.ThePAKKey).FirstOrDefault(); if (!string.IsNullOrEmpty(AESFromManager)) { AESKey = AESUtility.StringToByteArray(AESFromManager); } } } else { AESKey = AESUtility.StringToByteArray(FProp.Default.FPak_MainAES); } if (AESKey != null) { DebugHelper.WriteLine($".PAKs: Backing up {Pak.ThePAKPath} with key: {BitConverter.ToString(AESKey).Replace("-", string.Empty)}"); PakReader.PakReader reader = new PakReader.PakReader(Pak.ThePAKPath, AESKey); if (reader != null) { new UpdateMyProcessEvents($"{Path.GetFileNameWithoutExtension(Pak.ThePAKPath)} mount point: {reader.MountPoint}", "Waiting").Update(); foreach (FPakEntry entry in reader.FileInfos) { writer.Write(entry.Offset); writer.Write(entry.Size); writer.Write(entry.UncompressedSize); writer.Write(entry.Encrypted); writer.Write(entry.StructSize); writer.Write(entry.Name); writer.Write(entry.CompressionMethodIndex); } } } else { DebugHelper.WriteLine($".PAKs: Not backing up {Pak.ThePAKPath} because its key is empty"); } } } if (new FileInfo(BACKUP_FILE_PATH).Length > 0) //HENCE WE CHECK THE LENGTH { DebugHelper.WriteLine($".PAKs: \\Backups\\{Path.GetFileName(BACKUP_FILE_PATH)} successfully created"); new UpdateMyProcessEvents($"\\Backups\\{Path.GetFileName(BACKUP_FILE_PATH)} successfully created", "Success").Update(); } else { DebugHelper.WriteLine("Backup: File created is empty"); File.Delete(BACKUP_FILE_PATH); //WE DELETE THE EMPTY FILE CREATED new UpdateMyProcessEvents($"Error while creating {Path.GetFileName(BACKUP_FILE_PATH)}", "Error").Update(); } } }
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); } }
private static void LoadPAKFiles(bool bAllPAKs = false) { if (PAKEntries.PAKEntriesList != null && PAKEntries.PAKEntriesList.Any()) { AssetEntries.ArraySearcher = new Dictionary <string, FPakEntry[]>(); AssetEntries.AssetEntriesDict = new Dictionary <string, PakReader.PakReader>(); //MAIN PAKs LOOP foreach (PAKInfosEntry Pak in PAKEntries.PAKEntriesList.Where(x => !x.bTheDynamicPAK)) { if (!string.IsNullOrEmpty(FProp.Default.FPak_MainAES)) { DebugHelper.WriteLine($".PAKs: Loading {Pak.ThePAKPath} with key: {FProp.Default.FPak_MainAES}"); byte[] AESKey = AESUtility.StringToByteArray(FProp.Default.FPak_MainAES); PakReader.PakReader reader = null; try { reader = new PakReader.PakReader(Pak.ThePAKPath, AESKey); } catch (Exception ex) { DebugHelper.WriteException(ex, Pak.ThePAKPath); if (string.Equals(ex.Message, "The AES key is invalid")) { UIHelper.DisplayError(); } else { new UpdateMyConsole(ex.Message, CColors.Red, true).Append(); return; } break; } if (reader != null) { PAKEntries.PAKToDisplay.Add(Path.GetFileName(Pak.ThePAKPath), reader.FileInfos); if (bAllPAKs) { new UpdateMyProcessEvents($"{Path.GetFileNameWithoutExtension(Pak.ThePAKPath)} mount point: {reader.MountPoint}", "Loading").Update(); } foreach (FPakEntry entry in reader.FileInfos) { AssetEntries.AssetEntriesDict[entry.Name] = reader; AssetEntries.ArraySearcher[entry.Name] = reader.FileInfos; } } } } //DYNAMIC PAKs LOOP foreach (PAKInfosEntry Pak in PAKEntries.PAKEntriesList.Where(x => x.bTheDynamicPAK)) { byte[] AESKey = null; string AESFromManager = string.Empty; if (AESEntries.AESEntriesList != null && AESEntries.AESEntriesList.Any()) { AESFromManager = AESEntries.AESEntriesList.Where(x => string.Equals(x.ThePAKName, Path.GetFileNameWithoutExtension(Pak.ThePAKPath))).Select(x => x.ThePAKKey).FirstOrDefault(); if (!string.IsNullOrEmpty(AESFromManager)) { DebugHelper.WriteLine($".PAKs: Loading {Pak.ThePAKPath} with key: {AESFromManager}"); AESKey = AESUtility.StringToByteArray(AESFromManager); } } if (AESKey != null) { PakReader.PakReader reader = null; try { reader = new PakReader.PakReader(Pak.ThePAKPath, AESKey); } catch (Exception ex) { DebugHelper.WriteException(ex, Pak.ThePAKPath); if (string.Equals(ex.Message, "The AES key is invalid")) { UIHelper.DisplayError(Path.GetFileNameWithoutExtension(Pak.ThePAKPath), AESFromManager); } else { new UpdateMyConsole(ex.Message, CColors.Red, true).Append(); return; } continue; } if (reader != null) { PAKEntries.PAKToDisplay.Add(Path.GetFileName(Pak.ThePAKPath), reader.FileInfos); if (bAllPAKs) { new UpdateMyProcessEvents($"{Path.GetFileNameWithoutExtension(Pak.ThePAKPath)} mount point: {reader.MountPoint}", "Loading").Update(); } foreach (FPakEntry entry in reader.FileInfos) { AssetEntries.AssetEntriesDict[entry.Name] = reader; AssetEntries.ArraySearcher[entry.Name] = reader.FileInfos; } } } else { DebugHelper.WriteLine($".PAKs: No key found for {Pak.ThePAKPath}"); } } AssetTranslations.SetAssetTranslation(FProp.Default.FLanguage); } }
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 AESEncryptNode."); Boolean randomIV = false; if (IV.Value == null) { Byte[] iv = SimpleRandomGenerator.QuickGetRandomBytes(16); IV.SetValue(iv); randomIV = true; } Byte[] inBlock = UnicodeEncoding.Unicode.GetBytes((String)PlainText.Value); DLoggerManager.Instance.Logger.Log(DFramework.Logging.Interfaces.LoggerMessageType.VerboseHigh | DFramework.Logging.Interfaces.LoggerMessageType.Information | DFramework.Logging.Interfaces.LoggerMessageType.Sensitive, "Creating AES encryptor."); 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.EncryptBytes(inBlock, (Byte[])Key.Value, (Byte[])IV.Value); if (randomIV) { using (MemoryStream dataWithIV = new MemoryStream()) { dataWithIV.Write(IV.Value, 0, IV.Value.Length); dataWithIV.Write(outBlock, 0, outBlock.Length); dataWithIV.Flush(); _encryptedData.SetValue(dataWithIV.ToArray()); } } else { _encryptedData.SetValue(outBlock); } retVal = true; } finally { _state = Common.NodeState.Processed; 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); } }
public static string Encode <T>(this T target) { byte[] serialized = JsonConvert.SerializeObject(target).ToBytes().GZipCompress(); return(AESUtility.Encrypt(serialized, "HENA")); }
public static T Decode <T>(string source) { byte[] decrypted = AESUtility.DecryptBytes(source, "HENA").GZipDecompress(); return(JsonConvert.DeserializeObject <T>(ByteExtensions.ToUTF8String(decrypted))); }