I am looking for something more sophisticated than ROT13, but which does not require a library (preferablly not even a unit, just a drop in function).
I want to symetrically encrypt/decrypt a given string with a password provided by the user. However, the result has to be a string, in the sense that it I have to be able to store it in an .INI file.
Does anyone have a simple function to do this (delphi XE2)? Google is not my friend today.
Thanks in advance
[Update] / [Bounty] Just to make it clear (aplogies if it was not so originally), I don't want a hash. I have a list box where users can add/modiy/delete entries. I want to store those in an .INI file when the program closes and reload when it starts again. Anyone looking at the .INI file (for instance, opening it in Notepad) should not be able to read those strings.
I suppose that I could just stream the compnent as binary, but for eace of mind I would rather encrypt the strings using a user provided password. For the purpose of this applciation it does not matter if .INI file section names or keyte values are human readable, I just want to encrypt the data, giving me something list this when stored on disk:
[config]
numEntries=3
[listbox]
0=ywevdyuvewfcyuw
1=edw
2=hr4uifareiuf
The base64 is very good encoder and have string result and standard :
You can use with this sample :
encode :
decode:
I use Delphi Encryption Compendium which has wonderful functions for both hash and symmetric encryption/decryption. It is divided into units, but doesn't require any external libraries, and is pretty fast.
Here's how I use it in my code:
You can use any of the
TCipher_*
classes instead of GOST.First off, see this link for the wincrypt unit I'm using since I used it here.
What this does for encryption is take the string that's put into it (you are using INI so it's all single strings anyway, right?), and then runs it through the WinCrypt 3DES based on a password entered in, and then since that produces binary, I run that through Base64. For decryption, I reverse the process. An incorrect password produces garbage on decryption, but for the amount that I tested it, it seems to work right as long as the password is right for both steps. Of course, I may have forgotten to do some cleanup, but if that is the case it can readily be fixed.
Quick usage example:
Hope it helps out someone.
Using "Edit1" as input. Correct output for encryption ANSI: 3+Pp7o8aErc= Correct output for encryption WideString: HijzDYgRr/Y=
Edit: I posted WideString versions as well. I downloaded the XE3 demo to look at and play with. This code works there as well as Turbo Delphi 2006 and Delphi 3, so if you have difficulty check the line(s) that I put comments on about the Windows XP Base64 implementation not honoring CRYPT_STRING_NOCRLF, because if you are on a Windows that does, the line needs to be changed for this to work right. Regardless, for the OP's stated intention we DO NOT want $13$10 to appear in the encoded text
Disclaimer
The encryption algorithm used in this answer is very basic and can be easily broken by any individual with medium to high skills in cryptography. It is used in the solution because the OP is asking for a simple symmetric solution without requiring any library.
Principle
The solution is based on the XOR cipher. From the Wikipedia:
Pieces of the puzzle
My proposed solution is based in this basic routine:
The routine accepts a key and the source data as an array of bytes, and returns the resulting XORed array of bytes. The same routine functions to encrypt and to decrypt information, given the same key is used in both operations. To encrypt, the source is the plain data, and to decrypt, the source is the encrypted data.
I made two auxiliary routines to allow storing the result as a string. One to convert an array of bytes to a textual sequence of hexadecimal numbers, and the other to perform the reverse conversion:
With this foundations, you can build all of what you need. For convenience and test my code, I created some other routines, for example:
this one to store the key inside the exe and get it as a TBytes value
you can provide a key of any length, since it rolls to encrypt the data inside XorCipher routine.
this one to properly encode a given string using that key:
this other to properly decode a encoded string to a string
Writing the INI file
With this routines accessible to the place where you write and read your INI file, you can easily write and read it, for example:
This is not production ready-code, since it's written only to test the solution, but it is also a starting point for you to make it rock-solid.
A word of caution
This is not strong cryptography, so, don't rely on this to store really sensitive information. One weak point is the key is contained inside your exe in plain form. You can work on this, but the main weakens is the algorithm itself.
Take as an example of this issue the following: since you're encoding Unicode Delphi strings in UTF-16 format, the second byte of each character is usually zero (unless you're in the east or a country with a non-latin alphabet), and you will find the exact bytes of the key repeats in your encoded stored strings. You can make this less apparent by not using a plain hexadecimal representation of the encoded data (for example encoding it using base64 as already suggested here).
You can resort to AnsiStrings to avoid revealing this parts of your key, or you can code your key with explicit zero bytes (or other constant byte) in the even positions.
Anything of this will work if the users of your software are not cryptographically educated, but the fact is that anyone with a medium level of knowledge and good skills can get the key by analyzing your data. If the user knows a un-encoded value, it gets easier.
This is a replacement for Tinifile.
ReadString and WriteString are overridden, these are internal used to for Read/WriteFloat, Read/WriteInteger etc.
Strings are encrypted and stored as HEX-Strings.
Demo usage:
You may use internal encryption method by UseInternalVersion, or provide own procedures with
Procedure SetCryptingData(aEncryptProc, aDecryptProc: CryptingProc; aKey: Word);