Ad
Decrypt Upcoming Text Flutter
I just want to enter string and on press decrypt it.
this is my app, but I can't decrypt the string cause _cipher.text
type 'String'. it's type should be 'Encrypted'
onPressed: () {
setState(() {
final k = Key.fromUtf8('1234567891011123');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(k));
final decrypted = encrypter.decrypt(_cipher.text, iv: iv);
_plain.text = decrypted;
});
},
I don't want to use final crypted = encrypter.encrypt(_cipher.text, iv: iv);
cause it comes encrypted.
Ad
Answer
You can't just directly decrypt the string. You need to pass in the encrypted one into decrypt otherwise it would give block size error in most of the cases.
import 'package:encrypt/encrypt.dart';
void main() {
final cipher = 'Lorem ipsum dolor sit amet';
final k = Key.fromUtf8('1234567891011123');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(k));
final encrypted = encrypter.encrypt(cipher, iv: iv);
final decrypted = encrypter.decrypt(encrypted, iv: iv);
print(encrypted.base64);
print(decrypted);
}
If you have manually set the cipher as base16
or base64
string, you can do this :
import 'package:encrypt/encrypt.dart';
void main() {
final cipher = 'Xg4+gtUDU0Hd9uMUWU7IJtjxvocKzIOJwumyzbY5n40=';
final k = Key.fromUtf8('1234567891011123');
final iv = IV.fromLength(16);
final encrypter = Encrypter(AES(k));
final decrypted = encrypter.decrypt(Encrypted.fromBase64(cipher), iv: iv);
print(decrypted);
}
Edit: If you are taking the cipher input from user, you need to validate that using the encrypter if it's a base16
or base64
only then you can continue to decrypt using Encrypted.fromBase64(cipher)
or Encrypted.fromBase16(cipher)
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad