2020-05-24 08:40:51 +00:00
|
|
|
using System.IO;
|
2020-05-21 09:05:32 +00:00
|
|
|
using System;
|
2020-05-26 09:16:49 +00:00
|
|
|
using System.Text;
|
2020-05-25 08:52:39 +00:00
|
|
|
using Sodium;
|
2020-05-26 09:16:49 +00:00
|
|
|
using niceware;
|
|
|
|
|
2020-05-28 22:14:45 +00:00
|
|
|
using simplepack;
|
2020-05-26 09:16:49 +00:00
|
|
|
using chestcrypto.symmetric;
|
|
|
|
|
2020-05-21 09:05:32 +00:00
|
|
|
using treasurechest.STDIOWrapper;
|
|
|
|
|
2020-05-23 09:51:17 +00:00
|
|
|
namespace treasurechestCLI{
|
2020-05-21 09:05:32 +00:00
|
|
|
|
2020-05-23 09:51:17 +00:00
|
|
|
internal class EncryptMessageInterface{
|
2020-05-28 22:14:45 +00:00
|
|
|
private static void EncryptWithMnemonic(){
|
|
|
|
byte[] key = new byte[32]; // Key has to be 32 bytes in size
|
|
|
|
byte[] message; // Plaintext
|
|
|
|
string encrypted; // Ciphertext will be encoded with SimplePack.
|
|
|
|
try {
|
|
|
|
message = UTF8Encoding.UTF8.GetBytes(GetMessage.getTypedMessage());
|
|
|
|
}
|
|
|
|
catch(System.NullReferenceException){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SimplePack packer = new SimplePack("treasure chest-message ", " end treasure chest message.");
|
|
|
|
|
|
|
|
key = SecretBox.GenerateKey();
|
|
|
|
encrypted = packer.encode(Symmetric.encrypt(message, key));
|
|
|
|
STDIO.O(encrypted);
|
|
|
|
foreach (string word in Niceware.ToPassphrase(key)){
|
|
|
|
Console.Write(word + " ");
|
|
|
|
}
|
|
|
|
STDIO.O("");
|
|
|
|
}
|
|
|
|
|
2020-05-23 09:51:17 +00:00
|
|
|
public static void EncryptMessage(){
|
2020-05-21 09:05:32 +00:00
|
|
|
int choice = 0;
|
2020-05-23 09:51:17 +00:00
|
|
|
int counter = 1;
|
2020-05-24 08:40:51 +00:00
|
|
|
|
2020-05-23 09:51:17 +00:00
|
|
|
translations.Strings strings = new translations.Strings();
|
2020-05-21 09:05:32 +00:00
|
|
|
|
2020-05-23 09:51:17 +00:00
|
|
|
string[] encryptMenuOptions =
|
|
|
|
{
|
|
|
|
strings.ENCRYPT_MENU_USE_PASSPHRASE,
|
|
|
|
strings.ENCRYPT_MENU_USE_PUBKEY,
|
|
|
|
strings.RETURN_TO_PREVIOUS_MENU
|
|
|
|
};
|
|
|
|
while(true){
|
2020-05-24 08:40:51 +00:00
|
|
|
counter = 1;
|
2020-05-23 09:51:17 +00:00
|
|
|
foreach(string option in encryptMenuOptions){
|
2020-05-21 09:05:32 +00:00
|
|
|
STDIO.O(counter.ToString() + ". " + option);
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
choice = Int32.Parse(System.Console.ReadLine());
|
|
|
|
}
|
|
|
|
catch (System.OverflowException){
|
|
|
|
// User being silly with input
|
|
|
|
STDIO.O(strings.MAIN_MENU_SELECT_INTEGER);
|
|
|
|
counter = 1;
|
|
|
|
}
|
|
|
|
catch(System.FormatException){
|
|
|
|
// Too lazy to check strings, force them to use int from menu which is faster anyway
|
|
|
|
STDIO.O(strings.MAIN_MENU_SELECT_INTEGER);
|
|
|
|
counter = 1;
|
|
|
|
}
|
|
|
|
catch(System.ArgumentNullException){
|
|
|
|
// Can happen when stream closes (e.g. ctrl-d)
|
|
|
|
// since menu is intended to be directly human interfaced, user probably wants to exit
|
2020-05-23 09:51:17 +00:00
|
|
|
choice = encryptMenuOptions.Length;
|
2020-05-21 09:05:32 +00:00
|
|
|
}
|
2020-05-23 09:51:17 +00:00
|
|
|
if (choice == 1){
|
2020-05-28 22:14:45 +00:00
|
|
|
EncryptWithMnemonic();
|
|
|
|
}
|
|
|
|
else if (choice == 2){
|
2020-05-25 08:52:39 +00:00
|
|
|
|
2020-05-21 09:05:32 +00:00
|
|
|
}
|
2020-05-28 22:14:45 +00:00
|
|
|
else if (choice == 3){
|
2020-05-23 09:51:17 +00:00
|
|
|
break;
|
2020-05-21 09:05:32 +00:00
|
|
|
}
|
2020-05-23 09:51:17 +00:00
|
|
|
|
2020-05-21 09:05:32 +00:00
|
|
|
}
|
2020-05-23 09:51:17 +00:00
|
|
|
|
2020-05-21 09:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|