public void Encrypt_And_Decrypt() { // arrange var pair = _service.GenerateKeyPairPem(); var secret = Encoding.UTF8.GetBytes(Secret); var encryptedData = _service.Encrypt(secret, pair.GetPublicKey()); var decryptedData = _service.Decrypt(encryptedData, pair.GetPrivateKey()); // assert var decryptedDataBase64 = Encoding.UTF8.GetString(decryptedData); Assert.Equal(Secret, decryptedDataBase64); }
private static void TestEncription2() { var algo = new AsymmetricEncryptionService(); var readerPrivate = new StreamReader("private-key-test.txt"); var readerPublic = new StreamReader("public-key-test.txt"); var privateStr = readerPrivate.ReadToEnd(); var publicStrJson = readerPublic.ReadToEnd(); var publicStr = JsonConvert.DeserializeObject <KeyJson>(publicStrJson).Key; var service = new SymmetricEncryptionService(); var buf = service.GenerateKey(); var enc = algo.Encrypt(buf, publicStr); var str = Convert.ToBase64String(enc); Console.WriteLine(Convert.ToBase64String(buf)); Console.WriteLine(); Console.WriteLine(str); Console.WriteLine(); enc = Convert.FromBase64String("ov0P6xO2AXxLJe7DgMmfjXjCPC488wAElzbTVy+N7/6Q4g0ld6iRnuXtI2FN06ym/loHSHH7sy9375xPoHy3bDp1jCcs1FrClnynlWUO5c2Xq5M1mQBYlcg3u5OG4wgttHtXa9/cftm1B9hl/Nh9ItSwKI/br61dlT+gebZzOO0="); algo = new AsymmetricEncryptionService(); var res = algo.Decrypt(enc, privateStr); Console.WriteLine(Convert.ToBase64String(res)); Console.ReadLine(); }
private static void TestEncription() { var algo = new AsymmetricEncryptionService(); var readerPrivate = new StreamReader("private-key-test.txt"); var readerPublic = new StreamReader("public-key-test.txt"); var privateStr = readerPrivate.ReadToEnd(); var publicStrJson = readerPublic.ReadToEnd(); var publicStr = JsonConvert.DeserializeObject <KeyJson>(publicStrJson).Key; var buf = Encoding.UTF8.GetBytes("Hello world!"); var enc = algo.Encrypt(buf, publicStr); var str = Convert.ToBase64String(enc); Console.WriteLine(); Console.WriteLine(str); Console.WriteLine(); var res = algo.Decrypt(enc, privateStr); var txt = Encoding.UTF8.GetString(res); Console.WriteLine(txt); var s64 = "Y1hLuvDfQ4Dr71zIBYwcOPIfpzIMo+RWpgaY3A51s4xS1NeHcrWMCfMr4qq8d0mQbotx4g0UXu8Y4yTZSuYMeMW8ezjzGpbzV8aikk1Skc72OnmUuwt8/ns/HVQmMwYumn0VlKGiJMKiFOUHROUBC3D1bAv1L363qnu1Vmiifn8="; var d1 = Convert.FromBase64String(s64); res = algo.Decrypt(enc, privateStr); txt = Convert.ToBase64String(res); Console.WriteLine(txt); Console.ReadLine(); }
public override async Task <PingResponse> GetPing(PingRequest request, ServerCallContext context) { var validatorId = context.GetHttpContext().User.GetClaimOrDefault(Claims.KeyKeeperId); var publicKey = context.GetHttpContext().User.GetClaimOrDefault(Claims.PublicKeyPem); if (string.IsNullOrEmpty(publicKey)) { return(new PingResponse() { Error = new ValidatorApiError() { Code = ValidatorApiError.Types.ErrorCodes.InternalServerError, Message = "Incorrect Bearer Token." } }); } var message = _pingMessageReader.Get(PingMessageMyNoSqlEntity.GeneratePartitionKey(validatorId), PingMessageMyNoSqlEntity.GenerateRowKey()); var response = new PingResponse(); if (message == null) { response.MessageEnc = string.Empty; response.SignatureMessage = string.Empty; } else { var asynccrypto = new AsymmetricEncryptionService(); var messageEnc = asynccrypto.Encrypt(Encoding.UTF8.GetBytes(message.Message), publicKey); response.MessageEnc = Convert.ToBase64String(messageEnc); response.SignatureMessage = "not-implemented-please-skip"; await _pingMessageWriter.DeleteAsync(message.PartitionKey, message.RowKey); } _logger.LogInformation("GetPing response. ValidatorId='{ValidatorId}'; HasMessage={HasMessage}", validatorId, !string.IsNullOrEmpty(response.MessageEnc)); return(response); }