繁体   English   中英

Flutter 中的加密和 java (AES-256) 中的解密

[英]Encryption in Flutter and Decryption in java (AES-256)

我正在尝试在 Flutter 中实现加密,我使用 java 作为后端,使用 dart 作为移动应用程序。 I have taken code from this Encryption in Java and Decryption in Flutter (AES-256) But it only supplies decryption in Flutter, and I want to implement encryption in Flutter so the Java code can decrypt it.

您能帮我提供 Flutter 中的加密代码吗?

这是java代码加解密。

public class EncryptionService {
    public String encrypt(String item) throws Exception {
        byte[] ivBytes;
        String password = "Hello";
        /* you can give whatever you want for password. This is for testing purpose */
        SecureRandom random = new SecureRandom();
        byte bytes[] = new byte[20];
        random.nextBytes(bytes);
        byte[] saltBytes = bytes;
        // Derive the key
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

        PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

        SecretKey secretKey = factory.generateSecret(spec);
        SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        System.out.println("saltBytes : " + saltBytes);

        // encrypting the word
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();
        ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
        System.out.println("ivBytes : " + ivBytes);

        byte[] encryptedTextBytes = cipher.doFinal(item.getBytes("UTF-8"));
        // prepend salt and vi
        byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
        System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
        System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
        System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length);
        return new Base64().encodeToString(buffer);
    }

    public String decrypt(String encryptedText) throws Exception {
        String password = "Hello";
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        // strip off the salt and iv
        ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
        byte[] saltBytes = new byte[20];
        buffer.get(saltBytes, 0, saltBytes.length);
        byte[] ivBytes1 = new byte[cipher.getBlockSize()];
        buffer.get(ivBytes1, 0, ivBytes1.length);
        byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];

        buffer.get(encryptedTextBytes);
        // Deriving the key
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);
        SecretKey secretKey = factory.generateSecret(spec);
        SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));
        byte[] decryptedTextBytes = null;
        try {
            decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return new String(decryptedTextBytes);
    }
}

用于解密的 Dart 实现是这样的

class EncryptionHelper {
  static String decrypt(
    String ciphertext,
  ) {
    Uint8List ciphertextlist = base64.decode(ciphertext);
    var salt = ciphertextlist.sublist(0, 20);
    var iv = ciphertextlist.sublist(20, 20 + 16);
    var encrypted = ciphertextlist.sublist(20 + 16);

    Uint8List key = generateKey("Hello", salt);
    CBCBlockCipher cipher = new CBCBlockCipher(new AESFastEngine());

    ParametersWithIV<KeyParameter> params =
        new ParametersWithIV<KeyParameter>(new KeyParameter(key), iv);

    PaddedBlockCipherParameters<ParametersWithIV<KeyParameter>, Null>
        paddingParams =
        new PaddedBlockCipherParameters<ParametersWithIV<KeyParameter>, Null>(
            params, null);
    PaddedBlockCipherImpl paddingCipher =
        new PaddedBlockCipherImpl(new PKCS7Padding(), cipher);
    paddingCipher.init(false, paddingParams);
    var val = paddingCipher.process(encrypted);

    return new String.fromCharCodes(val);
  }

  static Uint8List generateKey(String passphrase, Uint8List salt) {
    Uint8List passphraseInt8List = Uint8List.fromList(passphrase.codeUnits);

    KeyDerivator derivator =
        PBKDF2KeyDerivator(HMac(SHA1Digest(), 64)); // 64 byte block size
    Pbkdf2Parameters params =
        Pbkdf2Parameters(salt, 65556, 32); // 32 byte key size
    derivator.init(params);
    return derivator.process(passphraseInt8List);
  }
}

你为什么不使用pointcastle? 它是 Bouncy castle 的 dart 移植,正如我在他们的页面中看到的,还有用于解密/加密的 AES 算法的实现。

此外使用:

https://pub.dev/packages/encrypt

这是 pointcastle 顶部的一个方便的包装器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM