private static SecureSession CreateSession(string identifier, Instant instant = null) { if (instant == null) { instant = new Instant(); } DateTime now = DateTime.UtcNow; SecureSession result = new SecureSession(); result.Identifier = identifier; result.CreationDate = now; result.LastActivity = now; result.TimeOffset = instant.DiffInMilliseconds(now); result.IsActive = true; AsymmetricCipherKeyPair keys = RsaKeyGen.GenerateKeyPair(DefaultKeySize); result.AsymmetricKey = keys.ToPem(); AesKeyVectorPair kvp = new AesKeyVectorPair(); result.SymmetricKey = kvp.Key.EncryptWithPublicKey(keys.Public); result.SymmetricIV = kvp.IV.EncryptWithPublicKey(keys.Public); result.Save(); return(result); }
public void EncryptTest() { string value = "This is a string: ".RandomLetters(16); AsymmetricCipherKeyPair keys = RsaKeyGen.GenerateKeyPair(RsaKeyLength._1024); string publicPem = keys.PublicKeyToPem(); string pemString = keys.ToPem(); string encrypted = value.EncryptWithPublicKey(keys); OutLine(encrypted, ConsoleColor.Cyan); string decrypted = encrypted.DecryptWithPrivateKey(pemString); OutLine(decrypted, ConsoleColor.Green); Expect.AreEqual(value, decrypted); }
/// <summary> /// Create a Vault in the specified database by the specified /// name using the specified password to create it if it /// doesn't exist /// </summary> /// <param name="database"></param> /// <param name="name"></param> /// <param name="password"></param> /// <param name="rsaKeyLength"></param> /// <returns></returns> public static Vault Create(Database database, string name, string password, RsaKeyLength rsaKeyLength = RsaKeyLength._1024) { Vault result = Vault.OneWhere(c => c.Name == name, database); if (result == null) { result = new Vault(); result.Name = name; result.Save(database); VaultKey key = result.VaultKeysByVaultId.JustOne(database, false); AsymmetricCipherKeyPair keys = RsaKeyGen.GenerateKeyPair(rsaKeyLength); key.RsaKey = keys.ToPem(); key.Password = password.EncryptWithPublicKey(keys); key.Save(database); } return(result); }