Hello. I have just finished a string encrpytion function using my own algorithm. I was hoping that if anyone on this forum was into cryptology or knows alot about it, that they could confirm that my function encrypts strings securely.
char * encrypt_string(constchar * s_stringToEncrypt, constchar * s_encryptionKey, bool b_moveLeft)
{
char * s_tempStringToEncrypt = newchar[get_size_of_string(s_stringToEncrypt) + 1];
for(int i = 0; i < get_size_of_string(s_stringToEncrypt); i ++)
{
s_tempStringToEncrypt[i] = s_stringToEncrypt[i];
}
s_tempStringToEncrypt[get_size_of_string(s_stringToEncrypt)] = NULL;
char * s_tempEncryptionKey = newchar[get_size_of_string(s_encryptionKey) + 1];
for(int i = 0; i < get_size_of_string(s_encryptionKey); i ++)
{
s_tempEncryptionKey[i] = s_encryptionKey[i];
}
s_tempEncryptionKey[get_size_of_string(s_encryptionKey)] = NULL;
for(int i = 0, ii = 0; i < get_size_of_string(s_tempStringToEncrypt); i ++, ii = 0)
{
if(ii > get_size_of_string(s_tempEncryptionKey) - 1)
{
ii = 0;
}
if(b_moveLeft == true)
{
s_tempStringToEncrypt[i] -= (s_tempEncryptionKey[ii]) * (i + 1);
}
else
{
s_tempStringToEncrypt[i] += (s_tempEncryptionKey[ii]) * (i + 1);
}
}
return s_tempStringToEncrypt;
}
int main()
{
cout << encrypt_string("Hello there, how are you?", "34jf582j4656367h75", false);
return 0;
}
Output: § Ëáp¯☼ðÜt4╚ë×rG¢╦®i±↨┌¡D
s_stringToEncrypt is the string to encrypt, s_encryptionKey is the encrpytion key and b_moveLeft is which way the characters in the string are moved, so if you encrypted a string, to decrypt it you must use the same string and key but move it the other way.
Well, you're currently using only the first character of the key, making it not very useful.
The encryption is perfectly safe (the * (i + 1) is unnecessary) if the key is at least as long as the plaintext (see "one-time pad").
Aside from that, there are a few technical issues. You have several memory leaks and you're using C strings, even though your encrypted text can contain null characters. You should be using std::string. When you use xor, you can drop the differentiation between encrypting and decrypting: