Encryption and decryption in flutter

This tutorial explores the data encryption and decryption using the dart language for the flutter apps. We come across the requirements of encrypting and decrypting data when we start building real-world applications.

For supporting encryption, we need to add a library called encrypt. It provides all the functionalities we require for encrypting, encoding encrypted data to base16 or base64, and of course for decryption.

Add the following line into your pubspec.yaml file under dependencies: section. This will add the ‘encrypt’ library into our project.

encrypt: ^5.0.1

To use the library in the code, import it as follows.

import 'package:encrypt/encrypt.dart';

AES Encryption & Decryption

AES aka Advanced Encryption Standard is a very widely used symmetric block encryption technique. AES can use 128, 192 or 256 length keys.

How the encryption key bit length is calculated?

The AES-128 can have a 128-bit key. You can either use the Key.fromSecureRandom(16) function to generate 128-bit keys. Or else, you need to provide a 16 character string as a key.

For example,

  • “I am 16-char key” contains 16 characters. Each character is 8 bit wide. It can be used as a 128-bit key.
  • “This 32 char key have 256 bits!!” contains 32 characters. Each character is 8 bit wide. So, it will become 256-bit key.

Example program for AES-CBC encryption and decryption

Let’s understand the encryption and decryption with an example code. The following program utilizes AES-CBC-PKSC7 algorithm.

import 'package:encrypt/encrypt.dart';

void main() {
  final plainText = "Fallback! I repeat fallback to sector 12!!";
  final key = "This 32 char key have 256 bits..";

  print('Plain text for encryption: $plainText');

  //Encrypt
  Encrypted encrypted = encryptWithAES(key, plainText);
  String encryptedBase64 = encrypted.base64;
  print('Encrypted data in base64 encoding: $encryptedBase64');

  //Decrypt
  String decryptedText = decryptWithAES(key, encrypted);
  print('Decrypted data: $decryptedText');
}

///Accepts encrypted data and decrypt it. Returns plain text
String decryptWithAES(String key, Encrypted encryptedData) {
  final cipherKey = Key.fromUtf8(key);
  final encryptService = Encrypter(AES(cipherKey, mode: AESMode.cbc)); //Using AES CBC encryption
  final initVector = IV.fromUtf8(key.substring(0, 16)); //Here the IV is generated from key. This is for example only. Use some other text or random data as IV for better security.

  return encryptService.decrypt(encryptedData, iv: initVector);
}

///Encrypts the given plainText using the key. Returns encrypted data
Encrypted encryptWithAES(String key, String plainText) {
  final cipherKey = Key.fromUtf8(key);
  final encryptService = Encrypter(AES(cipherKey, mode: AESMode.cbc));
  final initVector = IV.fromUtf8(key.substring(0, 16)); //Here the IV is generated from key. This is for example only. Use some other text or random data as IV for better security.

  Encrypted encryptedData = encryptService.encrypt(plainText, iv: initVector);
  return encryptedData;
}

The output of the program is as follows

Plain text for encryption: Fallback! I repeat fallback to sector 12!!
Encrypted data in base64 encoding: WI18tczGAOQfiLKGyKyXKcuCZeK9d8K9ONUeVvPEnC8H86l0EBWa76drqdZpmcXO
Decrypted data: Fallback! I repeat fallback to sector 12!!
Flutter AES Encryption and Decryption Example Output
Flutter AES Encryption and Decryption Example Output

What is an initialization vector?

IV or initVector as denoted in the above code is known as initialization vector. It is an arbitrary data that can be used along with a secret key for more secure data encryption. This is needed because, multiple people can use the same key and create the same encrypted data which can then be compromised. Using IV along with key make sure that the probability of being someone else using IV and KEY is very less. In the above code snippet, the IV is derived from key. It is for example purpose only. Always use random generated IV.

RSA Encryption and Decryption

RSA(Rivest Shamir Adleman) is a widely used public-key asymmetric encryption algorithm. You can do encryption and decryption with RSA in flutter with the encrypt library.

import 'package:encrypt/encrypt.dart';
import 'package:pointycastle/asymmetric/api.dart';

