From 46c9781d6b95ecf72cb819949204415dec3de737 Mon Sep 17 00:00:00 2001 From: "eduardo.davila@creatis.insa-lyon.fr" Date: Tue, 4 Jun 2024 19:04:59 +0200 Subject: [PATCH] #3519 box std Encrypting --- packages/std/bbs/appli/exampleEncrypting.bbg | 101 +++++++ packages/std/bbs/appli/exampleEncrypting.bbs | 62 ++++ packages/std/src/bbstdEncrypting.cxx | 280 +++++++++++++++++++ packages/std/src/bbstdEncrypting.h | 50 ++++ 4 files changed, 493 insertions(+) create mode 100644 packages/std/bbs/appli/exampleEncrypting.bbg create mode 100644 packages/std/bbs/appli/exampleEncrypting.bbs create mode 100644 packages/std/src/bbstdEncrypting.cxx create mode 100644 packages/std/src/bbstdEncrypting.h diff --git a/packages/std/bbs/appli/exampleEncrypting.bbg b/packages/std/bbs/appli/exampleEncrypting.bbg new file mode 100644 index 0000000..fcddfba --- /dev/null +++ b/packages/std/bbs/appli/exampleEncrypting.bbg @@ -0,0 +1,101 @@ +# ---------------------------------- +# - BBTKGEditor v 1.6 BBG BlackBox Diagram file +# - /Users/davila/Creatis/C23/creatools_source/bbtk/packages/std/bbs/appli/exampleEncrypting.bbg +# ---------------------------------- + +APP_START +CATEGORY: +DESCRIPTION:Description ?? +AUTHOR:Author ?? +EXPORTFORMAT:0 +COMPLEXBOX:FALSE +COMPLEXINPUTS:0 +BOXES:8 +BOX +std:Encrypting:Box00 +ISEXEC:FALSE +0.698659:41.492586:-900.000000 +22.348659:38.992586:-900.000000 +PORT +Mode:"1" +FIN_BOX +BOX +wx:OutputText:Box01 +ISEXEC:FALSE +3.959068:15.642200:-900.000000 +25.629068:13.142200:-900.000000 +FIN_BOX +BOX +wx:LayoutLine:Box02 +ISEXEC:TRUE +-19.562454:1.824276:-900.000000 +13.757546:-0.675724:-900.000000 +FIN_BOX +BOX +wx:OutputText:Box03 +ISEXEC:FALSE +-19.380378:15.587155:-900.000000 +2.289622:13.087155:-900.000000 +FIN_BOX +BOX +std:Encrypting:Box04 +ISEXEC:FALSE +4.839803:32.044457:-900.000000 +26.489803:29.544457:-900.000000 +PORT +Mode:"2" +FIN_BOX +BOX +std:ConcatStrings:Box05 +ISEXEC:FALSE +-28.179249:56.863085:-900.000000 +-5.419249:54.363085:-900.000000 +PORT +In1:"hola eduardo 1972 COLOMBIA kz" +FIN_BOX +BOX +wx:OutputText:Box06 +ISEXEC:FALSE +-44.660543:15.687367:-900.000000 +-22.990543:13.187367:-900.000000 +FIN_BOX +BOX +std:ConcatStrings:Box07 +ISEXEC:FALSE +18.087507:57.639373:-900.000000 +40.847507:55.139373:-900.000000 +PORT +In1:"3" +FIN_BOX +CONNECTIONS:10 +CONNECTION +Box05:Out:Box00:In +NumberOfControlPoints:0 +CONNECTION +Box00:Out:Box03:In +NumberOfControlPoints:0 +CONNECTION +Box00:Out:Box04:In +NumberOfControlPoints:0 +CONNECTION +Box04:Out:Box01:In +NumberOfControlPoints:0 +CONNECTION +Box01:Widget:Box02:Widget3 +NumberOfControlPoints:0 +CONNECTION +Box03:Widget:Box02:Widget2 +NumberOfControlPoints:0 +CONNECTION +Box05:Out:Box06:In +NumberOfControlPoints:0 +CONNECTION +Box06:Widget:Box02:Widget1 +NumberOfControlPoints:0 +CONNECTION +Box07:Out:Box00:Type +NumberOfControlPoints:0 +CONNECTION +Box07:Out:Box04:Type +NumberOfControlPoints:0 +APP_END diff --git a/packages/std/bbs/appli/exampleEncrypting.bbs b/packages/std/bbs/appli/exampleEncrypting.bbs new file mode 100644 index 0000000..ca44672 --- /dev/null +++ b/packages/std/bbs/appli/exampleEncrypting.bbs @@ -0,0 +1,62 @@ +# ---------------------------------- +# - BBTKGEditor v 1.6 BBS BlackBox Script +# - /Users/davila/Creatis/C23/creatools_source/bbtk/packages/std/bbs/appli/exampleEncrypting.bbs +# ---------------------------------- + +# BBTK GEditor Script +# ---------------------- + +include std +include itkvtk +include std +include wx + +author "Author ??" +description "Description ??" +category "" + +new std:Encrypting Box00 + set Box00.Mode "1" + +new wx:OutputText Box01 + +new wx:LayoutLine Box02 + +new wx:OutputText Box03 + +new std:Encrypting Box04 + set Box04.Mode "2" + +new std:ConcatStrings Box05 + set Box05.In1 "hola eduardo 1972 COLOMBIA kz" + +new wx:OutputText Box06 + +new std:ConcatStrings Box07 + set Box07.In1 "3" + + +connect Box05.Out Box00.In + +connect Box00.Out Box03.In + +connect Box00.Out Box04.In + +connect Box04.Out Box01.In + +connect Box01.Widget Box02.Widget3 + +connect Box03.Widget Box02.Widget2 + +connect Box05.Out Box06.In + +connect Box06.Widget Box02.Widget1 + +connect Box07.Out Box00.Type + +connect Box07.Out Box04.Type + + + +# Complex input ports +exec Box02 diff --git a/packages/std/src/bbstdEncrypting.cxx b/packages/std/src/bbstdEncrypting.cxx new file mode 100644 index 0000000..f20d64d --- /dev/null +++ b/packages/std/src/bbstdEncrypting.cxx @@ -0,0 +1,280 @@ +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +#include "bbstdEncrypting.h" +#include "bbstdPackage.h" + + +#include +#include + + +namespace bbstd +{ + +// +// +// https://stackoverflow.com/questions/66380465/c-encoding-decoding +// +// + +class encryptor +{ +public: + virtual std::string encode(std::string original) = 0; + virtual std::string decode(std::string secret) = 0; +}; + + + +class shift_encryptor : public encryptor +{ + int shift; + +public: + shift_encryptor(int shift) + { + // Your code starts here + this->shift = shift; + // Your code ends here + } + + std::string encode(std::string original) + { + // Your code starts here + + char c_original[74] = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k', 'l', 'm', 'n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.',',',':',';','@','*','$','/','-','_','A','B','C','D','E','J','G','H','I','J', 'K','L','M','N','O','P','Q', 'R','S','T','U', 'V','W','X', 'Y','Z' }; + + + std::string original_result = ""; + for (int i = 0; i < original.length(); i++) + { + int c_index = 0; + for (int j = 0; j < sizeof(c_original); j++) + { + if (original[i] == c_original[j]) + { + c_index = j; + break; + } + } + int final_index = c_index + this->shift; + if (final_index > sizeof(c_original) - 1) + final_index -= sizeof(c_original); + original_result += c_original[final_index]; + } + return original_result; + // Your code ends here + } + + std::string decode(std::string secret) + { + // Your code starts here + char c_secret[74] = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k', 'l', 'm', 'n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.',',',':',';','@','*','$','/','-','_','A','B','C','D','E','J','G','H','I','J', 'K','L','M','N','O','P','Q', 'R','S','T','U', 'V','W','X', 'Y','Z' }; + std::string secret_result = ""; + for (int i = 0; i < secret.length(); i++) + { + int c_index = 0; + for (int j = 0; j < sizeof(c_secret); j++) + { + if (secret[i] == c_secret[j]) + { + c_index = j; + break; + } + } + int final_index = c_index - this->shift; + if (final_index < 0) + final_index += sizeof(c_secret); + secret_result += c_secret[final_index]; + } + return secret_result; + // Your code ends here + } +}; + +class cypher_encryptor : public encryptor +{ + std::string cypher; +public: + cypher_encryptor(std::string cypher) + { + // Your code starts here + this->cypher = cypher; + // Your code ends here + } + std::string encode(std::string original) + { + // Your code starts here + char c_original[74] = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k', 'l', 'm', 'n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.',',',':',';','@','*','$','/','-','_','A','B','C','D','E','J','G','H','I','J', 'K','L','M','N','O','P','Q', 'R','S','T','U', 'V','W','X', 'Y','Z' }; + std::string original_result = ""; + for (int i = 0; i < original.length(); i++) + { + int c_index = 0; + for (int j = 0; j < this->cypher.length(); j++) + { + if (original[i] == this->cypher[j]) + { + c_index = j; + break; + } + } + original_result += c_original[c_index]; + } + return original_result; + // Your code ends here + } + + std::string decode(std::string secret) + { + // Your code starts here + char c_secret[74] = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k', 'l', 'm', 'n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.',',',':',';','@','*','$','/','-','_','A','B','C','D','E','J','G','H','I','J', 'K','L','M','N','O','P','Q', 'R','S','T','U', 'V','W','X', 'Y','Z' }; + std::string secret_result = ""; + for (int i = 0; i < secret.length(); i++) + { + int c_index = 0; + for (int j = 0; j < sizeof(c_secret); j++) + { + if (secret[i] == c_secret[j]) + { + c_index = j; + break; + } + } + secret_result += this->cypher[c_index]; + } + return secret_result; + // Your code ends here + } +}; + + +std::string encryptDecrypt(std::string toEncrypt) { + char key[5] = {'K', 'C', 'Q','e','r'}; //Any chars will work + std::string output = toEncrypt; + + for (int i = 0; i < toEncrypt.size(); i++) + output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))]; + + return output; +} + +BBTK_ADD_BLACK_BOX_TO_PACKAGE(std,Encrypting) +BBTK_BLACK_BOX_IMPLEMENTATION(Encrypting,bbtk::AtomicBlackBox); + + + +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void Encrypting::Process() +{ + +// THE MAIN PROCESSING METHOD BODY +// Here we simply set the input 'In' value to the output 'Out' +// And print out the output value +// INPUT/OUTPUT ACCESSORS ARE OF THE FORM : +// void bbSet{Input|Output}NAME(const TYPE&) +// const TYPE& bbGet{Input|Output}NAME() const +// Where : +// * NAME is the name of the input/output +// (the one provided in the attribute 'name' of the tag 'input') +// * TYPE is the C++ type of the input/output +// (the one provided in the attribute 'type' of the tag 'input') + + bbSetOutputOut( bbGetInputIn() ); + + if (bbGetInputType()==1) + { + shift_encryptor* shiftTest = new shift_encryptor(9); + if (bbGetInputMode()==1) + { + bbSetOutputOut ( shiftTest->encode( bbGetInputIn() ) ); + } + if (bbGetInputMode()==2) + { + bbSetOutputOut ( shiftTest->decode( bbGetInputIn() ) ); + } + } + if (bbGetInputType()==2) + { + cypher_encryptor* crypter = new cypher_encryptor("04dw51CeVD23 67W89XYZASTBEfyhazlvjnktbogrumcpiqxs.,:;@*$/-_OHIPKLQRUJGJMNF"); + if (bbGetInputMode()==1) + { + bbSetOutputOut ( crypter->encode( bbGetInputIn() ) ); + } + if (bbGetInputMode()==2) + { + bbSetOutputOut ( crypter->decode( bbGetInputIn() ) ); + } + } + + if (bbGetInputType()==3) + { + if ((bbGetInputMode()==1) ||(bbGetInputMode()==2) ) + { + bbSetOutputOut ( encryptDecrypt( bbGetInputIn() ) ); + } + } + + + /* + encodeResult = shiftTest->encode("hello world"); + std::cout << "shift encode result == " << encodeResult << std::endl; + + decodeResult = shiftTest->decode("lipps${svph"); + std::cout << "shift encode result == " << decodeResult << std::endl; + + // string encodeResult = shiftTest->en + + cypher_encryptor* crypterTest = new cypher_encryptor("efyhadwzlvjnktbogrumcpiqxs"); + +# encodeResult = crypterTest->encode("heyj fvmpe"); + std::cout << "encryptor encode result == " << encodeResult << std::endl; + decodeResult = crypterTest->decode("me emt shevyp wtjp ihls ftssjkt"); + std::cout << "encryptor decode result == " << decodeResult << std::endl; + */ + +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void Encrypting::bbUserSetDefaultValues() +{ + +// SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX +// Here we initialize the input 'In' to 0 + bbSetInputIn(""); + bbSetInputMode(0); + bbSetInputType(0); + +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void Encrypting::bbUserInitializeProcessing() +{ + +// THE INITIALIZATION METHOD BODY : +// Here does nothing +// but this is where you should allocate the internal/output pointers +// if any + + +} +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +void Encrypting::bbUserFinalizeProcessing() +{ + +// THE FINALIZATION METHOD BODY : +// Here does nothing +// but this is where you should desallocate the internal/output pointers +// if any + +} +} +// EO namespace bbstd + + diff --git a/packages/std/src/bbstdEncrypting.h b/packages/std/src/bbstdEncrypting.h new file mode 100644 index 0000000..85922e1 --- /dev/null +++ b/packages/std/src/bbstdEncrypting.h @@ -0,0 +1,50 @@ +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +#ifndef __bbstdEncrypting_h_INCLUDED__ +#define __bbstdEncrypting_h_INCLUDED__ + +#include "bbstd_EXPORT.h" +#include "bbtkAtomicBlackBox.h" +#include "iostream" + +namespace bbstd +{ + +class bbstd_EXPORT Encrypting + : + public bbtk::AtomicBlackBox +{ + BBTK_BLACK_BOX_INTERFACE(Encrypting,bbtk::AtomicBlackBox); +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== + BBTK_DECLARE_INPUT(In,std::string); + BBTK_DECLARE_INPUT(Mode,int); + BBTK_DECLARE_INPUT(Type,int); + BBTK_DECLARE_OUTPUT(Out,std::string); + BBTK_PROCESS(Process); + void Process(); +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== +}; + +BBTK_BEGIN_DESCRIBE_BLACK_BOX(Encrypting,bbtk::AtomicBlackBox); + BBTK_NAME("Encrypting"); + BBTK_AUTHOR("InfoDev"); + BBTK_DESCRIPTION("No Description."); + BBTK_CATEGORY("empty"); + BBTK_INPUT(Encrypting,In,"Input string",std::string,""); + BBTK_INPUT(Encrypting,Mode,"(default 0) 0:Nothing Type1: 0_Nothing 1_Encode 2_Decode Type2: 0_Nothing 1_Encode 2_Decode Type3: 0_Nothing 1:EncodeDecode",int,""); + BBTK_INPUT(Encrypting,Type,"(default 0) 0:Nothing 1:shiftTest(LowerCase) 2:cypher_encryptor(LowerCase) 3:EncodeDecode",int,""); + BBTK_OUTPUT(Encrypting,Out,"Output string",std::string,""); +BBTK_END_DESCRIBE_BLACK_BOX(Encrypting); +//===== +// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost) +//===== + +} // EO namespace bbstd + +#endif // __bbstdEncrypting_h_INCLUDED__ + -- 2.49.0