Java文件加密
文章作者 100test 发表时间 2011:08:06 01:48:57
来源 100Test.Com百考试题网
package com.happy.security.properties.
import java.io.ByteArrayInputStream.
import java.io.ByteArrayOutputStream.
import java.io.File.
import java.io.FileInputStream.
import java.io.FileOutputStream.
import java.io.InputStream.
import java.io.ObjectInputStream.
import java.io.ObjectOutputStream.
import java.security.Key.
import java.security.NoSuchAlgorithmException.
import java.security.SecureRandom.
import java.security.Security.
import javax.crypto.Cipher.
import javax.crypto.KeyGenerator.
public class DESEncryptUtil {
public static Key createKey() throws NoSuchAlgorithmException {//创建密钥
Security.insertProviderAt(new com.sun.crypto.provider.SunJCE(), 1).
KeyGenerator generator = KeyGenerator.getInstance("DES").
generator.init(new SecureRandom()).
Key key = generator.generateKey().
return key.
}
public static Key getKey(InputStream is) {
try {
ObjectInputStream ois = new ObjectInputStream(is).
return (Key) ois.readObject().
} catch (Exception e) {
e.printStackTrace().
throw new RuntimeException(e).
}
}