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

Cant decrypt AES stored value #297

Open
anywheresa opened this issue Mar 23, 2023 · 1 comment
Open

Cant decrypt AES stored value #297

anywheresa opened this issue Mar 23, 2023 · 1 comment

Comments

@anywheresa
Copy link

anywheresa commented Mar 23, 2023

I have an encrypted base64 string stored in my db. Now I am trying to decrypt it.
ERROR MESSAGE (in the highlighted line below):
The argument type 'String' cant be assigned to the parameter type 'Encrypted'.

Future pwDecrypt(String password) async {
final key = encrypt.Key.fromSecureRandom(32);
final iv = encrypt.IV.fromSecureRandom(16);
final encrypter = encrypt.Encrypter(encrypt.AES(key));

final decrypted = encrypter.decrypt(password, iv: iv);
FFAppState().tempPassword = decrypted;
}

@pncsoares
Copy link

pncsoares commented Mar 31, 2023

@anywheresa try this:

import * as CryptoJS from 'crypto-js';

export const decrypt = (secret: string, encryptedString: string): string => {
  const keyWordArray = CryptoJS.enc.Utf8.parse(secret);
  const ivWordArray = CryptoJS.enc.Utf8.parse(''.padStart(16, '\0'));
  const cipherTextBase64 = encryptedString;

  const decryptedWordArray = CryptoJS.AES.decrypt(cipherTextBase64, keyWordArray, {
    mode: CryptoJS.mode.CTR,
    iv: ivWordArray,
    padding: CryptoJS.pad.NoPadding,
  });

  const decryptedString = decryptedWordArray.toString(CryptoJS.enc.Utf8);

  return removeEscapeSequences(decryptedString);
};

export const removeEscapeSequences = (text: string): string => {
  return text.replace(/[^ -~]+/g, '');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants