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

How to do CBC-PKCS7 and iv=MD5(key)? #23

Closed s349856186 closed 4 years ago

s349856186 commented 4 years ago

I used JS encrypt as below: CryptoJS.AES.encrypt(pw, SHA256(key), {iv:MD5(key), mode:CBC, padding:Pkcs7})

How to do use this crypt ? Thanks

AKushWarrior commented 4 years ago

Okay, so this is how you do this here:

import 'package:steel_crypt/steel_crypt.dart';
var hash1 = HashCrypt('SHA-256');
var hash2 = HashCrypt('MD5');
var keyAES = hash1.hash(key); //If key is the same variable from your example
print(AesCrypt(keyAES, 'cbc', 'pkcs7').encrypt(pw); //if pw is the same variable from your example

Lemme know if you have questions or issues. Closing this for now.

s349856186 commented 4 years ago

@AKushWarrior Thanks for your reply. var pw = "1234"; var key = "123"; var hash1 = HashCrypt('SHA-256'); var hash2 = HashCrypt('MD5'); var keyAES = hash1.hash(key); var pwd = AesCrypt(keyAES, 'cbc', 'pkcs7').encrypt(pw); print("=log=111="+pwd); But hash2 not used, and the code get error: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s): Initialization vector must be the same length as block size

AKushWarrior commented 4 years ago

@s349856186 whoops. The below should work.

import 'package:steel_crypt/steel_crypt.dart';
var hash1 = HashCrypt('SHA-256');
var hash2 = HashCrypt('MD5');
var keyAES = hash1.hash(key); //If key is the same variable from your example
print(AesCrypt(keyAES, 'cbc', 'pkcs7').encrypt(pw, hash2.hash(key))); //if pw and key are the same variables from your example
s349856186 commented 4 years ago

@AKushWarrior Thanks for your reply. I tried this method before and still got the same error, so I do n’t know how to use it. Can you help me,Thanks

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Invalid argument(s): Initialization vector must be the same length as block size

s349856186 commented 4 years ago

@AKushWarrior Can you help me,Thanks

AKushWarrior commented 4 years ago

Sorry, I wasn't monitoring this issue for some reason. I'll take a look...

AKushWarrior commented 4 years ago

Ah. The problem is that AES uses UTF-16 codeunits, whereas HashCrypt() uses base64 encoding/decoding.

This will be fixed in steel_crypt 2.0. Tune into #22 .

AKushWarrior commented 4 years ago

Okay, what I said was not the problem at all. You should use the new "raw" classes, and just take the first 16 digits of the hash as the iv.

AKushWarrior commented 4 years ago

I can't give you a concrete code example until you give me what encoding CryptoJS uses.