MCPcopy Create free account
hub / github.com/FIT-DNU/Cryptography-and-Cyber-Security / encipher

Function encipher

Feistel Cipher.cpp:20–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18}
19
20void encipher(const std::string& str, const std::string& key, std::string* out)
21{
22 unsigned int v[2], w[2], k[4], keyBuff[4];
23
24 // Set array values to 0 (windows sets them to default values)
25 memset(v, 0, sizeof(v));
26 memset(w, 0, sizeof(w));
27 memset(k, 0, sizeof(k));
28 memset(keyBuff, 0, sizeof(keyBuff));
29
30 // Check key size, store in key buffer and divide it into 4 subkeys
31 int len = key.length();
32 if (len > 16)
33 len = 16;
34 memcpy(keyBuff, key.c_str(), len);
35 for (int i = 0; i < 4; ++i) // ++i more efficient
36 k[i] = keyBuff[i]; // store each of the 4 parts of key into array k[]
37
38 // Copy input string to a buffer of size multiple of 4
39 int strBuffLen = str.length();
40 if (strBuffLen == 0)
41 return;
42 if ((strBuffLen % 4) > 0)
43 strBuffLen += 4 - (strBuffLen % 4);
44 unsigned char* strBuff = new unsigned char[strBuffLen];
45
46 // Initialize and copy input string into the string buffer
47 memset(strBuff, 0, strBuffLen);
48 memcpy(strBuff, str.c_str(), str.length());
49
50 // Encode
51 out->clear();
52 v[1] = 0;
53 for (int i = 0; i < strBuffLen; i += 4)
54 {
55 v[0] = *(unsigned int*)&strBuff[i];
56 encBlock(&v[0], &w[0], &k[0]); // encode one block with a piece of given string
57 out->append((char*)&w[0], 4); // add encoded part to output string
58 v[1] = w[1];
59 }
60 out->append((char*)&v[1], 4);
61 delete[] strBuff;
62}
63void decBlock(unsigned int* v, unsigned int* w, unsigned int* k)
64{
65 register unsigned int v0 = v[0], v1 = v[1], i, sum = 0xC6EF3720; // initial sum derived from TEA algorithm definition

Callers 1

mainFunction · 0.85

Calls 1

encBlockFunction · 0.85

Tested by

no test coverage detected