gpt4 book ai didi

java - 如何用Java读取CSV文件然后通过AES算法加密

转载 作者:行者123 更新时间:2023-12-02 01:55:59 27 4
gpt4 key购买 nike

我有以下代码来用 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 writer(此代码用于将我的加密数据写入 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;
} }

最佳答案

学生数组[j]

当 j >= 4 时,你就会遇到问题。

使用像 ArrayList 这样的 List 实现。

关于java - 如何用Java读取CSV文件然后通过AES算法加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276352/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com