Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Security Encryption

Title: Security Encryption Question: How can I do encryption on a string? Answer: I wrote this component because I was tired to "Copy & Paste" the same portion of code. This component are very simple: - Assign a "BASE KEY" for encryption in the KEY property - Enter the string to encrypt in the INSTRING property - The result should appear in the OUTSTRING property - You can look at the ACTION property to ENCRYPT / DECRYPT a string unit RVEncrypt; interface uses SysUtils, Classes; type TRVEncryptAction = (rveEncrypt,rveDecrypt); TRVEncrypt = class(TComponent) private FAction : TRVEncryptAction; FKey : String; FInString : String; FOutString: String; procedure FEncrypt; procedure FDecrypt; procedure SetAction(Value: TRVEncryptAction); procedure SetKey(Value: String); procedure SetInString(Value: String); protected { Protected declarations } public { Public declarations } published property Action: TRVEncryptAction read FAction write SetAction; property Key: String read FKey write SetKey; property InString: String read FInString write SetInString; property OutString: String read FOutString; end; procedure Register; implementation procedure Register; begin RegisterComponents('MyPalette', [TRVEncrypt]); end; { TRVEncrypt } procedure TRVEncrypt.FDecrypt; var Len : Byte; PCounter: Byte; SCounter: Byte; begin FOutString := ''; Len := Length(FKey) div 2; SCounter := 1; PCounter := 1; while SCounter begin FOutString := FOutString + Chr(Ord(FInString[SCounter]) - Ord(FKey[PCounter]) - Len); Inc(SCounter); Inc(PCounter); if PCounter Length(FKey) then PCounter := 1; end; end; procedure TRVEncrypt.FEncrypt; var Len : Byte; PCounter: Byte; SCounter: Byte; begin FOutString := ''; Len := Length(FKey) div 2; SCounter := 1; PCounter := 1; while SCounter begin FOutString := FOutString + Chr(Ord(FKey[PCounter]) + Ord(FInString[SCounter]) + Len); Inc(SCounter); Inc(PCounter); if PCounter Length(FKey) then PCounter := 1; end; end; procedure TRVEncrypt.SetAction(Value: TRVEncryptAction); begin if FKey = '' then exit; if FAction Value then begin FAction := Value; if FAction = rveEncrypt then FEncrypt else FDecrypt; end; end; procedure TRVEncrypt.SetInString(Value: String); begin if FKey = '' then exit; if FInString Value then begin FInString := Value; if FAction = rveEncrypt then FEncrypt else FDecrypt; end; end; procedure TRVEncrypt.SetKey(Value: String); begin if FKey Value then begin FKey := Value; if FAction = rveEncrypt then FEncrypt else FDecrypt; end; end; end.