Ver Mensaje Individual
  #1  
Antiguo 08-06-2006
Avatar de seoane
[seoane] seoane is offline
Miembro Premium
 
Registrado: feb 2004
Ubicación: A Coruña, España
Posts: 3.717
Reputación: 24
seoane Va por buen camino
Cifrado de texto MUY simple

En este truco vamos a utilizar el echo de que si se incializa el valor RandSeed con un mismo valor la funcion Random genera la misma secuencia de numeros "aleatorios". A parte de usar la propiedad: (a XOR b) XOR b = a

Sin mas preambulos:
Código Delphi [-]
function Cifrar(Texto: string; Clave: Longint): string;
var
  i: integer;
begin
  Result:= '';
  RandSeed:= Clave;
  for i:= 1 to Length(Texto) do
    Result:= Result + IntToHex(Byte(Texto[ i ]) xor Byte(Random(256)),2);
end;

function Descifrar(Texto: string; Clave: Longint): string;
var
  i: integer;
begin
  Result:= '';
  RandSeed:= Clave;
  while Length(Texto) > 0 do
  begin
    if not TryStrToInt('$'+Copy(Texto,1,2),i) then
    begin
      Result:= '';
      Exit;
    end;
    Result:= Result + Char(Byte(i) xor Byte(Random(256)));
    Delete(Texto,1,2);
  end;
end;

Ejemplo de uso:
Código Delphi [-]
var
  s: string;
begin
  s:= Cifrar('Hola mundo',1978);
  ShowMessage('Texto cifrado = ' + s);
  s:= Descifrar(s,1978);
  ShowMessage('Texto descifrado = ' + s);
end;
Responder Con Cita