Simple (en)decryption routine needed

I need a simple routine that encrypts a string passed as input parameter into encrypted but readable output string and vice versa. Routine need not to be robust (I don't require an extra-protection) but it must produce READABLE output string.
LVL 2
ivobauerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PeterLarsenCommented:
An easy way is to run each character through a XOR in a loop.
0
ivobauerAuthor Commented:
Yes but it is not guaranteed that output string will contain ONLY readable (printable) characters.
0
PeterLarsenCommented:
Sorry,
Maybe the solution could be to add a character (a code) to each of yours characters. This code could tell you something about which byte you used to the following charactor - used with XOR.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

florisbCommented:
hmmm, no mathematics here if you want the output string to be readable (why!).

this might sound stupid, but you could:
just change every permitted character in another one: a -> k, k -> c, et cetera en vise versa.

Readable, and without any logic!
0
cademanCommented:
Look at Marcus Cantu in Mastering Delphi4...He had an example of a program that would encrypt the information...if you don't have the book, you can go to his homepage and download the example programs...Soemwhere towards the end of the book I believe...Im at work, and the books at home, otherwise I would post the information here....
Cade
0
bogieman_Commented:
I have written an en/decryption function that does what you want, but the string is quite long because it creates hexedecimal strings, e.g hello = 32F2 432B 56FC 34AC A4SD

The function asks for a password integer to encrypt it.  I can't get the code now, but if you don't mind the string being too long, then I will get it.  I also have a binary one that makes strings like 1101010010101001 1010010101 10101 ... which is even longer...
0
bogieman_Commented:
..actually, I have some other good ones that are printable, but again I can't get them right now - but please, I need the points, so hold on!
0
bogieman_Commented:
In the first comment, when I say it asks for a password, I mean there is a parameter that is the password - p.s sorry for clogging up this question.
0
PeterLarsenCommented:
DOH :-)
Great idea - converting each character to two hexdecimals. First you can run every characters through XOR or anything else and then convert to hex.

That should do it...
0
gwillsonCommented:
Here are two very simple routines to encrypt and decrypt a string.  It converts each character to its ASCII value, then subtracting a value defined with the "iDisplacement" integer constant.  The new ASCII value is then converted back to its corresponding character.  You can play around with the number you pass it.  For a string with numbers in it "iDisplacement" should not exceed 47 otherwise it will be converted to a non-printable character.

Give this a try and see if it does what your looking for.

function EncryptStr(str: string; const iDisplacement: integer) : string;
var i: integer;
begin
  result := '';
  for i := 1 to length(str) do
    result := result + char(ord(str[i]) - iDisplacement);
end;

function DecryptStr(str: string; const iDisplacement: integer) : string;
{
  ***REQUIREMENT:
  The "Decrypt" function must be passed the same integer value for "iDisplacement" used for the "Encrypt" function.
}
var i: integer;
begin
  result := '';
  for i := 1 to length(str) do
    result := result + char(ord(str[i]) + iDisplacement);
end;
0
aubsCommented:
Hi ivobauer

This is a simple routine that I use for quick and dirty password operations.

Its similar to gWillson's except that a random number (up to 26) is used to offset each character. This way the string returned will always be readable, but much harder to decrypt because the offset is different every time.

You may want to alter the seed value, but there is no real advantage to this. MAKE SURE THE SEED IS THE SAME FOR ENCRYPTION AND DECRYTION!

function EncryptPassword(password: string): string;
var
  i: integer;
begin
  RandSeed:= 9999;
  for i:= 1 to length(password) do
    password[i]:= chr(ord(password[i]) + random(26));
  Result:= Password;
end;

function DecryptPassword(s: string): string;
var
  i: integer;
begin
  s:='';
  RandSeed:= 9999;
  for i:= 1 to length(s) do
    s[i]:= chr(ord(s[i]) - random(26));
  Result:= s;
end;

Regards
Aubs
0
ivobauerAuthor Commented:
bogieman, i am staying tuned. Please post your code snippets as soon as possible. Thanks.
0
aubsCommented:
What about this function.

function EnDeCrypt(s:string): String;
var
      i: integer;
begin
  for i:= 1 to length(s) do
  begin
    if s[i]< '[' then //must be uppercase
          s[i]:= chr(155 - ord(s[i]))
    else
          s[i]:= chr(219 - ord(s[i]));
  end;
  Result:= s;
end;

It can be used to encrypt and decrypt by simply swapping characters within the alphabet.
a to z,
b to y,
c to x,...
l to o,
m to n,
n to m,
o to l, etc. similarly for uppercase A to Z etc


155 = ASCII 'A' + ASCII 'Z'
219 = ASCII 'a' + ASCII 'z'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ivobauerAuthor Commented:
aub, I have tried out your routine (with some minor modifications) and I am satisfied so here are your points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.