小编mun*_*ong的帖子

将ArrayList <String>转换为byte []

我希望能够转换ArrayList<String>存储从BufferedReader读取的文件内容的内容,然后将内容转换为byte [],以允许使用Java的Cipher类对其进行加密.

我尝试过使用.getBytes()但它没有工作,因为我认为我需要首先转换ArrayList,而我在弄清楚如何做到这一点时遇到了麻烦.

码:

// File variable
private static String file;

// From main()
file = args[2];

private static void sendData(SecretKey desedeKey, DataOutputStream dos) throws Exception {
        ArrayList<String> fileString = new ArrayList<String>();
        String line;
        String userFile = file + ".txt";

        BufferedReader in = new BufferedReader(new FileReader(userFile));
        while ((line = in.readLine()) != null) {
            fileString.add(line.getBytes()); //error here
        }

        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, desedeKey);
        byte[] output = cipher.doFinal(fileString.getBytes("UTF-8")); //error here
        dos.writeInt(output.length);
        dos.write(output);
        System.out.println("Encrypted Data: " + Arrays.toString(output)); …
Run Code Online (Sandbox Code Playgroud)

java string encryption byte arraylist

1
推荐指数
2
解决办法
457
查看次数

标签 统计

arraylist ×1

byte ×1

encryption ×1

java ×1

string ×1