Skip to main content

Command Palette

Search for a command to run...

How to encrypt your text messages in flutter using Encrypt package

Published
1 min read
A

Somethings about me JAVA for DS JAVASCRIPT FOR CREATIVITY HASHNODE For Blogs Node Js Because i love how things works behind the scene And love you because you visit me

  1. Install Encrypt package from https://pub.dev/packages/encrypt
  2. Import your package import 'package:encrypt/encrypt.dart' as enc;
  3. Use the below code for encryption and decryption
  4. Point to remember key length should be either 32 lengths,64 length and 128 length
void encryption(String text) {
    final key = enc.Key.fromUtf8('my 32 length key................');

    final iv = enc.IV.fromLength(1);

    final encrypter = enc.Encrypter(AES(key));

    final encrypted = encrypter.encrypt(text, iv: iv);

    setState(() {
      encryptedText = encrypted.base64;
    });
  }

  void decrypt(String text) {
    final key = enc.Key.fromUtf8('my 32 length key................');

    final iv = enc.IV.fromLength(1);
    final encrypter = enc.Encrypter(AES(key));
    final decrypted = encrypter.decrypt64(text, iv: iv);
    setState(() {
      decryptedText = decrypted;
    });
  }

More from this blog

Web Developer having good knowledge in designing and problem solving skills. I have an ample amount of experience in Frontend Technology

13 posts

I am a FullStack developer having good amount of experience in FrontEnd Technology like Html,css,javascript,React,Flutter,Node js,Database.I am a good problem solver having knowledge in DSA