1 #include "CypherTest.h" 2 3 #include <Cypher/Cypher.h> 4 CypherTest()5CypherTest::CypherTest() 6 { 7 CtrlLayout(*this, "Cypher test"); 8 9 goBtn <<= THISBACK(onGo); 10 cancelBtn.Cancel() <<= Breaker(0); 11 cypherSwitch <<= THISBACK(onCypherSwitch); 12 onCypherSwitch(); 13 14 keyEdit <<= "AABBCCDDEEFF00112233445566778899"; 15 16 splitSwitch = 0; 17 cypherSwitch = 0; 18 streamSwitch = 0; 19 } 20 onCypherSwitch(void)21void CypherTest::onCypherSwitch(void) 22 { 23 if(cypherSwitch == 0) 24 keyLabel.SetText("Key (16 or 32 bytes long, hex-ascii format - 0011223344) :"); 25 else 26 keyLabel.SetText("Key, usually from 5 to 32 bytes long, hex-ascii format - 0011223344) :"); 27 } 28 onGo()29void CypherTest::onGo() 30 { 31 String hexAsciiKey = ToUpper(String(~keyEdit)); 32 if(hexAsciiKey.GetCount() != 32 && hexAsciiKey.GetCount() != 64 && cypherSwitch == 0) 33 { 34 PromptOK("Key MUST be 16 or 32 bytes long for snow2 cypher"); 35 return; 36 } 37 38 // convert key in hex bytes 39 String key; 40 for(int i = 0; i < hexAsciiKey.GetCount()-1;) 41 { 42 byte b; 43 char c = hexAsciiKey[i++]; 44 if(c >= '0' && c <= '9') 45 b = c - '0'; 46 else if(c >= 'A' && c <= 'F') 47 b = c - 'A' + 10; 48 else 49 { 50 PromptOK(String("Invalid hex digit '") + c + "' in key"); 51 return; 52 } 53 b <<= 4; 54 c = hexAsciiKey[i++]; 55 if(c >= '0' && c <= '9') 56 b += c - '0'; 57 else if(c >= 'A' && c <= 'F') 58 b += c - 'A' + 10; 59 else 60 { 61 PromptOK(String("Invalid hex digit '") + c + "' in key"); 62 return; 63 } 64 key.Cat(b); 65 } 66 67 // check source and dest file names 68 if(~sourceEdit == "" || ~destEdit == "") 69 return; 70 if(!FileExists(~sourceEdit)) 71 { 72 PromptOK(String().Cat() << "File '" << ~sourceEdit << "' doesn't exists"); 73 return; 74 } 75 if(FileExists(~destEdit)) 76 if(!PromptYesNo(String().Cat() << "File '" << ~destEdit << "' exists&[*= OVERWRITE ?]")) 77 return; 78 79 // load file to encode 80 String s = LoadFile(~sourceEdit); 81 82 // encode it choosing requested encoder 83 One<Cypher> cypher; 84 switch(cypherSwitch) 85 { 86 // snow2 Cypher 87 case 0: 88 cypher = new Snow2; 89 break; 90 91 // RC4 Cypher 92 case 1: 93 cypher = new RC4; 94 break; 95 default: 96 PromptOK("Unsupported Cypher"); 97 return; 98 } 99 String d; 100 101 // sets Cypher's key 102 cypher->SetKey(key, "12345"); 103 104 if(streamSwitch == 0) 105 { 106 // buffer mode 107 if(splitSwitch == 0) 108 { 109 d = (*cypher)(s); 110 } 111 else 112 { 113 int len = s.GetCount(); 114 int randMax = len / 10; 115 116 while(len) 117 { 118 int rnd = min((int)(Random(randMax) + 1), len); 119 String ss = s.Left(rnd); 120 s = s.Right(len - rnd); 121 len = s.GetCount(); 122 d += (*cypher)(ss); 123 } 124 } 125 } 126 else 127 { 128 // stream mode 129 if(splitSwitch == 0) 130 { 131 (*cypher) << s; 132 (*cypher) >> d; 133 } 134 else 135 { 136 int len = s.GetCount(); 137 int randMax = len / 10; 138 139 while(len) 140 { 141 int rnd = min((int)(Random(randMax) + 1), len); 142 String ss = s.Left(rnd); 143 s = s.Right(len - rnd); 144 len = s.GetCount(); 145 (*cypher) << ss; 146 } 147 (*cypher) >> d; 148 } 149 } 150 151 // save encoded file 152 if(!SaveFile(~destEdit, d)) 153 { 154 PromptOK("Error writing dest file!"); 155 return; 156 } 157 PromptOK("Encoding ok!"); 158 159 } 160 161 GUI_APP_MAIN 162 { 163 CypherTest().Run(); 164 } 165 166