11

I have a string - a serial number of a mother board (only numbers and letters). How to encrypt/decrypt it and have a normal view: letters only from A to Z and numbers from 0 to 9. A user must send me the string, I must response.

I can encrypt but with not readable characters.

Thanks!

2
  • 1
    Please clarify your question. It looks like you're asking about an encryption scheme that will leave you with a human-viewable "ASCII" string. If so, then use any ol' encryption scheme, but Base64 encode the string. Commented Jul 23, 2011 at 4:18
  • right!!! I need a human-viewable string
    – maxfax
    Commented Jul 23, 2011 at 5:33

4 Answers 4

20

Simple Enc/Dec with support Unicode , Enc output is only hexadecimal characters :

const CKEY1 = 53761;
      CKEY2 = 32618;

function EncryptStr(const S :WideString; Key: Word): String;
var   i          :Integer;
      RStr       :RawByteString;
      RStrB      :TBytes Absolute RStr;
begin
  Result:= '';
  RStr:= UTF8Encode(S);
  for i := 0 to Length(RStr)-1 do begin
    RStrB[i] := RStrB[i] xor (Key shr 8);
    Key := (RStrB[i] + Key) * CKEY1 + CKEY2;
  end;
  for i := 0 to Length(RStr)-1 do begin
    Result:= Result + IntToHex(RStrB[i], 2);
  end;
end;

function DecryptStr(const S: String; Key: Word): String;
var   i, tmpKey  :Integer;
      RStr       :RawByteString;
      RStrB      :TBytes Absolute RStr;
      tmpStr     :string;
begin
  tmpStr:= UpperCase(S);
  SetLength(RStr, Length(tmpStr) div 2);
  i:= 1;
  try
    while (i < Length(tmpStr)) do begin
      RStrB[i div 2]:= StrToInt('$' + tmpStr[i] + tmpStr[i+1]);
      Inc(i, 2);
    end;
  except
    Result:= '';
    Exit;
  end;
  for i := 0 to Length(RStr)-1 do begin
    tmpKey:= RStrB[i];
    RStrB[i] := RStrB[i] xor (Key shr 8);
    Key := (tmpKey + Key) * CKEY1 + CKEY2;
  end;
  Result:= UTF8Decode(RStr);
end;

Example :

procedure TForm1.btn1Click(Sender: TObject);
begin
  txt2.Text:= EncryptStr(txt1.Text, 223);
  lbl1.Caption:= DecryptStr(txt2.Text, 223);
end;
6
  • Why is the 's' parameter declared as string in one function and widestring in the other???
    – IceCold
    Commented Aug 17, 2017 at 17:16
  • Why do you useTRY/EXCEPT in decryptstr?
    – IceCold
    Commented Aug 17, 2017 at 17:18
  • The code has an warning: W1000 Symbol 'UTF8Decode' is deprecated: 'Use UTF8ToWideString or UTF8ToString'
    – IceCold
    Commented Aug 17, 2017 at 17:20
  • I get a RangeCheckError in EncryptStr. Probably this is why you used the Try/Except. To hide RangeCheckErrors.
    – IceCold
    Commented Aug 17, 2017 at 17:24
  • Sorry. Your code is execrable. I don't know how you got all those upvotes!
    – IceCold
    Commented Aug 17, 2017 at 17:24
14

The best way is encrypt and then encode the string.

Check this sample which uses the JWSCL library to encrypt a string and Indy to encode and decode in base64.

{$APPTYPE CONSOLE}

uses
  ExceptionLog,
  Classes,
  JwaWinType,
  JwaWinCrypt,
  IdCoderMIME,
  SysUtils;

function CryptString(Const  Input: string; password : AnsiString;  Encrypt: Boolean) : string;
const
  BufferSize=1024*1024;
var
  StreamSource  : TStringStream;
  StreamDest    : TStringStream;
  CRYPTPROV     : HCRYPTPROV;
  CRYPTHASH     : HCRYPTHASH;
  CRYPTKEY      : HCRYPTKEY;
  Buffer        : LPBYTE;
  BytesIn       : DWORD;
  Final         : Boolean;

  Encoder     : TIdEncoderMIME;
  Decoder     : TIdDecoderMIME;
  DestStream  : TStringStream;
