public void EncryptDecryptBlob_WithPrivateKey() { using (var options = new SymmetricEncryptionProviderOptions()) using (var engine = new SymmetricEncryptionProvider(options)) { byte[] privateKey = null; using (var rng = new RandomNumberGenerator()) { privateKey = rng.NextBytes(20); } ReadOnlySpan <byte> text = Utf8.NoBom.GetBytes("My name Jeff"); var encryptedBlob = engine.Encrypt(text, privateKey); assert.True(!encryptedBlob.IsEmpty); assert.True(encryptedBlob != text); var data = new byte[encryptedBlob.Length]; encryptedBlob.CopyTo(data); var text2 = engine.Decrypt(encryptedBlob, privateKey); assert.True(!text2.IsEmpty); assert.Equal(text.Length, text2.Length); for (var i = 0; i < text2.Length; i++) { assert.Equal(text[i], text2[i]); } } }
public static void HashTokenWithSalt() { HashingProvider provider = new HashingProvider(); Console.WriteLine(provider.ComputeHashWithSalt("b$7SDt%43J*a!9", "SHA256", null)); PackingService service = new PackingService(new NumberDataGenerator()); Token t = new Token(); t.Data = "MXLBEcLe6/i1CjdyomC7T0vTlACTXpdRmnxcDXDE8yDuCal0xA=="; t.Timestamp = DateTime.Now; Console.WriteLine(service.PackToken(t)); SymmetricEncryptionProvider encryption = new SymmetricEncryptionProvider(); EncryptionInfo ei = new EncryptionInfo(); ei.HashAlgorithm = "SHA1"; ei.InitVector = "a01JQ3481Ahnqwe9"; ei.Iterations = 2; ei.KeySize = 256; ei.PassPhrase = "Da*eW6_EzU4_swuk8*hU"; ei.SaltValue = "VuW9uDrE"; Console.WriteLine(encryption.Encrypt("861641072009MXLBEcLe6/i1CjdyomC7T0vTlACTXpdRmnxcDXDE8yDuCal0xA==41410860", ei)); Console.WriteLine(); Console.WriteLine("Press enter to exit."); Console.ReadLine(); }
public void EncryptStringTest() { EncryptionInfo info = new EncryptionInfo(); info.PassPhrase = "Pas5pr@se"; // can be any string info.SaltValue = "s@1tValue"; // can be any string info.HashAlgorithm = "SHA1"; // can be "MD5" info.Iterations = 2; // can be any number info.InitVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes info.KeySize = 256; // can be 192 or 128 SymmetricEncryptionProvider provider = new SymmetricEncryptionProvider(); string cipherText2 = provider.Encrypt(plainText, info); Assert.IsNotNull(cipherText2); Assert.AreEqual(cipherText2, cipherText); }