Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
s349856186 opened this issue Feb 28, 2020 · 9 comments
Closed

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

s349856186 opened this issue Feb 28, 2020 · 9 comments
Labels
clarification User is having trouble understanding classes educational Good for learning about the package

Comments

@s349856186
Copy link

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
Copy link
Owner

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.

@AKushWarrior AKushWarrior added clarification User is having trouble understanding classes educational Good for learning about the package labels Mar 1, 2020
@s349856186
Copy link
Author

@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
Copy link
Owner

AKushWarrior commented Mar 3, 2020

@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
Copy link
Author

@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
Copy link
Author

@AKushWarrior
Can you help me,Thanks

@AKushWarrior
Copy link
Owner

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

@AKushWarrior AKushWarrior reopened this Mar 19, 2020
@AKushWarrior
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clarification User is having trouble understanding classes educational Good for learning about the package
Projects
None yet
Development

No branches or pull requests

2 participants