Chandan Vidyarthi
Chandan Vidyarthi

Reputation: 1

AES encryption in flutter android

here is my code that was written in java in android for encryption - I am using AES encryption using private key length of 256 and PKCS5 padding.

Please let me know how can i use same encryption method in flutter also. I have also used PointyCastle but it did not work for me.

public AES() {
   cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
   _key = new byte[32]; //256 bit key space
   _iv = new byte[16]; //128 bit IV
}

I want to achieve encryption in flutter using same encryption method. please help me to achieve this in flutter.

Upvotes: 0

Views: 1608

Answers (2)

Hassan Ali Hassan
Hassan Ali Hassan

Reputation: 27

I have write both java and dart working together Using AES CBC mode PKCS5 padding Note PKCS5 in Java Equivalent to PKCS7 in Dart - and they works together you can encrypt in java and decrypt in Dart and vise versa this Java Part


import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/*********this class to Encrypt and decrypt data used in sending and receiving  data**** */

public class Encryptor {

    static Encryptor instance = null;


    private Encryptor(){
        instance=this;
    }


    final static String AccKey = "ShaniaTwain2023f";
       
    public static void main(String[] args)  throws Exception{
        //() TODO Auto-generated method stub
         
          String originalString = "I like Shaina Twain Songs";
          
          byte[] ecryptedString =Encryptor.getInstance().encrypt(Encryptor.AccKey, originalString);
          String decryptedString = Encryptor.getInstance().decrypt(new String(ecryptedString) , Encryptor.AccKey) ;    
        }
     public    byte[]  encrypt (String key, String value) {
         
             String encryptString = "";
             byte[] keyBytes = new byte[16];
        
           try {
              Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //1
               SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");//2
               IvParameterSpec ivSpec = new IvParameterSpec(keyBytes); //3
               cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); //4
                encryptString = Base64.getEncoder().encodeToString(cipher.doFinal(value.getBytes("UTF-8")));//5
           
           } catch (Exception e) {
               e.printStackTrace();
           }
           return encryptString.getBytes();
            }
        public   String decrypt (String encrypted , String Key) {
            byte[] keyBytes = new byte[16];//1
            
            try {
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");//1
                SecretKeySpec skeySpec = new SecretKeySpec(Key.getBytes("UTF-8"), "AES");//2
                IvParameterSpec iv = new IvParameterSpec(keyBytes);//3
                cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);//4
                byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));//5
                    return new String(original);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                return null;
        }
        public static Encryptor getInstance() {
            // TODO Auto-generated method stub
            if (instance==null) {
                return new Encryptor();
            }else {
                return instance;
            }
        }
}


this Dart Part using encrypt 5.0.1

import 'dart:typed_data';
import 'package:encrypt/encrypt.dart' as ency;
import 'package:flutter/material.dart';

 
class Encryptor_ {
  static final key = ency.Key.fromUtf8('ShaniaTwain2023f');
  static final iv = ency.IV.fromLength(16);
  static final encrypter =
      ency.Encrypter(ency.AES(key, mode: ency.AESMode.cbc, padding: 'PKCS7'));

  static String encrypt(plainText) {
    return encrypter.encrypt(plainText, iv: iv).base64.toString();
  }

  static String decrypt(plainText) {
    ency.Encrypted encrypted = ency.Encrypted.from64(plainText);
    return encrypter.decrypt(encrypted, iv: iv).toString();
  }
}

void main() {
  print(Encryptor_.encrypt("Hassan"));
}

Upvotes: 0

Greco
Greco

Reputation: 49

This is how I implemented it in flutter to encrypt a string.

Create a dart file crypto.dart and copy/paste the contents below.

import 'package:encrypt/encrypt.dart';

final aesKey = '<your key';

String encryptData(final String data)
{
  final key = Key.fromUtf8(aesKey);
  final iv = IV.fromUtf8(aesKey);
  final aesEncrypter = Encrypter(AES(key, mode: AESMode.cbc));
  
  final encrypted = aesEncrypter.encrypt(data, iv: iv);
  return encrypted.base64;
}

// Decrypt data
String decryptData(final String encrypted)
{
  final key = Key.fromUtf8(aesKey);
  final iv = IV.fromUtf8(aesKey);
  final aesEncrypter = Encrypter(AES(key, mode: AESMode.cbc));

  final data = aesEncrypter.decrypt64(encrypted, iv: iv);
  return data;
}

Then you can import the crypt.dart file and call the appropriate methods required to encrypt/decrypt.

Upvotes: 0

Related Questions