Monday, November 28, 2022
HomeiOS Developmentandroid - I've a code encrypt/decrypt knowledge wish to convert Flutter or...

android – I’ve a code encrypt/decrypt knowledge wish to convert Flutter or iOS code


I’ve a code to encrypt/decrypt knowledge, beforehand written on android, however do not know tips on how to convert it to Flutter code or iOS technique channel, are you able to assist me, please!
Under is the android code:

Instance enter:

  1. enter: “mobileapi|cauugf6ORCafNuvfhBNxLg:APA91bGWk7S0Z1we_YrKm9Hc-FVG04230kodgyuQftmKL7mf4Stwt-hypkYzSzJH19emDxnKdEQN1IclTyCfGCWAN–5qasNLr3Dxski9IcEt3WXLmN2heDG1BWZboD_Vphq3Jx7f_TG”
  2. key: 28103264-9141-4540-a55b-c4ec6596ee2d
  3. iv: byte[] IV = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
import java.math.BigInteger;
import java.safety.AlgorithmParameters;
import java.safety.MessageDigest;
import java.safety.SecureRandom;
import java.safety.spec.KeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;
import android.util.Log;

public class Encryption {

    public static byte[] getSHA(String enter)throws Exception{
        strive {
            // Static getInstance technique is named with hashing SHA
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] messageDigest = md.digest(enter.getBytes());
            return messageDigest;
        }
        // For specifying mistaken message digest algorithms
        catch (Exception e) {
            return null;
        }
    }

    public static byte[] encrypt (byte[] plaintext,String key,byte[] IV )throws Exception{
        strive
        {
            //Get Cipher Occasion
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            //Create SecretKeySpec
            SecretKeySpec keySpec = new SecretKeySpec(getSHA(key), "AES");
            //Create IvParameterSpec
            IvParameterSpec ivSpec = new IvParameterSpec(IV);
            //Initialize Cipher for ENCRYPT_MODE
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            //Carry out Encryption
            byte[] cipherText = cipher.doFinal(plaintext);
            return cipherText;
        } catch(Exception e){
            return null;            // At all times should return one thing
        }
    }

    public static String decrypt (byte[] cipherText, String key,byte[] IV)throws Exception{
        strive {
            //Get Cipher Occasion
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            //Create SecretKeySpec
            SecretKeySpec keySpec = new SecretKeySpec(getSHA(key), "AES");
            //Create IvParameterSpec
            IvParameterSpec ivSpec = new IvParameterSpec(IV);
            //Initialize Cipher for DECRYPT_MODE
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            //Carry out Decryption
            byte[] decryptedText = cipher.doFinal(cipherText);
            return new String(decryptedText);
        }
        catch(Exception e){
            return null;            // At all times should return one thing
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments