DES加密由于在java中非常方便使用,而且性能不错,所以非常流行!!!
使用DES 首先需要密钥。。2种方法:第一自己设置
第二种:系统生成:
有了密钥之后开始对文件加密:详情见代码:【酷毙程序员,一切都在代码中】
当然也可分成小点方法:代码就好的看点
把解密分开写:
测试代码:速度相当之快,DES>>base64encode.估计也是流行原因之一
使用DES 首先需要密钥。。2种方法:第一自己设置
- //自己设置
- private
final static String encodeKey ="moomaia"; -
-
-
private Key getKey() throws Exception { -
// 创建一个空的8位字节数组(默认值为0) -
byte[] arrBTmp = encodeKey.getBytes(); -
System.out.println(arrBTmp.length); -
byte[] arrB = new byte[8]; -
// 将原始字节数组转换为8位 -
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { -
arrB[i] = arrBTmp[i]; -
} -
// 生成密钥 -
-
System.out.println(arrB.length); -
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); -
return key; -
} -
第二种:系统生成:
- private
void createKey() { -
try { -
// 得到密钥的实例 以什么方式加密。加密的方式比较多。 -
KeyGenerator kg = KeyGenerator.getInstance("DES"); -
kg.init(56); -
-
SecretKey key = kg.generateKey(); -
// 将生成的密钥对象写入文件。 -
ObjectOutputStream objectOutputStream = new ObjectOutputStream( -
new FileOutputStream(new File("e:\\key.obj"))); -
objectOutputStream.writeObject(key); -
} catch (NoSuchAlgorithmException e) { -
e.printStackTrace(); -
} catch (FileNotFoundException e) { -
e.printStackTrace(); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
} -
-
-
private static Key getKey(String KeyFilePath) { -
Key key = null; -
try { -
// 将生成的密钥对象从文件中读取出来,然后再强制转换成一个密钥对象。 -
ObjectInputStream objectInputStream = new ObjectInputStream( -
new FileInputStream(new File(KeyFilePath))); -
key = (Key) objectInputStream.readObject(); -
} catch (FileNotFoundException e) { -
e.printStackTrace(); -
} catch (IOException e) { -
e.printStackTrace(); -
} catch (ClassNotFoundException e) { -
e.printStackTrace(); -
} -
return key; -
}
有了密钥之后开始对文件加密:详情见代码:【酷毙程序员,一切都在代码中】
-
-
public String encrypt(String source) throws Exception { -
byte[] target =null; -
File srcFile = new File(source); -
System.out.println(srcFile.getCanonicalPath()); -
if(!srcFile.exists()){ -
System.out.println("文件不存在"); -
} -
String newName = srcFile.getAbsolutePath()+".m"; -
FileInputStream fis = new FileInputStream(srcFile); -
int fileSize = fis.available(); -
FileOutputStream fos = new FileOutputStream(new File(newName)); -
-
Key key = getKey(); -
try { -
Cipher cipher = Cipher.getInstance("DES"); -
cipher.init(Cipher.ENCRYPT_MODE, key); -
byte[] buf = new byte[fileSize]; -
System.out.println(fileSize); -
int c = 0; -
while((c=fis.read(buf))!=-1){ -
target = cipher.doFinal(buf); -
//base64encoder.encode(target,fos); -
fos.write(target); -
} -
} catch (NoSuchAlgorithmException e) { -
e.printStackTrace(); -
} catch (NoSuchPaddingException e) { -
e.printStackTrace(); -
} catch (UnsupportedEncodingExcep tion e) { -
e.printStackTrace(); -
} catch (InvalidKeyException e) { -
e.printStackTrace(); -
} catch (IllegalBlockSizeExceptio n e) { -
e.printStackTrace(); -
} catch (BadPaddingException e) { -
e.printStackTrace(); -
} -
return newName; -
}
当然也可分成小点方法:代码就好的看点
把解密分开写:
-
-
public byte[] decrypt(byte[] source) { -
byte[] dissect = null; -
try { -
Key key = getKey(); -
Cipher cipher = Cipher.getInstance("DES"); -
cipher.init(Cipher.DECRYPT_MODE, key);// 使用私钥解密 -
dissect = cipher.doFinal(source); -
} catch (NoSuchAlgorithmException e) { -
e.printStackTrace(); -
} catch (NoSuchPaddingException e) { -
e.printStackTrace(); -
} catch (InvalidKeyException e) { -
e.printStackTrace(); -
} catch (IllegalBlockSizeExceptio n e) { -
e.printStackTrace(); -
} catch (BadPaddingException e) { -
e.printStackTrace(); -
} catch (Exception e) { -
// TODO Auto-generated catch block -
e.printStackTrace(); -
} -
return dissect; -
} -
-
-
-
public byte[] getByteFromFile(){ -
//BASE64Decoder base64decoder = new BASE64Decoder(); -
byte[] encodeByte =null; -
try { -
// encodeByte = base64decoder.decodeBuffer(new FileInputStream(new File("D:\\t.txt"))); -
FileInputStream fis =new FileInputStream(new File("D:\\ch03.pdf.m")); -
int fileSize = fis.available(); -
encodeByte = new byte[fileSize]; -
fis.read(encodeByte); -
} catch (FileNotFoundException e) { -
e.printStackTrace(); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
return encodeByte; -
} -
-
-
public void writeByteToFile(byte[] b, String filePath) { -
File file = new File(filePath); -
if (!file.exists()) { -
try { -
file.createNewFile(); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
} -
FileOutputStream fileOutputStream; -
try { -
fileOutputStream = new FileOutputStream(file); -
-
fileOutputStream.write(b); -
fileOutputStream.close(); -
} catch (IOException e) { -
e.printStackTrace(); -
} -
}
测试代码:速度相当之快,DES>>base64encode.估计也是流行原因之一
- public
static void main(String[] args) throws Exception { -
-
DESofFile desf= new DESofFile(); -
//desf.createKey(); -
Date times = new Date(); -
long start = times.getTime(); -
desf.encrypt("d:\\ch03.pdf"); -
desf.writeByteToFile(desf.decrypt(desf.getByteFromFile()), "d:\\ch03-9.pdf"); -
Date times2 = new Date(); -
long end =times2.getTime(); -
-
System.out.println(end - start); -
}