From 1b6abbdbe44736cd2d8dd0dfd2d8e202f3345908 Mon Sep 17 00:00:00 2001 From: kohait Date: Thu, 5 Aug 2010 19:01:00 +0000 Subject: [PATCH] bazaar: DeEncrypter, an AESStream bases app, small key generater & Encrypter / Decrypter for multiple files git-svn-id: svn://ultimatepp.org/upp/trunk@2594 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- bazaar/DeEncrypter/DeEncrypter.h | 38 ++++ bazaar/DeEncrypter/DeEncrypter.lay | 10 + bazaar/DeEncrypter/DeEncrypter.upp | 15 ++ bazaar/DeEncrypter/init | 5 + bazaar/DeEncrypter/main.cpp | 179 ++++++++++++++++++ .../DeEncrypter/src.tpp/DeEncrypter$en-us.tpp | 12 ++ 6 files changed, 259 insertions(+) create mode 100644 bazaar/DeEncrypter/DeEncrypter.h create mode 100644 bazaar/DeEncrypter/DeEncrypter.lay create mode 100644 bazaar/DeEncrypter/DeEncrypter.upp create mode 100644 bazaar/DeEncrypter/init create mode 100644 bazaar/DeEncrypter/main.cpp create mode 100644 bazaar/DeEncrypter/src.tpp/DeEncrypter$en-us.tpp diff --git a/bazaar/DeEncrypter/DeEncrypter.h b/bazaar/DeEncrypter/DeEncrypter.h new file mode 100644 index 000000000..437a3ffc8 --- /dev/null +++ b/bazaar/DeEncrypter/DeEncrypter.h @@ -0,0 +1,38 @@ +#ifndef _DeEncrypter_DeEncrypter_h +#define _DeEncrypter_DeEncrypter_h + +#include + +using namespace Upp; + +#define LAYOUTFILE +#include + +#include + +#define DEFKEYSIZE 32 +#define DEFFILENAME "aes" ASSTRING(DEFKEYSIZE) ".key" +#define BUFFSIZE (512*1024) + +class DeEncrypter : public WithDeEncrypterLayout { +public: + typedef DeEncrypter CLASSNAME; + DeEncrypter(); + + void ToInfo(const String & s) + { + info.Insert(info.GetLength(), s + "\n"); + info.SetCursor(info.GetLength()); + } + + void Crypt(bool encrypt = true); + void Generate(); + void OpenKey(); + + FileSel fs; + FileSel dirs; + String key; +}; + +#endif + diff --git a/bazaar/DeEncrypter/DeEncrypter.lay b/bazaar/DeEncrypter/DeEncrypter.lay new file mode 100644 index 000000000..89e8c390f --- /dev/null +++ b/bazaar/DeEncrypter/DeEncrypter.lay @@ -0,0 +1,10 @@ +LAYOUT(DeEncrypterLayout, 404, 276) + ITEM(EditString, keypath, AlignRight(true).SetEditable(false).HSizePosZ(168, 64).TopPosZ(4, 19)) + ITEM(Label, dv___1, SetLabel(t_("KeyFile")).HCenterPosZ(60, -68).TopPosZ(4, 19)) + ITEM(Button, open, SetLabel(t_("Open Key")).RightPosZ(4, 56).TopPosZ(4, 19)) + ITEM(Button, generate, SetLabel(t_("Generate Key")).LeftPosZ(4, 96).TopPosZ(4, 20)) + ITEM(DocEdit, info, HSizePosZ(4, 4).VSizePosZ(56, 4)) + ITEM(Button, decrypt, SetLabel(t_("Decrypt some Files")).RightPosZ(4, 196).TopPosZ(28, 24)) + ITEM(Button, encrypt, SetLabel(t_("Encrypt some Files")).LeftPosZ(4, 196).TopPosZ(28, 24)) +END_LAYOUT + diff --git a/bazaar/DeEncrypter/DeEncrypter.upp b/bazaar/DeEncrypter/DeEncrypter.upp new file mode 100644 index 000000000..c2008ce00 --- /dev/null +++ b/bazaar/DeEncrypter/DeEncrypter.upp @@ -0,0 +1,15 @@ +description "a simple little gui that makes it possible to decrypt or encrypt stuff, using AESStream\377"; + +uses + CtrlLib, + AESStream; + +file + DeEncrypter.h, + main.cpp, + DeEncrypter.lay, + src.tpp; + +mainconfig + "" = "GUI MT"; + diff --git a/bazaar/DeEncrypter/init b/bazaar/DeEncrypter/init new file mode 100644 index 000000000..866c7ab87 --- /dev/null +++ b/bazaar/DeEncrypter/init @@ -0,0 +1,5 @@ +#ifndef _DeEncrypter_icpp_init_stub +#define _DeEncrypter_icpp_init_stub +#include "CtrlLib/init" +#include "AESStream/init" +#endif diff --git a/bazaar/DeEncrypter/main.cpp b/bazaar/DeEncrypter/main.cpp new file mode 100644 index 000000000..9322cab0b --- /dev/null +++ b/bazaar/DeEncrypter/main.cpp @@ -0,0 +1,179 @@ +#include "DeEncrypter.h" + + + +DeEncrypter::DeEncrypter() +{ + CtrlLayout(*this, "Window title"); + + String activedir = GetFileFolder(GetExeFilePath()); + + fs.ActiveDir(activedir); + dirs.ActiveDir(activedir); + + encrypt << THISBACK1(Crypt, true); + decrypt << THISBACK1(Crypt, false); + generate << THISBACK(Generate); + open << THISBACK(OpenKey); + + //try preload an existing key + String filename = activedir + DIR_SEP + DEFFILENAME; + key = LoadFile(filename); + if(key.IsEmpty()) + { + ToInfo("Choose or Generate key"); + return; + } + + if(key.GetLength() > DEFKEYSIZE) + { + ToInfo("Could not open key " + filename); + key.Clear(); + keypath.Clear(); + } + else + { + ToInfo("Key opened from file " + filename); + keypath.SetData(filename); + } +} + +void DeEncrypter::Crypt(bool encrypt) +{ + if(key.IsEmpty()) + { + ToInfo("No Key selected"); + return; + } + + fs.Multi(); + fs.ClearTypes(); + + if(!fs.ExecuteOpen(String().Cat() << "Select files to " << ((encrypt)?("ENcrypt"):("DEcrypt")) )) + { + ToInfo("Canceled"); + return; + } + + if(!dirs.ExecuteSelectDir(String().Cat() << "Select directory where to put " << ((encrypt)?("ENcrypted"):("DEcrypted")) << " files (same file name used)")) + { + ToInfo("Canceled"); + return; + } + String dir = dirs.Get(); + + Progress pi; + pi.Create(); + + for(int i = 0; i < fs.GetCount(); i++) + { + String filename = fs.GetFile(i); + ToInfo("\n\nProcessing: " + filename); + + String sIn; + + sIn = LoadFile(filename); + if(sIn.IsEmpty()) + { + ToInfo("!!! ERR: Could not load: " + filename); + continue; + } + else + ToInfo("File loaded: " + filename); + + String sOut; + if(0) + sOut = sIn; + else + { + int total = (sIn.GetLength()/BUFFSIZE) + 1; + pi.SetTotal(total); + + if(encrypt) + { + pi.SetText("Encrypting: " + filename); + AESEncoderStream aesEncoder(sIn.GetLength(), key); + for(int i = 0; i < sIn.GetLength();) + { + aesEncoder << sIn.Mid(i, BUFFSIZE); + sOut << aesEncoder; + i+=BUFFSIZE; + pi.Step(); + } + } + else + { + pi.SetText("Decrypting: " + filename); + AESDecoderStream aesDecoder(key); + for(int i = 0; i < sIn.GetLength();) + { + aesDecoder << sIn.Mid(i, BUFFSIZE); + sOut << aesDecoder; + i+=BUFFSIZE; + pi.Step(); + } + } + } + + filename = dir + DIR_SEP + GetFileName(filename); + if(!SaveFile(filename, sOut)) + ToInfo("!!! ERR: Could not save: " + filename); + else + ToInfo(">> saved to " + filename); + } + + pi.Close(); +} + +void DeEncrypter::Generate() +{ + fs.Multi(false); + fs.Types("All Files\t*\nKey Files\t*.key\nDat Files\t*.dat\n"); + fs.Set(DEFFILENAME); + if(!fs.ExecuteSaveAs("Save new key to")) + return; + String filename = fs.Get(); + + key = AESRandomString(DEFKEYSIZE); + + if(!SaveFile(filename, key)) + { + ToInfo("Could not save file " + filename); + key.Clear(); + keypath.Clear(); + return; + } + + ToInfo("New key in file " + filename); + keypath.SetData(filename); +} + +void DeEncrypter::OpenKey() +{ + fs.Multi(false); + fs.Types("All Files\t*\nKey Files\t*.key\nDat Files\t*.dat\n"); + fs.Set("aes32"); + if(!fs.ExecuteOpen("Open key from")) + return; + String filename = fs.Get(); + + key = LoadFile(filename); + + if(key.IsEmpty() || key.GetLength() > DEFKEYSIZE) + { + ToInfo("Could not open key " + filename); + key.Clear(); + keypath.Clear(); + return; + } + + ToInfo("Key opened from file " + filename); + keypath.SetData(filename); +} + + +GUI_APP_MAIN +{ + DeEncrypter().Run(); +} + diff --git a/bazaar/DeEncrypter/src.tpp/DeEncrypter$en-us.tpp b/bazaar/DeEncrypter/src.tpp/DeEncrypter$en-us.tpp new file mode 100644 index 000000000..605e041a9 --- /dev/null +++ b/bazaar/DeEncrypter/src.tpp/DeEncrypter$en-us.tpp @@ -0,0 +1,12 @@ +topic "This is a small tool to encrypt and decrypt files and generate random aes keys."; +[ $$0,0#00000000000000000000000000000000:Default] +[i448;a25;kKO9; $$1,0#37138531426314131252341829483380:structitem] +[l288;2 $$2,0#27521748481378242620020725143825:desc] +[0 $$3,0#96390100711032703541132217272105:end] +[{_}%EN-US +[s0; This is a small tool to encrypt and decrypt files and generate +random aes keys.&] +[s0; it uses default key size 32 byte (256 bit).&] +[s0; &] +[s0; known issue: files > 200 MB produce an `'Out of Memory`'...maybe +because of the heap allocator in Upp.] \ No newline at end of file