AKushWarrior / steel_crypt

A collection of high-level API's exposing PointyCastle to perform hashing and encrypting in popular/secure algorithms.
https://pub.dev/packages/steel_crypt
Mozilla Public License 2.0
40 stars 10 forks source link

Encrypt and Decrypt MD5? #2

Closed gladson closed 4 years ago

gladson commented 4 years ago

final encrypter = HashCrypt('MD5'); final result = encrypter.hash('123'); duvida

final encrypter = HashCrypt('MD5');
final encodeResult = encrypter.hash('1234');

duvida2

I need to decode the generated hash.

final decodeResult = encrypter.decodehash("2FoqAjj0eOGRjccTlMQZEg==");

result => 1234

AKushWarrior commented 4 years ago

I'm going to divide your issue into two parts: The "divide-by-four" exception, and the "decode" request.

1) So this is a function of the way I do hashing, and I'm working on a secure solution as we speak. There will be a hotfix in the coming days, but you probably won't end up needing it, because of the answer to part 2.

2) This question is a little more complex, but, simply put, you can't. MD5 is what's known as a hashing algorithm. Hashing algorithms are one-way, meaning you can't decrypt them. Instead, you can use it to verify that two strings are the same. The most common use case for this is passwords, as you can hash them, and then use the hashed password to securely verify if any new entry is the same. What you are looking for is a two-way encryption algorithm. With these, you can both encrypt and decrypt chunks of text. My personal recommendation is AES, which is a standardized, secure algorithm. The way you achieve AES encryption varies based on what version of steel_crypt you have installed:

0.6.1 or higher:

var private = CryptKey().genKey();

var iv = CryptKey().genIV(16);

var encrypter = AesCrypt(private);

print(encrypter.encrypt('word', iv)); //encrypt

String crypted = encrypter.encrypt('word', iv);

print(encrypter.decrypt(crypted, iv)); //decrypt

0.5.6 or lower:

var private = CryptKey().genKey();

var iv = CryptKey().genIV(16);

var encrypter = SymCrypt(private, 'AES');

print(encrypter.encrypt('word', iv)); //encrypt

String crypted = encrypter.encrypt('word', iv);

print(encrypter.decrypt(crypted, iv)); //decrypt

Note: If you end up using AES, your problems with the "multiple-of-four" thing will disappear.

Further Reading: You can read more about two-way vs hashing at https://www.securityinnovationeurope.com/blog/page/whats-the-difference-between-hashing-and-encrypting

AKushWarrior commented 4 years ago

For some reason, the code blocks aren't working, but I think it's still readable.

EDIT: Got the code blocks working. Have a good one!

AKushWarrior commented 4 years ago

I'm going to close this issue. If the solution above doesn't work for you, please let me know.