从数组列表中加密数组中的每个值? Java

From Array list encrypt every value inside array? Java

是否可以对数组中的每个对象值进行加密?我的加密器只接受字符串而不接受数组,我已经被困了 4 天任何帮助都会很棒!

我想从 [a,b,c,d,e] 加密到 --> [@#%,@#%,@#%,!@$!$,@ #$@#%]

这是我的加密密码!

    public class Aes {
        private static final String SECRET_KEY = "my_super_secret_key_ho_ho_ho";
        private static final String SALT = "sfasf";
        public static String encrypt(String strToEncrypt) {
            try {
                byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                IvParameterSpec ivspec = new IvParameterSpec(iv);
    
                SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
                KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT.getBytes(), 65536, 256);
                SecretKey tmp = factory.generateSecret(spec);
                SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
    
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
                return Base64.getEncoder()
                        .encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
            } catch (Exception e) {
                System.out.println("Error while encrypting: " + e.toString());
            }
            return null;

这是我的 reader 数组列表代码 txt 文件:

    public ArrayList reader() throws IOException {
        while (true) {
            System.out.println("Give me the Data file here : ");
            String path = "";
            Scanner sc = new Scanner(System.in);
            path = sc.nextLine();
            if (path.contains(".txt")) {
                FileReader fr = new FileReader(path);
                BufferedReader br = new BufferedReader(fr);
                String str;
                String[] wordsArray;
                while ((str = br.readLine()) != null) {

                    wordsArray = str.split(" ");
                    for (String each : wordsArray) {
                        if (!"".equals(each)) {
                            words.add(each);
                        }
                    }
                }

                System.out.println(words);
                br.close();
                return words;
            } else {
                System.out.println("Wrong type,try again");
            }
        }
    }

您可以使用 Stream#map.

List<String> list = List.of("a", "b", "c");
List<String> result = list.stream().map(Aes::encrypt).collect(Collectors.toList());