JAVA文件加密与解密

JAVA文件加密与解密

加密算法:DES

加密模式:CBC(密码分组链)模式

分组填充方式:PKCS#5-Padding

名词解释:

Ø        DES

DES算法是一种对称加密机制,由IBM美国政府于70年代开发。
其入口参数有三个:keydatamode 其中Key8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;ModeDES的工作方式,有两种:加密或解密。
实际运用中,密钥只用到了64位中的56位,这样才具有高的安全性。

 

Ø        CBC模式:

所有在本文档中描述或涉及的对称块加密算法都使用加密块链接(BCB)模式。这种模式
的算法需要一个和块的长度一样大小的初始化向量(IV)。使用一个随机产生的IV,防止完
全一致的和加密算法块尺寸一样长度的第一个明文数据块产生完全一致的密文。
在数据加密之前,IV和第一个明文块进行XOR运算。然后对于连续的块,在当前的明文
块加密之前,先和前一个密文块进行一次XOR运算。

 

Ø        MessageDigest类:

提供一种安全的单程哈希函数,把任意长度的数据转换成固定长度的哈希值。

该函数通过update方法把要处理的数据传入MessageDigest类,然后调用digest方法完成转换。此时MessageDigest对象又回到了原来的初始状态。

这是一个抽像类,应用程序开发人员只需要注意他的几个方法,因为在这个超类中的方法是专门留给愿意把自己的消息摘要算法实现提供出来的密码服务提供者设计的。

 

Ø        DESKeySpec类:

DES密钥的的详细说明。

 

Ø        SecretKeyFactory类:

一种密钥转换机制,用于密钥(Key)和密钥规范(key specification)之间的互相转换。这个类只作用于秘密密钥(对称密钥)。通过key spefication转换成key.

这个类会为指定的密码算法产生SecretKeyFactory对象。

 

Ø        SealedObject类:

给定一个序列化对象,用SealedObject对象把原始对象封装起来,然后使用一种加密算法用序列化的方式把它的原始内容加密。而加密的内容也可以重新序列化,使用正确的密钥解密。

原始对象可以通过两种方式还原:

1.通过一个密码对象(Cipher

2.通过一个密钥对象(Key

 

Ø        Cipher类:

通过传入的一个转换信息字符串,产生一个Cipher对象。

转换格式:

1.  “algorithm/mode/padding”

2.  “algorithm”

 

如:Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
     
     

  
  
   
    
  
  

  
  
   
    
  
  

Ø         主要异常有:

NosuchAlgorithmException

NosuchPaddingException

 

 

 
文件加密解密算法(Java源码) java,file,算法,加密解密,java源码 package com.crypto.encrypt; import java.security.SecureRandom; import java.io.*; import javax.crypto.spec.DESKeySpec; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.Cipher; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import javax.crypto.NoSuchPaddingException; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import java.lang.reflect.Constructor; import java.security.spec.KeySpec; import java.lang.reflect.InvocationTargetException; public class EncryptData { private String keyfile=null; public EncryptData() { } public EncryptData(String keyfile) { this.keyfile=keyfile; } /** * 加密文件 * @param filename String 源路径 * @param filenamekey String 加密后的路径 */ public void createEncryptData(String filename,String filenamekey) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, IOException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IllegalStateException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, IOException { //验证keyfile if(keyfile==null || keyfile.equals("")) { throw new NullPointerException("无效的key文件路径"); } encryptData(filename,filenamekey); } /** * 加密文件 * @param filename String 原始的类文件 * @param encryptfile String 加密后的类文件 * @throws IOException * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws BadPaddingException * @throws IllegalBlockSizeException * @throws IllegalStateException */ private void encryptData(String filename,String encryptfile) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, IllegalStateException, ClassNotFoundException, SecurityException, NoSuchMethodException, InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException { byte data[]=Util.readFile(filename); // 执行加密操作 byte encryptedClassData[] = getencryptData(data); // 保存加密后的文件,覆盖原有的类文件。 Util.writeFile(encryptedClassData,encryptfile); } /** * 直接获得加密数据 * @param bytes byte[] * @throws IllegalStateException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidKeyException * @throws NoSuchPaddingException * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws InstantiationException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws SecurityException * @throws ClassNotFoundException * @throws IOException * @return byte[] */ public byte[] createEncryptData(byte[] bytes) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IOException { bytes=getencryptData(bytes); return bytes; } private byte[] getencryptData(byte[] bytes) throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IllegalStateException { // 产生一个可信任的随机数源 SecureRandom sr = new SecureRandom(); //从密钥文件key Filename中得到密钥数据 byte[] rawKeyData = Util.readFile(keyfile); // 从原始密钥数据创建DESKeySpec对象 Class classkeyspec=Class.forName(Util.getValue("keyspec")); Constructor constructor = classkeyspec.getConstructor(new Class[]{byte[].class}); KeySpec dks = (KeySpec) constructor.newInstance(new Object[]{rawKeyData}); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(Util.getAlgorithm()); SecretKey key = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(Util.getAlgorithm()); // 用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, key, sr); // 执行加密操作 bytes = cipher.doFinal(bytes); // 返回字节数组 return bytes; } /** * 设置key文件路径 * @param keyfile String */ public void setKeyFile(String keyfile) { this.keyfile=keyfile; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值