/// <summary> /// The command method to encrypt the given data /// </summary> private void Encrypt() { byte[] encrypted; byte[] plainBytes; //Encryption sequence differs according to the selected data format switch (DataInput.DataFormatSelected) { //Encrypt file case Format.File: //Get plain file bytes plainBytes = DataInput.GetBytesFromFile(); //Encrypt encrypted = KeyPairSetup.Encrypt(plainBytes); //Verify that the encryption is successfull if (encrypted != null) { //Create encrypted file path EncryptedFilePath = DataInput.GetEncryptedFilePath(); //Write the encrypted bytes to the new file path File.WriteAllBytes(EncryptedFilePath, encrypted); } break; //Encrypt text case Format.Text: //Encrypt encrypted = KeyPairSetup.Encrypt(DataInput.Data); //Verify that the encryption is successfull if (encrypted != null) { //Comvert the encrypted bytes to hex string EncryptedText = ByteConvert.BytesToHexString(encrypted); } break; //Encrypt hex string case Format.Hex: //Convert hex string to plain bytes plainBytes = ByteConvert.HexStringToBytes(DataInput.Data); //Encrypt encrypted = KeyPairSetup.Encrypt(plainBytes); //Verify that the encryption is successfull if (encrypted != null) { //Convert encrypted bytes to hex string EncryptedText = ByteConvert.BytesToHexString(encrypted); } break; } }
/// <summary> /// The command method to decrypt the given data /// </summary> private void Decrypt() { byte[] encrypted; byte[] decrypted; //Decryption sequence differs according to the selected data format switch (DataInput.DataFormatSelected) { //Decrypt file case Format.File: //Get the encrypted file bytes encrypted = ByteConvert.FileToBytes(EncryptedFilePath); //Decrypt decrypted = KeyPairSetup.Decrypt(encrypted); //true if decrypted not null if (decrypted != null) { //Create an encrypted file path DecryptedFilePath = DataInput.GetDecryptedFilePath(EncryptedFilePath); //Write decrypted bytes to file File.WriteAllBytes(DecryptedFilePath, decrypted); } break; //Decrypt a text case Format.Text: //Convert the encrypted hex string to bytes encrypted = ByteConvert.HexStringToBytes(EncryptedText); //Decrypt as text DecryptedText = KeyPairSetup.DecryptToText(encrypted); break; //Decrypt a hex value case Format.Hex: //Convert the encrypted hex string to bytes encrypted = ByteConvert.HexStringToBytes(EncryptedText); //Decrypt decrypted = KeyPairSetup.Decrypt(encrypted); //true if decrypted not null if (decrypted != null) { //Convert decrypted bytes to hex string DecryptedText = ByteConvert.BytesToHexString(decrypted); } break; } }
/// <summary> /// The command method to verify a signature /// </summary> private void Verify() { var pubKey = File.ReadAllBytes(KeyPairSetup.PublicKeyFilePath); var signature = ByteConvert.HexStringToBytes(OriginalSignature); byte[] data = null; switch (DataInput.DataFormatSelected) { case Format.File: data = File.ReadAllBytes(DataInput.Data); break; case Format.Text: data = ByteConvert.StringToUTF8Bytes(DataInput.Data); break; } SignatureVerified = KeyPairSetup.Verify(signature, pubKey, data); }
/// <summary> /// The command method to sign some data /// </summary> private void Sign() { var privKey = File.ReadAllBytes(KeyPairSetup.PrivateKeyFilePath); byte[] data = null; switch (DataInput.DataFormatSelected) { case Format.File: data = File.ReadAllBytes(DataInput.Data); break; case Format.Text: data = ByteConvert.StringToUTF8Bytes(DataInput.Data); break; } var signature = KeyPairSetup.Sign(privKey, data); OriginalSignature = ByteConvert.BytesToHexString(signature); }
/// <summary> /// The command method to derive a shared secret key from the other party public key and our own keys /// </summary> private void DeriveKey() { var derivedKey = KeyPairSetup.DeriveKey(); DerivedKey = ByteConvert.BytesToHexString(derivedKey); }