文件加密解密

对称加密算法,AES 128位加密算法

/**
* 加密文件infilename,输出加密后的文件outfilename 返回AES加密密钥
*/
public static byte[] AesEnCrypt(String infilename, String outfilename)
throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey key = keyGen.generateKey();

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

InputStream in = new FileInputStream(infilename);
DataOutputStream out = new DataOutputStream(new FileOutputStream(
outfilename));

cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipher);

in.close();
out.close();
return key.getEncoded();
}

/**
* 用密钥k解密文件infilename,输出明文文件outfilename
*
* @param infilename
* @param outfilename
* @param k
*/
public static void AesDeCrypt(String infilename, String outfilename,
byte[] k) {

try {

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

SecretKey key = new javax.crypto.spec.SecretKeySpec(k, "AES");
OutputStream out = new FileOutputStream(outfilename);
DataInputStream in = new DataInputStream(new FileInputStream(
infilename));

cipher.init(Cipher.DECRYPT_MODE, key);
crypt(in, out, cipher);

in.close();
out.close();

} catch (GeneralSecurityException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}

}

/**
* 自己定义的加 密函数
*
* @param in
* @param out
* @param cipher
* @throws IOException
* @throws GeneralSecurityException
*/
public static void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException, GeneralSecurityException {
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while (more) {
inLength = in.read(inBytes);
if (inLength == blockSize) {
int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
out.write(outBytes, 0, outLength);
} else {
more = false;
}
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.write(outBytes);
}



public static String crypt(String in,Cipher cipher)
throws IOException, GeneralSecurityException {
StringBuffer out = new StringBuffer();
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
while (more) {
for(int i = 0;i<in.getBytes().length;i++)
{
System.out.println(in.getBytes().length*8);
if(in.getBytes().length>=i*blockSize)
{
System.arraycopy(in, 0*blockSize, inBytes, 0, blockSize);
cipher.update(inBytes, 0, blockSize, outBytes);
out.append(outBytes);
}
else
{
inLength = in.getBytes().length - i*blockSize;
more = false;
}
}
}
if (inLength > 0)
outBytes = cipher.doFinal(inBytes, 0, inLength);
else
outBytes = cipher.doFinal();
out.append(outBytes);
return out.toString();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值