加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

主题:java版AES文件加密速度问题

发布时间:2022-10-16 21:31:48 所属栏目:Unix 来源:网络
导读: 简单的一个java版的AES文件加密demo, 运行正常, 但文件一大速度就会很慢,不知道是否能优化一下,以提高增快加密的速度或许是我的代码写法有问题unix 文件加密, 希望各位大俠指正
import jav

简单的一个java版的AES文件加密demo, 运行正常, 但文件一大速度就会很慢,不知道是否能优化一下,以提高增快加密的速度或许是我的代码写法有问题unix 文件加密, 希望各位大俠指正

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class AES {

// 加密文件

public static void encryptfile(String pwd, File fileIn) throws Exception {

try {

//读取文件

FileInputStream fis = new FileInputStream(fileIn);

byte[] bytIn = new byte[(int) fileIn.length()];

for (int i = 0; i < fileIn.length(); i++) {

bytIn[i] = (byte) fis.read();

}

//AES加密

KeyGenerator kgen = KeyGenerator.getInstance("AES");

kgen.init(128, new SecureRandom(pwd.getBytes()));

SecretKey skey = kgen.generateKey();

byte[] raw = skey.getEncoded();

SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

//写文件

byte[] bytOut = cipher.doFinal(bytIn);

FileOutputStream fos = new FileOutputStream(fileIn.getPath()

+ ".aes");

for (int i = 0; i < bytOut.length; i++) {

fos.write((int) bytOut[i]);

}

fos.close();

fis.close();

} catch (Exception e) {

throw new Exception(e.getMessage());

}

}

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

AES aes = new AES();

String pwd = "123";

File file = new File("d:/xxx.doc");

aes.encryptfile(pwd, file);

}

}

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!