使用 DART 的 AES-CBC PKCS7 数据解密

问题描述 投票:0回答:0

你能帮我解密 DART 从 Java 后端加密的数据吗? 这是我的代码,但我有任何解密数据的问题,如果我们是其他方式来解密数据

import 'dart:convert';
import 'dart:typed_data';
import 'package:pointycastle/export.dart';

void main() {
  String encryptedData = "VnZPWFp0aG13RE1tT3lOaW1tT3pDTlV0dGJYRWZmbTQ="; // données chiffrées en base64
  String password = "monMotDePasse"; // le mot de passe utilisé pour dériver la clé
  String salt = "monSelSecret"; // le sel utilisé pour dériver la clé

  // décoder le sel en base64
  Uint8List saltBytes = base64.decode(salt);

  // dériver la clé à partir du mot de passe et du sel en utilisant PBKDF2
  final pbkdf2 = PBKDF2KeyDerivator(HMac(SHA256Digest(), 64))
    ..init(Pbkdf2Parameters(saltBytes, 10000, 32));

  Uint8List keyBytes = Uint8List(32);
  Uint8List encryptedDataBytes =
        Uint8List.fromList(password.codeUnits);
  pbkdf2.deriveKey(encryptedDataBytes, 0, keyBytes, 0);

  // décoder les données chiffrées en base64
  Uint8List encryptedBytes = base64.decode(encryptedData);

  // initialiser le déchiffreur avec la clé et le vecteur d'initialisation
  final cbc = CBCBlockCipher(AESFastEngine())
    ..init(false, ParametersWithIV(KeyParameter(keyBytes), saltBytes));

  // initialiser le déchiffreur avec le mode de remplissage PKCS7
  final cipher = PaddedBlockCipherImpl(PKCS7(), cbc);

  // déchiffrer les données
  Uint8List decryptedBytes = cipher.process(encryptedBytes);

  // convertir les données déchiffrées en une chaîne de caractères
  String decryptedData = utf8.decode(decryptedBytes);

  print(decryptedData); // afficher les données déchiffrées
}

我在下面询问加密代码。 这是后端开发人员用来加密数据的 Java 代码:

private Key getCipherKey(String encryptionKey,String encryptionSalt) throws Exception {
        byte[] salt = encryptionSalt.getBytes();
        PBEKeySpec pbeKeySpec = new PBEKeySpec(encryptionKey.toCharArray(), salt, cipherIteration, cipherLength);
        SecretKey pbeKey = SecretKeyFactory.getInstance(keyFactory).generateSecret(pbeKeySpec);
        return new SecretKeySpec(pbeKey.getEncoded(), EncryptionAlgorithmType.AES.name());
    }

public String encrypt(final Object valueEnc,String encryptionKey,String encryptionSalt) {

        String encryptedVal;
        try {
            final Key key = getCipherKey(encryptionKey,encryptionSalt);
            final Cipher cipher = Cipher.getInstance(EncryptionAlgorithmType.AES.name());
            cipher.init(Cipher.ENCRYPT_MODE, key);
            final byte[] encValue = cipher.doFinal(mapper.writeValueAsString(valueEnc).getBytes());
            encryptedVal = Base64.encodeBase64String(encValue);
        } catch (Exception ex) {
            log.info("The Exception is=" + ex);
            throw  new EncryptionException("Nous n'avons pas pu sécuriser les données ");
        }

        return encryptedVal;
    }
flutter dart aes pkcs#7
© www.soinside.com 2019 - 2024. All rights reserved.