public static void GetData() { client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); ConnectToServer(); SendMessage("3;" + App.compID.ToString()); ReceiveMessage(); string decryptedResponse = aesManager.Decrypt(Convert.FromBase64String(response)); data.Companies = JsonConvert.DeserializeObject <ObservableCollection <Company> >(decryptedResponse.Split(";")[0]); data.Accounts = JsonConvert.DeserializeObject <ObservableCollection <Account> >(decryptedResponse.Split(";")[1]); App.chartData = JsonConvert.DeserializeObject <ChartData>(decryptedResponse.Split(";")[2]); Disconnect(); }
/// <summary> /// Reads a keyName and passphrase from the server and attempts to answer it. /// </summary> /// <param name="client"></param> /// <param name="packet"></param> internal static void Passphrase(this ClusterClient client, Packet packet) { string keyName = packet.ReadString(); byte[] cypher = Convert.FromBase64String(packet.ReadString()); byte[] iv = Convert.FromBase64String(packet.ReadString()); client.AnswerPassphrase(AESManager.Decrypt(keyName, cypher, iv)); }
private void btnAESDecrypt_Click(object sender, EventArgs e) { if (this.IsAESValid()) { if (!string.IsNullOrEmpty(this.txtAESEncryptedText.Text)) { AESManager certificateManager = new AESManager(this.txtAESEncryptionKey.Text, this.txtAESSalt.Text); this.txtAESClearText.Text = certificateManager.Decrypt(this.txtAESEncryptedText.Text); } } }
public string DecryptString(string encryptedString, string decryptionKey, string decryptionSalt) { string retVal = string.Empty; if (!string.IsNullOrEmpty(encryptedString)) { AESManager internalManager = new AESManager(decryptionKey, decryptionSalt); retVal = internalManager.Decrypt(encryptedString); } return(retVal); }
/// <summary> /// Decrypt to a PucksAndProgramming User /// </summary> /// <param name="encryptedString">The encrypted string</param> /// <returns>A PucksAndProgramming user instance</returns> public PucksAndProgramming.Common.DomainModel.User Decrypt(string encryptedString) { PucksAndProgramming.Common.DomainModel.User retVal = null; if (!string.IsNullOrEmpty(encryptedString)) { AESManager encryptor = new AESManager(this.EncryptionKey, this.Salt); string decryptedValue = encryptor.Decrypt(encryptedString); retVal = PucksAndProgramming.Common.Utilities.SerializationUtilities.DeserializeXmlToObject <PucksAndProgramming.Common.DomainModel.User>(decryptedValue); } return(retVal); }
/// <summary> /// This method decrypts a string using the configuration settings supplied /// </summary> /// <param name="encryptedString">The encrypted string</param> /// <returns>The passed in string, decrypted</returns> public string DecryptString(string encryptedString) { string retVal = string.Empty; if (!string.IsNullOrEmpty(encryptedString)) { switch (this.EncryptionMethod) { case EncryptionMethodOptions.None: retVal = encryptedString; break; case EncryptionMethodOptions.AES: AESConfiguration aesconfiguration = AESConfiguration.GetInstance(); AESManager aesencryption = new AESManager(aesconfiguration.EncryptionKey, aesconfiguration.Salt); retVal = aesencryption.Decrypt(encryptedString); break; case EncryptionMethodOptions.CertificateKeyFile: KeyFileConfiguration keyfileConfiguration = KeyFileConfiguration.GetInstance(); X509CertificateManager keyfileEncryption = new X509CertificateManager(keyfileConfiguration.KeyFile, keyfileConfiguration.KeyFilePassword); retVal = keyfileEncryption.Decrypt(encryptedString); break; case EncryptionMethodOptions.CertificateKeyStore: KeyStoreConfiguration keystoreConfiguration = KeyStoreConfiguration.GetInstance(); X509CertificateManager keystoreEncryption = new X509CertificateManager(keystoreConfiguration.StoreName, keystoreConfiguration.StoreLocation, keystoreConfiguration.CertificateName); retVal = keystoreEncryption.Decrypt(encryptedString); break; case EncryptionMethodOptions.RSAXmlKeyFile: RSAXmlKeyFileConfiguration rsaxmlKeyFileConfiguration = RSAXmlKeyFileConfiguration.GetInstance(); RSAXmlKeyFileManager rsaxmlKeyFileEncryption = new RSAXmlKeyFileManager(rsaxmlKeyFileConfiguration.PublicKeyFile, rsaxmlKeyFileConfiguration.PrivateKeyFile); retVal = rsaxmlKeyFileEncryption.Decrypt(encryptedString); break; } } return(retVal); }
static void Main(string[] args) { /*AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); * ICryptoTransform encryptor = aes.CreateEncryptor(); * MemoryStream ms = new MemoryStream(); * CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write); * * cs.Dispose(); * * try * { * ms.Write(new byte[] { 0, 0, 0, 0 }, 0, 4); * } * catch (Exception e) * { * Console.WriteLine($"1. {e.Message}"); * } * * try * { * encryptor.TransformFinalBlock(new byte[] { 0, 0, 0, 0 }, 0, 4); * } * catch (Exception e) * { * Console.WriteLine($"2. {e.Message}"); * } * * try * { * aes.CreateDecryptor(); * } * catch (Exception e) * { * Console.WriteLine($"3. {e.Message}"); * }*/ byte[] buffer = new byte[81920]; byte[] decrypted; DateTime printed = DateTime.Now; TimeSpan printSpan = new TimeSpan(0, 0, 1); int read; using (FileStream sStream = File.OpenRead(SEND_PATH)) using (FileStream rStream = File.Create(RECEIVE_PATH)) using (AESManager aes1 = new AESManager()) using (AESManager aes2 = new AESManager(aes1.KeyIVPair)) { while (sStream.Position != sStream.Length) { read = sStream.Read(buffer, 0, 81920); decrypted = aes2.Decrypt(aes1.Encrypt(buffer)); rStream.Write(decrypted, 0, read); if (DateTime.Now - printed > printSpan) { Console.WriteLine($"{sStream.Position}/{sStream.Length} ({(double)sStream.Position / (double)sStream.Length}) 완료..."); printed = DateTime.Now; } } } Console.WriteLine("파일이 손상없이 암/복호화되었는지 확인합니다..."); FileInfo sent = new FileInfo(SEND_PATH); FileInfo received = new FileInfo(RECEIVE_PATH); if (sent.Length != received.Length) { Console.WriteLine($"파일의 길이가 다릅니다. (전송한 파일 : {sent.Length}, 받은 파일 : {received.Length})"); return; } byte[] sBuffer = new byte[81920], rBuffer = new byte[81920]; try { using (FileStream sStream = sent.OpenRead()) using (FileStream rStream = received.OpenRead()) { while (sStream.Position != sStream.Length) { sStream.Read(sBuffer, 0, 81920); read = rStream.Read(rBuffer, 0, 81920); for (int i = 0; i < read; ++i) { if (sBuffer[i] != rBuffer[i]) { throw new Exception($"{(sStream.Position - read + i)}에서 ({sStream.Position - 81920} ~ {sStream.Position}) 불일치를 발견하였습니다. (전체 길이 {sent.Length})"); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); return; } Console.WriteLine("파일이 일치합니다."); Console.ReadKey(false); }