Recent

Author Topic: [SOLVED] Storing and encrypting/decrypting passwords  (Read 21877 times)

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[SOLVED] Storing and encrypting/decrypting passwords
« on: April 28, 2016, 06:58:59 am »
Maybe a simple question or a difficult one...


But does Lazarus have a way of encrypt/decrypt passwords or do i have to write it myself?
« Last Edit: May 03, 2016, 08:50:48 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

balazsszekely

  • Guest
Re: Storing and encrypting passwords
« Reply #1 on: April 28, 2016, 07:10:46 am »
Quote
But does Lazarus have a way of encrypt/decrypt passwords or do i have to write it myself?
You have to write it yourself.  :P
http://wiki.freepascal.org/DCPcrypt
Use a hash for passwords.

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Storing and encrypting passwords
« Reply #2 on: April 28, 2016, 07:34:18 am »
FreePascal has units like BlowFish & MD5sum so you will be able to use them to encrypt passwords.

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

serbod

  • Full Member
  • ***
  • Posts: 142
Re: Storing and encrypting passwords
« Reply #3 on: April 28, 2016, 08:18:32 am »
MD5 is extremelly weak, use SHA-1 instead.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Storing and encrypting passwords
« Reply #4 on: April 29, 2016, 08:36:48 pm »
I am using the suggested module/unit/package.


I am using the following code to encrypt my password
Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm_Options.Button1Click(Sender: TObject);
  3. var
  4.   Texto: String;
  5.   Output: array [1..20] of byte;
  6.   i: integer;
  7. begin
  8.   Hash:= TDCP_sha1.Create(nil);
  9.   Hash.Init;
  10.   Hash.UpdateStr('1234567890');
  11.  
  12.  
  13.   Hash.Final(Output);
  14.   Hash.Free;
  15.  
  16.  
  17.   texto:='';
  18.   for i:=1 to 20 do
  19.   texto:=texto+inttohex(Output[i],1);
  20.   showmessage(texto);
  21. end;     // Button1Click    


But how can i decrypt my password?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

balazsszekely

  • Guest
Re: Storing and encrypting passwords
« Reply #5 on: April 29, 2016, 09:58:23 pm »
Quote
@madref
But how can i decrypt my password?
You can't! Please read more about hashing.
When a user enter a password, just hash it and compare with the saved values.
« Last Edit: April 29, 2016, 10:03:32 pm by GetMem »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Storing and encrypting passwords
« Reply #6 on: April 29, 2016, 10:11:57 pm »
But how can i decrypt my password?
You don't. Hash is one way, once hashed, you can't retrieve the original value back as hash allows collision (distinct values might hash to the same hashed value). What you do to login is to hash the entered password in the same way as you hash the correct password and compare the hashed value.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Storing and encrypting passwords
« Reply #7 on: April 29, 2016, 10:37:10 pm »
But how can i then encrypt en decrypt a string?


I want to use this to store my password and then retrieve it for use.
« Last Edit: April 29, 2016, 10:39:58 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Storing and encrypting passwords
« Reply #8 on: April 29, 2016, 11:12:20 pm »
Oh man...why todays programmers are so stubborn...? Fine, if you want encryption for password, go for it:
http://wiki.lazarus.freepascal.org/DCPcrypt
use the (block) ciphers, not the hashes. But don't say that we didn't warn you if someone can still find out the password and steal your money.

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Storing and encrypting passwords
« Reply #9 on: April 30, 2016, 12:26:26 am »
But how can i then encrypt en decrypt a string?

I want to use this to store my password and then retrieve it for use.

As GetMem & Leledumbo said earlier, you don't need to retrieve or decrypt the encrypted password. The sequence is as follows:

FIRST LOGIN
a) user enters password
b) encrypt the password
c) save the encrypted password

SUBSEQUENT LOGINS
a) user enters password
b) encrypt the password
c) compare the encrypted value to the value saved to file earlier in (c) above
d) if they are equal, allow access; if not tell the user his/her password does not match what is on file

JD
« Last Edit: April 30, 2016, 12:40:02 am by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Storing and encrypting passwords
« Reply #10 on: April 30, 2016, 12:34:48 am »
@madref

You might benefit from learning to add salt to passwords before you hash them. Read this thread.

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Storing and encrypting passwords
« Reply #11 on: April 30, 2016, 12:50:55 am »
@madref

I often recommend this article when I'm asked about passwords. Interesting reading if I may say so

You're Probably Storing Passwords Incorrectly
http://blog.codinghorror.com/youre-probably-storing-passwords-incorrectly/

JD
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Storing and encrypting passwords
« Reply #12 on: April 30, 2016, 03:10:40 pm »
Thats one thing i understand. It's logical.


But i want to store my password for the mailserver i use in a file so that i can access it.
So i have to be able to encrypt and decrypt it.
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Storing and encrypting passwords
« Reply #13 on: April 30, 2016, 04:45:49 pm »
MD5 is extremelly weak, use SHA-1 instead.
SHA-1 is extremely weak (not as weak as MD5) so use at a minimum SHA256.

And for the OP: plz don't be silly, don't store passwords for a mail server, only hashes. Anything else smells....
Usually your problem with decrypting (which is a no, no ,no) is solved by an out-of band solution that you know but is not in anyway connected to the password or file(s) you control. Like with SMS or (worse but still plausible) a question about your favorite dinner ever or something like that.
Don't ever store passwords from somebody else. it is a stupid idea.
« Last Edit: April 30, 2016, 04:54:03 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Storing and encrypting/decrypting passwords
« Reply #14 on: April 30, 2016, 04:51:03 pm »
Why the hell do you think i want to hash them !!!
And i am experimenting with it to get a better knowledge of it.
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

 

TinyPortal © 2005-2018