void main() {
  final publicKey = await readKeyFromFile<RSAPublicKey>('/public.pem'); //Read public key from file
  final privateKey = await readKeyFromFile<RSAPrivateKey>('/private.pem'); //Read private key from file

  final plainText = 'Your plain text to be encrypted';
  final encryptService = Encrypter(RSA(publicKey: publicKey, privateKey: privateKey));

  //Encrypt
  final encrypted = encryptService.encrypt(plainText);
  final encryptedBase64 = encrypted.base64;
  print('Encrypted data in base64 encoding: $encryptedBase64');

  //Decrypt
  final decrypted = encryptService.decrypt(encrypted);
  print('Decrypted data: $decrypted');
}

You can find more elaborate example here in GitHub.

More supported algorithms

The ‘encrypt’ library support some other encryption algorithms like Fernet and Salsa20.

Conclusion

In this tutorial, we have explored the encryption and decryption support in dart for flutter apps. We have analyzed an example program for AES algorithm and went through an RSA algorithm example as well. If you liked this article, you might want to have a look into some other flutter articles I have written.

Comments

74 responses to “Encryption and decryption in flutter”

  1. […] In the above example, the IV is created from the key itself to keep the code easy to read. Use random data for IV for better security. Referred article for flutter encryption. […]

  2. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  3. … [Trackback]

    […] Here you will find 29540 more Information on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  4. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  5. … [Trackback]

    […] Information to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  6. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  7. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  8. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  9. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  10. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  11. … [Trackback]

    […] Here you will find 11987 additional Info to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  12. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  13. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  14. … [Trackback]

    […] Info to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  15. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  16. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  17. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  18. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  19. … [Trackback]

    […] Information to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  20. … [Trackback]

    […] Read More on on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  21. … [Trackback]

    […] Info on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  22. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  23. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  24. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  25. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  26. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  27. … [Trackback]

    […] Info on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  28. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  29. … [Trackback]

    […] Information to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  30. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  31. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  32. … [Trackback]

    […] Information to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  33. … [Trackback]

    […] Information to that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  34. … [Trackback]

    […] Information on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  35. … [Trackback]

    […] Info on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  36. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/encryption-and-decryption-in-flutter/ […]

  37. […] rybelsus online sales […]

  38. […] clomid 50mg for male […]

  39. […] sildenafil citrate tablets 50mg […]

  40. […] how much is cialis […]

  41. […] canadian pharmacy cialis 40 mg […]

  42. […] Siberian vs Korean ginseng for men […]

  43. get androxal generic uae

    cheap androxal buy adelaide

  44. usa phizer brand hame enclomiphene from phizer

    enclomiphene mail order singapore

  45. rifaximin cheap cod

    canadian rifaximin for purchase

  46. cheap xifaxan purchase toronto

    get xifaxan buy sydney

  47. ordering staxyn generic cheap

    ordering staxyn generic new zealand

  48. buy avodart generic online buy

    low price avodart without prescription

  49. online dutasteride canada

    is dutasteride over the counter in Australia

  50. order flexeril cyclobenzaprine price by pharmacy

    cheapest buy flexeril cyclobenzaprine purchase tablets

  51. cheap gabapentin without prescription

    24 meds gabapentin

  52. cheapest buy fildena cost effectiveness

    Buying fildena online without a perscription

  53. order itraconazole generic switzerland

    Purchase itraconazole online

  54. kamagra porovnat cenu cvs

    kamagra cenových obchodů

  55. acheter kamagra en ligne sans ordonnance

    acheter à bas prix kamagra

  56. […] sildenafil tab 25mg cost […]

  57. […] 50 milligram viagra […]

  58. […] how to get cialis online […]

  59. […] blue viagra pill 100 […]

  60. […] sildenafil citrate 50 mg […]

  61. […] sildenafil citrate tablets 50 mg […]

  62. hello world

    hello world

  63. lasix furosemide 40 mg

    lasix furosemide 40 mg

  64. stromectol tablets canada

    stromectol tablets canada

  65. tadalafil 20 mg price

    tadalafil 20 mg price

  66. fluconazole for yeast infection

    fluconazole for yeast infection

  67. antibiotics for tonsillitis

    antibiotics for tonsillitis

  68. prevacid medication

    prevacid medication

  69. doryx generic

    doryx generic

  70. diflucan uti medication

    diflucan uti medication

  71. augmentin for pneumonia

    augmentin for pneumonia

Leave a Reply