Raz-Soft » Blog Archive » Encrypting your data Buy Cheap Software
La lumina neonului din camera… »
« Say hello to my little blog

Encrypting your data

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:

C++:
  1. void BinaryShift(char *s,int n)
  2. {
  3.     for(int i=0;s[i]!=0;i++)
  4.        {
  5.         s[i] += n;
  6.     }
  7. }

2. XOR : Exclusive OR encryption, this encryption while extremely simple, is nearly unbreakable (it's weak against pattern attacks) .

C++:
  1. int xorme(char *string, char *key)
  2. {
  3.     int l=0;
  4.     for(int x=0; string[x]!='\0'; x++)
  5.     {      
  6.         string[x]=string[x]^key[l];
  7.         l++;
  8.         if(l>=strlen(key)) l=0;
  9.     }
  10.     return 0;
  11. }

xor an entire structure like this:

C++:
  1. void xorstruct(char *str,char *key,int sz)
  2. {
  3.     int l=0;
  4.     for(int x=0; str[x]!='\0'; x++)
  5.     {      
  6.         str[x]=str[x]^key[l];
  7.         l++;
  8.         if(l>=strlen(key)) l=0;
  9.     }
  10. }

Use it like:

C++:
  1. xorstruct((char*)&MyRecStruct,"thepassword12",sizeof(RecStruct));

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.

related things:

Author: Raz | On December 7th, 2006 | C/C++, [ En ] | No Comments

Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> and all YM emotions