Mega Code Archive

 
Categories / Delphi / String
 

String encryption (mild)

Title: String encryption (mild) Question: I wanted to encrypt a string in such a way that the result left me with a string of printable characters. Answer: Okay, dont bother telling me that this encrytion method is weak, its supposed to be. I created this because I needed a method to keep text in a plain printable format and yet keep it away from prying eyes, any kid with 10 minutes spare could probably crack this....I dont really know or care, it works for me. So no flames please. This system uses a key that can effectively be of any length. What this function does is to XOR the lower four bits of a character with the lower four bits of a character from the key, this ensures that the character remains a 7 bit character and is still printable. {NOTE :- Key may be an empty string to use a default key} function DenCrypt(Str : string; Key : string): string; var X, Y : Integer; A : Byte; begin if Key = '' then Key := 'd1duOsy3n6qrPr2eF9u'; Y := 1; for X := 1 to length(Str) do begin A := (ord(Str[X]) and $0f) xor (ord(Key[Y]) and $0f); Str[X] := char((ord(Str[X]) and $f0) + A); inc(Y); if Y length(Key) then Y := 1; end; Result := Str; end; Being as this routine uses simple XOR you can run the same string back through the routine with the same key to decode it. Now there may or may not be a better or faster way to do this, I am not demonstrating my talents or lack of talent as a programmer, its just an idea I came up with for a project I was doing and I thought others may find it useful. Hopefully in this form its easy for a beginner to understand how it works.