如何用Java读取CSV文件,然后用AES算法加密

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

我有以下代码用Java读取CSV文件,使用AES算法加密数据,然后将加密数据写入另一个CSV文件。

我现在得到这个例外:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at encryption.Reading.main(Reading.java:45)

有谁知道如何修复此异常?

我的CSV阅读器(此代码用于读取给定的CSV文件)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

    public class Reading {
        public static void main(String[] args) throws Exception {

                Aes aes=new Aes();

                String csvFile = "C:/Users/nana/Desktop/book1.csv";
                String csvFile1 = "C:/Users/nana/Desktop/output.csv";
                BufferedReader br = null;
                String line = "";
                String cvsSplitBy = ",";

                int j=0;
                String[] studentArray = new String[4];

                try {

                    br = new BufferedReader(new FileReader(csvFile));
                    while ((line = br.readLine()) != null) {

                        // use comma as separator
                        String[] country = line.split(cvsSplitBy);
                        String country1="";
                        for(int i=0;i<country.length;i++){
                             String password =  country[i];
                        String passwordEnc = Aes.encrypt(password);
                        country1=country1+passwordEnc+',';} 

                         studentArray[j]=country1;
                         j=j+1;

                    }
                    Csvwrite.writeCsvFile(csvFile1, studentArray);

                  }catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (br != null) {
                        try {
                            br.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }   }   }    }  }

我的CSV编写器(此代码用于将加密数据写入CSV文件)

import java.io.FileWriter;
import java.util.List;

public class Csvwrite{

   // private static final String COMMA_DELIMITER = ",";

    private static final String NEW_LINE_SEPARATOR = "\n";     
    private static final String FILE_HEADER = "name,age";
    public static void writeCsvFile(String fileName, String[]  aa) {

        FileWriter fileWriter = null;

        try {

            fileWriter = new FileWriter(fileName);

            //Write the CSV file header
            fileWriter.append(FILE_HEADER.toString());

            //Add a new line separator after the header

            fileWriter.append(NEW_LINE_SEPARATOR);

            //Write a new student object list to the CSV file

            for ( int k = 0;k<aa.length;k++)
            {

               /* String str=aa[k];
                for (int m=0;m<str.length();m++){*/

                fileWriter.append(aa[k]);

               // fileWriter.append(COMMA_DELIMITER);
                  fileWriter.append(NEW_LINE_SEPARATOR);

            }

            System.out.println("CSV file was created successfully !!!");
            fileWriter.flush();

            fileWriter.close();

    } catch (Exception e) {

        System.out.println("Error in CsvFileWriter !!!");
        e.printStackTrace();

    }   }    }

编码器:(此代码用于使用AES算法对我的数据进行编码)

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
public class Aes {

     private static final String ALGO = "AES";
    private static final byte[] keyValue = 
        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

public static String encrypt(String Data) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }
    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGO);
        return key;
} }
java encryption aes
1个回答
2
投票

studentArray [j]的

当j> = 4时,您遇到了问题。

使用像ArrayList这样的List的实现。

© www.soinside.com 2019 - 2024. All rights reserved.