private void bulkTest(PrivateKeyManager privateKey, CertificateX509 cert, string hashAlgorithm) { string enc = Global.GLOBAL_ENCODING; if (SecurityUtils.compareStrings(enc, "UTF_32") || SecurityUtils.compareStrings(enc, "UTF_32BE") || SecurityUtils.compareStrings(enc, "UTF_32LE")) { plainText = eu.getString(eu.getBytes(plainText32)); } else { plainText = eu.getString(eu.getBytes(plainText16)); } for (int p = 0; p < arrayPaddings.Length; p++) { AsymmetricCipher asymCipher = new AsymmetricCipher(); // AsymmetricCipher asymCipherD = new AsymmetricCipher(); string encrypted1 = asymCipher.DoEncrypt_WithPrivateKey(hashAlgorithm, arrayPaddings[p], privateKey, plainText); string decrypted1 = asymCipher.DoDecrypt_WithPublicKey(hashAlgorithm, arrayPaddings[p], cert, encrypted1); Assert.AreEqual(decrypted1, plainText); True(SecurityUtils.compareStrings(decrypted1, plainText), asymCipher); string encrypted2 = asymCipher.DoEncrypt_WithPublicKey(hashAlgorithm, arrayPaddings[p], cert, plainText); string decrypted2 = asymCipher.DoDecrypt_WithPrivateKey(hashAlgorithm, arrayPaddings[p], privateKey, encrypted2); Assert.IsTrue(SecurityUtils.compareStrings(decrypted2, plainText)); True(SecurityUtils.compareStrings(decrypted2, plainText), asymCipher); } }
protected override void EstablishConnection(NetworkMessage message) { //Generate symmetric key + IV, save it and encrypt it with the given public assymetric key and send it to the client. try { if (message.TryGetObject <string>(out string publicKey)) { AsymmetricCipher asymmetric = new AsymmetricCipher(); asymmetric.LoadPublicKey(publicKey); cipher = new SymmetricCipher(); SymmetricKey key = new SymmetricKey { Key = asymmetric.Encrypt(cipher.Key), IV = asymmetric.Encrypt(cipher.IV) }; Send(MessageProtocols.Setup, key, false); Send(MessageProtocols.SetUsername, Username); server.Broadcast(MessageProtocols.Connect, this, Username); // Tell users that it connected Send(MessageProtocols.Users, server.ConnectedUsers().ToArray()); } else { throw new Exception("Failed to load the public key"); } } catch (Exception) { Terminate(); } }
public static void TestAsymmetricSign1() { var text = "非对称签名测试1"; Console.WriteLine(text); var cipher = new AsymmetricCipher(AsymmetricAlgorithm.Rsa); Console.WriteLine("=====================publicKey================"); Console.WriteLine(cipher.Base64PublicKey); Console.WriteLine(); Console.WriteLine("=====================privateKey================"); Console.WriteLine(cipher.Base64PrivateKey); Console.WriteLine(); var data = Encoding.UTF8.GetBytes(text); var sign = cipher.Sign(data); Console.WriteLine($"sign:{Convert.ToBase64String(sign)}"); var verify = cipher.Verify(data, sign); Console.WriteLine($"verify:{verify}"); }
private void GenerateAsymmetricCipher(AsymmetricCipherGenerator acg, int lenIndex) { int cipherSize = acg.CipherSize[lenIndex]; AsymmetricCipher cipher = acg.Generate(cipherSize); AsymmetricResult asymmetricResult = (AsymmetricResult)this.resultContentControl.Content; asymmetricResult.Cipher = cipher; }
/// <summary> /// Ends handshake by sending the symmetric iv and key to the remote peer /// </summary> private void EndHandshake(byte[] data) { AsymmetricCipher.PublicKey = data; Connection.SendData(AsymmetricCipher.Encrypt(SymmetricCipher.IV)); Connection.SendData(AsymmetricCipher.Encrypt(SymmetricCipher.Key)); handshakeDone = true; OnConnected?.Invoke(); }
public virtual void SetUp() { path_RSA_sha1_1024 = Path.Combine(BASE_PATH, "dummycerts", "RSA_sha1_1024"); plainText = "Lorem ipsum"; pathKey = Path.Combine(path_RSA_sha1_1024, "sha1d_key.pem"); pathCert = Path.Combine(path_RSA_sha1_1024, "sha1_cert.crt"); key = new PrivateKeyManager(); cert = new CertificateX509(); asymCipher = new AsymmetricCipher(); }
/// <summary> /// Ends handshake after receiving both IV and PublicKey. /// </summary> private void EndHandshake(byte[] data) { if (!receivedIV) { SymmetricCipher.IV = AsymmetricCipher.Decrypt(data); receivedIV = true; } else { SymmetricCipher.Key = AsymmetricCipher.Decrypt(data); handshakeDone = true; OnConnected?.Invoke(); } }
public static void TestAsymmetricEncrypt2() { var data = "非对称加密测试2"; Console.WriteLine(data); var publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjoHfRKSPSURhRYJu9UtgkvP2TFjOI4fHbXUmsBgIU1TiNugP/JepY5lMZ6ISF3zg0QO5DTJ09god76BZVFKnKAsWA4Dqcv3bNVUneSZygtsB/SCzjUQ9o8bZkiCd5vaAOER/Z6g75jtl8XGtjE7GtQ/ezd37JQR7xZ2axZrWtdQIDAQAB"; var privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKOgd9EpI9JRGFFgm71S2CS8/ZMWM4jh8dtdSawGAhTVOI26A/8l6ljmUxnohIXfODRA7kNMnT2Ch3voFlUUqcoCxYDgOpy/ds1VSd5JnKC2wH9ILONRD2jxtmSIJ3m9oA4RH9nqDvmO2Xxca2MTsa1D97N3fslBHvFnZrFmta11AgMBAAECgYBGaYVmApghpzgZvMMII6BTnuhX5VPj8acMSQas+iDnKiIeCxAxOfWwr9zO51ov6bDb+50MZOm9UHBRB7ykfDHbxkFXCLA3Tesr2/X3+0Iyz30pu1ctCOcjlJaHQpnE09okYI9kZpkTzzK3Hkm59wE/9SOor5Ag70hrXcAApwIceQJBANKXscBScD6nySdmbxBjfPc+UwSEFIwb+XYey3hJHNY9rKuA4opRTTWMB9JaFfgeXeRP8y5Fd+B8Y1R6n9z5KKcCQQDG6FsBtEmj6mVL/ZPr2JeUAo1Z7VH9uB6bG5o2sTeeERAz3GSqZd+K7s/LSjbJ9XbIuGHoFrE4wT+uIfwD9yCDAkAyUVKEXG47WkXC50PES7ExNjAJ1TE/pPN/GK6PKBD+06+tLtdyKyjikXnQ9ftn1IGkqsG1HZ4eAjqNldsapmHjAkApMEZgJPg21Dvjr3/pD7HbuWeR3p3i3zSfQ+j8OFhfCAOF6baCvpO6zlcDLrwHuCe/ysaja8eJDCNmqKzqGUuHAkBlTjExgc35J2TY1pn4FE1pqj8Yavll4vfiDhIrRwLdVJyKq8Sjm4OzVFAkk3rMkqPR/ZL3cxb45RMmtGkYwVQ9"; var cipher = new AsymmetricCipher(AsymmetricAlgorithm.Rsa, publicKey, privateKey); var encrypted = cipher.Encrypt(Encoding.UTF8.GetBytes("非对称加密测试2")); Console.WriteLine($"encrypted:{Convert.ToBase64String(encrypted)}"); // encrypted = Convert.FromBase64String("js3wD7eeMtEVyZvO0HrvlRU5esVrmXOyW21c+woVC5r9no1xm3KT1IBMcrGPWj8YL1NIjdBW+Qfq81bCRC1Oqsxou/5IZOIV4JFSnY0k1yxF+8TH9IWXsfU/nrFyQagQrJZ3gGG7NygIM6dxK8StsCLTVNmJX8vxvwPNqITql1A="); var decrypted = cipher.Decrypt(encrypted); Console.WriteLine($"decrypted:{Encoding.UTF8.GetString(decrypted)}"); }
public static void TestAsymmetricEncrypt1() { var data = "非对称加密测试1"; Console.WriteLine(data); var cipher = new AsymmetricCipher(AsymmetricAlgorithm.Rsa); Console.WriteLine("=====================publicKey================"); Console.WriteLine(cipher.Base64PublicKey); Console.WriteLine(); Console.WriteLine("=====================privateKey================"); Console.WriteLine(cipher.Base64PrivateKey); Console.WriteLine(); var encrypted = cipher.Encrypt(Encoding.UTF8.GetBytes(data)); Console.WriteLine($"encrypted:{Convert.ToBase64String(encrypted)}"); var decrypted = cipher.Decrypt(encrypted); Console.WriteLine($"decrypted:{Encoding.UTF8.GetString(decrypted)}"); }
public static void TestAsymmetricSign2() { var text = "非对称签名测试2"; Console.WriteLine(text); var publicKey = "MIIBtzCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoDgYQAAoGABmpaSji+l7CBZ9a3wtEHMWwpCUnZy66wGAff5fZuAHpPRB64ljSn0OAcBwhHBcjVBKvP+XUDExkACvAm//oz0bpbPzUeslw2yIELJDRr0oXFZCbJpPZWUBw9v4CiiWUBMoDMggjXQ8MNcLrZkOXK8q/jEJbLXrApFQb1AOmLf4g="; var privateKey = "MIIBSwIBADCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAhUAl2BQjxUjC8yykrmCouuEC/BYHPUCgYEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoEFgIUDzw/Nh7Wa4Wg4NnquitP0NgQ1+0="; var cipher = new AsymmetricCipher(AsymmetricAlgorithm.Dsa, publicKey, privateKey); var data = Encoding.UTF8.GetBytes(text); var signature = cipher.Sign(data); Console.WriteLine($"signature:{Convert.ToBase64String(signature)}"); signature = Convert.FromBase64String("MCwCFGSjitt2rP7A8p3mcFmjjkuvAsCUAhQvlzprmCWVqEbLrs928Kl2QA2h2g=="); var verification = cipher.Verify(data, signature); Console.WriteLine($"verification:{verification}"); }
public AsymmetricAlgorithm GetAsymmetricCipher(AsymmetricCipher cipher) { return AsymmetricCipherBuilders[cipher].Build(); }
private void SetupConnection() { asymCipher = new AsymmetricCipher(); Send(MessageProtocols.Setup, asymCipher.PublicKey()); }