begin
  CryptAcquireContext(CRYPTPROV, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
  try
      //create a valid key  based in the password
      if not CryptCreateHash(CRYPTPROV, CALG_SHA1, 0, 0, CRYPTHASH) then RaiseLastOSError;
      try
        if not CryptHashData(CRYPTHASH, @Password[1], Length(Password), 0) then RaiseLastOSError;
        if not CryptDeriveKey(CRYPTPROV,  CALG_RC4, CRYPTHASH, 0, CRYPTKEY)  then RaiseLastOSError;
      finally
        CryptDestroyHash(CRYPTHASH);
      end;

      StreamSource := TStringStream.Create(Input);
      StreamSource.Position:=0;
      StreamDest   := TStringStream.Create;
      try
        GetMem(Buffer, BufferSize);
        try

          if not Encrypt then
          begin
            //decode the string using base64
            Decoder := TIdDecoderMIME.Create(nil);
            try
              DestStream := TStringStream.Create;
              try
                StreamDest.Position:=0;
                Decoder.DecodeBegin(DestStream);
                Decoder.Decode(StreamSource);
                Decoder.DecodeEnd;
                StreamSource.Clear;
                DestStream.Position:=0;
                StreamSource.CopyFrom(DestStream,DestStream.Size);
                StreamSource.Position:=0;
              finally
                DestStream.Free;
              end;
            finally
              Decoder.Free;
            end;

          end;


            repeat
              BytesIn   := StreamSource.Read(Buffer^, BufferSize);
              Final     := (StreamSource.Position >= StreamSource.Size);
              if Encrypt then
              begin
               if not CryptEncrypt(CRYPTKEY, 0, Final, 0, Buffer, BytesIn, BytesIn) then RaiseLastOSError;
              end
              else
              if not CryptDecrypt(CRYPTKEY, 0, Final, 0, Buffer, BytesIn) then RaiseLastOSError;

              StreamDest.Write(Buffer^, BytesIn);
            until Final;


          //encode the string using base64
          if Encrypt then
          begin
            Encoder := TIdEncoderMIME.Create(nil);
            try
              DestStream:=TStringStream.Create;
              try
                StreamDest.Position:=0;
                Encoder.Encode(StreamDest,DestStream);
                Result := DestStream.DataString;
              finally
                DestStream.Free;
              end;
            finally
              Encoder.Free;
            end;
          end
          else
          Result:= StreamDest.DataString;


        finally
         FreeMem(Buffer, BufferSize);
        end;

      finally
        StreamSource.Free;
        StreamDest.Free;
      end;
  finally
    CryptReleaseContext(CRYPTPROV, 0);
  end;
end;


var 
   plaintext : string; 
   Encrypted : string;
begin
  try
    plaintext:='this is a plain text'; Writeln('Plain Text '+plaintext);
    Encrypted:=CryptString(plaintext,'...ThiS Is A PaSsWord...',True);
    Writeln('Encrypted/Encoded string '+Encrypted);
    plaintext:=CryptString(Encrypted,'...ThiS Is A PaSsWord...',False);
    Writeln('Original string '+plaintext);
  except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.
11
  • Thanks you!!! But everyone knows Base64. It has '=' - ease to recognize and easy to decode.
    – maxfax
    Commented Jul 23, 2011 at 8:46
  • Thats not matter because the string is encrypted. if someone decode the base64 string will had a encrypted string.
    – RRUZ
    Commented Jul 23, 2011 at 9:21
  • 1
    @maxfax, check the code I updated the code adding a sample of use and small modifications to support old (non unicode) versions of delphi.
    – RRUZ
    Commented Jul 31, 2011 at 22:06
  • 1
    Hey, if the = bothers you, remove it and then add it back in when you go to decode it. It won't matter, the encryption is what makes it secure. The Base64 is just for the human-readable requirement. Commented Feb 20, 2013 at 22:13
  • 2
    Nice code BUT it uses a 30MB library. Why all Jedi libraries are an entanglement of files and dependences.
    – IceCold
    Commented Aug 17, 2017 at 17:12
8

This is some kind of the well-known ROT13 crypt:

// will crypt A..Z, a..z, 0..9 characters by rotating
function Crypt(const s: string): string;
var i: integer;
begin
  result := s;
  for i := 1 to length(s) do
    case ord(s[i]) of
    ord('A')..ord('M'),ord('a')..ord('m'): result[i] := chr(ord(s[i])+13);
    ord('N')..ord('Z'),ord('n')..ord('z'): result[i] := chr(ord(s[i])-13);
    ord('0')..ord('4'): result[i] := chr(ord(s[i])+5);
    ord('5')..ord('9'): result[i] := chr(ord(s[i])-5);
    end;
end;

Any characters other than A..Z, a..z, 0..9 will stay unchanged.

2
  • isn't a bit TOO simple?
    – IceCold
    Commented Aug 17, 2017 at 17:08
  • 2
    @Allforfree It is clearly simple obfuscation, not cryptographic encryption. Use AES for something stronger. But you will need to use a proven library. See e.g. this method for symetric encryption, and this one for asymetric computation of the key. Commented Aug 20, 2017 at 14:47
4

I use this very simple trick found on https://edn.embarcadero.com/article/28325 that is good for its simplicity:

I use it to store non critical encrypted passwords in a database. My goal is to put a layer of obfuscation (and minimize the computation needed for decrypting), not to do serious encryption, for admin password for example i use AES:

// this function both  encryptes and decryptes
// EnDecrypt(EnDeCrypt(astring)) = astring
function EnDeCrypt(const Value : String) : String;
var
  CharIndex : integer;
begin
  Result := Value;
  for CharIndex := 1 to Length(Value) do
    Result[CharIndex] := chr(not(ord(Value[CharIndex])));
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.