private int FinalizeRemove(String filename, String passphrase) { try { core.InitializeKeys(passphrase); } catch { Console.Error.WriteLine("Invalid passphrase"); return(1); } StreamReader sr = new StreamReader(Path.Combine(core.ApplicationDataFolder, "identity")); String username = sr.ReadLine(); String email = sr.ReadLine(); sr.Close(); username.Trim(); email.Trim(); Connect(); ArrayList key = new ArrayList(File.ReadAllBytes(Path.Combine(core.ApplicationDataFolder, "answers.key"))); AESInfo info = new AESInfo(); info.key = (byte[])key.GetRange(0, Crypto.AESKeySize / 8).ToArray(Type.GetType("System.Byte")); info.IV = (byte[])key.GetRange(Crypto.AESKeySize / 8, Crypto.AESIVSize / 8).ToArray(Type.GetType("System.Byte")); Rijndael aes = Rijndael.Create(); String e_macpass = File.ReadAllText(filename); e_macpass = Crypto.StripMessage(e_macpass); byte[] macpass = Crypto.AESDecrypt(Convert.FromBase64String(e_macpass), aes.CreateDecryptor(info.key, info.IV)); HMAC hmac = HMACSHA1.Create(); hmac.Key = macpass; byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes("I want to remove my current public key")); try { if (server.USKeyRem_SendRemoveRequest(username, email, Convert.ToBase64String(hash))) { Console.WriteLine("Removal request successfully sent."); } } catch (Exception e) { Console.Error.WriteLine(e.Message); } File.Delete(Path.Combine(core.ApplicationDataFolder, "answers.key")); return(0); }
private static AESInfo DeriveKeyAndIV(String passphrase, byte[] salt, int keyLen, int IVLen) { const int saltLength = 8; int k = keyLen / 8; int i = IVLen / 8; AESInfo result = new AESInfo(); byte[] passphraseBytes = Encoding.UTF8.GetBytes(passphrase); if (salt == null) { result.salt = new byte[saltLength]; Random random = new Random(); random.NextBytes(result.salt); } else { result.salt = salt; } ArrayList keyAndIV = new ArrayList(k + i); keyAndIV.AddRange(PBKDF2(passphraseBytes, result.salt, 10000, k + i)); result.key = (byte[])keyAndIV.GetRange(0, k).ToArray(Type.GetType("System.Byte")); result.IV = (byte[])keyAndIV.GetRange(k, i).ToArray(Type.GetType("System.Byte")); return(result); }
public static byte[] AESDecrypt(byte[] cipherText, String passphrase, byte[] salt) { Rijndael aes = Rijndael.Create(); AESInfo aesInfo = DeriveKeyAndIV(passphrase, salt, AESKeySize, AESIVSize); return(AESDecrypt(cipherText, aes.CreateDecryptor(aesInfo.key, aesInfo.IV))); }
public static byte[] Decrypt(String cipherText, String privateKey) { Rijndael aes = Rijndael.Create(); AESInfo message = Destruct(cipherText, privateKey); return(AESDecrypt(message.message, aes.CreateDecryptor(message.key, message.IV))); }
public static AESInfo AESEncrypt(byte[] clearText, String passphrase) { Rijndael aes = Rijndael.Create(); AESInfo aesInfo = DeriveKeyAndIV(passphrase, null, AESKeySize, AESIVSize); aesInfo.message = AESEncrypt(clearText, aes.CreateEncryptor(aesInfo.key, aesInfo.IV)); return(aesInfo); }
private String EncryptMACPass(String email, AESInfo aesInfo) { HMAC hmac = HMACSHA1.Create(); Rijndael aes = Rijndael.Create(); DatabaseConnection connection = new DatabaseConnection(); connection.setMACPass(email, Convert.ToBase64String(hmac.Key)); connection.close(); String result = Util.Wrap(Convert.ToBase64String(Crypto.AESEncrypt(hmac.Key, aes.CreateEncryptor(aesInfo.key, aesInfo.IV))), 64); return(result); }
public static AESInfo Destruct(String message, String privateKey) { int ebs = RSAKeySize / 8; AESInfo result = new AESInfo(); ArrayList bytes = new ArrayList(Convert.FromBase64String(StripMessage(message))); byte[] keyPart = (byte[])bytes.GetRange(0, ebs).ToArray(Type.GetType("System.Byte")); result.key = RSADecrypt(keyPart, privateKey); result.IV = RSADecrypt((byte[])bytes.GetRange(ebs, ebs).ToArray(Type.GetType("System.Byte")), privateKey); result.message = (byte[])bytes.GetRange(ebs * 2, bytes.Count - ebs * 2).ToArray(Type.GetType("System.Byte")); return(result); }
private bool EnvelopeAnswers(String userID, String email, String answersEnveloped, String bodyMsg) { DatabaseConnection connection = new DatabaseConnection(); String dbUserid = connection.getUserID(email); //connection.close(); if (userID == null) { ErrorLog_Write(email + ": Email does not exist!"); Console.WriteLine(email + ": Email does not exist!"); throw new Exception("Invalid user"); } if (userID != dbUserid) { ErrorLog_Write(email + ": User id does not exist!"); Console.WriteLine(email + ": User id does not exist!"); throw new Exception("Invalid user"); } Core core = new Core(Server.passphrase); String privateKey = core.PrivateKey; Rijndael aes = Rijndael.Create(); AESInfo aesInfo = Crypto.Destruct(answersEnveloped, privateKey); String answers = Encoding.UTF8.GetString(Crypto.AESDecrypt(aesInfo.message, aes.CreateDecryptor(aesInfo.key, aesInfo.IV))); // connection = new DatabaseConnection(); String dbAnswers = connection.getAnswers(email); connection.close(); if (answers == dbAnswers) { SendMail(email, aesInfo, bodyMsg); return(true); } else { //protocol stops and socket is closed. ErrorMail(email); ErrorLog_Write("Error - " + email + ": Answers are not correct!"); Console.WriteLine("Error - " + email + ": Answers are not correct!"); throw new Exception("Answers are not correct"); } }
private void SendMail(String email, AESInfo aesInfo, String bodyMsg) { String macPassword_encrypted = EncryptMACPass(email, aesInfo); String subject = "PractiSES notification"; StringBuilder body = new StringBuilder(bodyMsg); body.AppendLine(); body.AppendLine(Crypto.BeginMessage); body.AppendLine(); body.AppendLine(macPassword_encrypted); body.AppendLine(Crypto.EndMessage); Email mailer = new Email(email, subject, body.ToString()); //recepient, subject, body if (mailer.Send()) { ActionLog_Write(email + ": Mail sent."); Console.WriteLine(email + ": Mail sent."); } }
private static AESInfo DeriveKeyAndIV(String passphrase, byte[] salt, int keyLen, int IVLen) { const int saltLength = 8; int k = keyLen/8; int i = IVLen/8; AESInfo result = new AESInfo(); byte[] passphraseBytes = Encoding.UTF8.GetBytes(passphrase); if (salt == null) { result.salt = new byte[saltLength]; Random random = new Random(); random.NextBytes(result.salt); } else { result.salt = salt; } ArrayList keyAndIV = new ArrayList(k + i); keyAndIV.AddRange(PBKDF2(passphraseBytes, result.salt, 10000, k + i)); result.key = (byte[]) keyAndIV.GetRange(0, k).ToArray(Type.GetType("System.Byte")); result.IV = (byte[]) keyAndIV.GetRange(k, i).ToArray(Type.GetType("System.Byte")); return result; }
public static AESInfo Destruct(String message, String privateKey) { int ebs = RSAKeySize/8; AESInfo result = new AESInfo(); ArrayList bytes = new ArrayList(Convert.FromBase64String(StripMessage(message))); byte[] keyPart = (byte[]) bytes.GetRange(0, ebs).ToArray(Type.GetType("System.Byte")); result.key = RSADecrypt(keyPart, privateKey); result.IV = RSADecrypt((byte[]) bytes.GetRange(ebs, ebs).ToArray(Type.GetType("System.Byte")), privateKey); result.message = (byte[]) bytes.GetRange(ebs*2, bytes.Count - ebs*2).ToArray(Type.GetType("System.Byte")); return result; }
private int FinalizeUpdate(String filename, String passphrase) { try { core.InitializeKeys(passphrase); } catch { Console.Error.WriteLine("Invalid passphrase"); return 1; } StreamReader sr = new StreamReader(Path.Combine(core.ApplicationDataFolder, "identity")); String username = sr.ReadLine(); String email = sr.ReadLine(); sr.Close(); username.Trim(); email.Trim(); Connect(); ArrayList key = new ArrayList(File.ReadAllBytes(Path.Combine(core.ApplicationDataFolder, "answers.key"))); AESInfo info = new AESInfo(); info.key = (byte[]) key.GetRange(0, Crypto.AESKeySize/8).ToArray(Type.GetType("System.Byte")); info.IV = (byte[]) key.GetRange(Crypto.AESKeySize/8, Crypto.AESIVSize/8).ToArray(Type.GetType("System.Byte")); Rijndael aes = Rijndael.Create(); String e_macpass = File.ReadAllText(filename); e_macpass = Crypto.StripMessage(e_macpass); byte[] macpass = Crypto.AESDecrypt(Convert.FromBase64String(e_macpass), aes.CreateDecryptor(info.key, info.IV)); HMAC hmac = HMACSHA1.Create(); hmac.Key = macpass; byte[] hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(core.PublicKey)); if (server.USKeyUpdate_SendPublicKey(username, email, core.PublicKey, Convert.ToBase64String(hash))) { Console.WriteLine("Public key successfully sent."); } else { Console.WriteLine("Public key could not be sent, please try again."); } File.Delete(Path.Combine(core.ApplicationDataFolder, "answers.key")); return 0; }
private String EncryptMACPass(String email, AESInfo aesInfo) { HMAC hmac = HMACSHA1.Create(); Rijndael aes = Rijndael.Create(); DatabaseConnection connection = new DatabaseConnection(); connection.setMACPass(email, Convert.ToBase64String(hmac.Key)); connection.close(); String result = Util.Wrap(Convert.ToBase64String(Crypto.AESEncrypt(hmac.Key, aes.CreateEncryptor(aesInfo.key, aesInfo.IV))), 64); return result; }
private static void WriteKey(String path, String key, String passphrase) { AESInfo info = Crypto.AESEncrypt(Encoding.ASCII.GetBytes(key), passphrase); File.WriteAllText(path, Convert.ToBase64String(Util.Join(info.salt, info.message))); }