You are here: Home » C++ » [ En ] » Encrypting your data

Encrypting your data

7 December |

Sometimes it is necessary to hide the user sensitive settings like the passwords/ login etc in your application settings file (registry), so you have to make it somehow unreadable applying some encryption (scrambling the text). I’m only going to write two simple and fast ways to achieve that:
1. Binary Shift : the simplest of all, you just increase or decrease a char by a number:
[cpp]
void BinaryShift(char *s,int n)
{
for(int i=0;s[i]!=0;i++)
{
s[i] += n;
}
}[/cpp]

2. XOR : Exclusive OR encryption, this encryption while extremely simple, is nearly unbreakable (it’s weak against pattern attacks) .
[cpp]int xorme(char *string, char *key)
{
int l=0;
for(int x=0; string[x]!=’\0′; x++)
{
string[x]=string[x]^key[l];
l++;
if(l>=strlen(key)) l=0;
}
return 0;
}[/cpp]
xor an entire structure like this:

[cpp]void xorstruct(char *str,char *key,int sz)
{
int l=0;
for(int x=0; str[x]!=’\0′; x++)
{
str[x]=str[x]^key[l];
l++;
if(l>=strlen(key)) l=0;
}
}[/cpp]
Use it like:
[cpp]
xorstruct((char*)&MyRecStruct,”thepassword12″,sizeof(RecStruct));
[/cpp]

There are strong algorithms like AES (Rijndael) that has been designed with the state of art in the cryptographic research an it will definitely keep away attackers that are not so skilled in reversing it (or not: Block ciphers sensitive to Groebner Basis Attacks. ) .
If you are mybe ‘paranoia’ about the sensitive data or your project requires strong encryption you may try the open source library Crypto++ and use different algorithms.
Other ways to encrypt your data :
Fast Simple Secure Encryption Routine
File Encryption ASG, SG, RC4
Encryption using the Win32 Crypto API
FileCrypt (Rijndael, Blowfish, TEA, XOR256)
A C++ Implementation of the Blowfish Encryption/Decryption method
Portable, Rinjdael (AES) Based Stream Cipher Class

Good luck in securing your private data.

You like this post? Be the first of my friends to give me a beer! click here. Thanks!
Filed under

C++, [ En ]

| Tags:

Comments are closed.