Java文件加解密

Java文件加解密,第1张

做网站有时会处理一些上传下载的文件 可能会用到加解密功能 以下是一个加解密方法

Java代码

import java io File

import java io FileInputStream

import java io FileOutputStream

import java io IOException

import nf Conf

import mon time TimeHandler

/**

* 加解密单元

* @author lupingui

* : :

*/

public class EncryptDecrypt {

//加解密KEY 这个不能变动 这里可以由任意的字符组成 尽量用特殊字符

static final byte[] KEYVALUE = getBytes()

//读取字节的长度

static final int BUFFERLEN =

//加密临时存储目录

static final String TRANSIT_DIR_ENC =

//解密临时存储目录

static final String TRANSIT_DIR_DEC =

/**

* 文件加密

* @param oldFile 待加密文件

* @param saveFileName 加密后文件保存路径

* @return

* @throws IOException

*/

public static boolean encryptFile(File oldFile String saveFileName) throws IOException{

//如果传入的文件不存在或者不是文件则直接返回

if (!oldFile exists() || !oldFile isFile()){

return false

}

FileInputStream in = new FileInputStream(oldFile)

//加密后存储的文件

File file = new File(saveFileName)

if (!file exists()){

return false

}

//读取待加密文件加密后写入加密存储文件中

FileOutputStream out = new FileOutputStream(file)

int c pos keylen

pos =

keylen = KEYVALUE length

byte buffer[] = new byte[BUFFERLEN]

while ((c = in read(buffer)) != ) {

for (int i = i <ci++) {

buffer[i] ^= KEYVALUE[pos]

out write(buffer[i])

pos++

if (pos == keylen){

pos =

}

}

}

in close()

out close()

return true

}

/**

* 文件加密

* @param oldFile:待加密文件

* @param saveFile 加密后的文件

* @return

* @throws IOException

*/

public static boolean encryptFile(File oldFile File saveFile) throws IOException{

//如果传入的文件不存在或者不是文件则直接返回

if (!oldFile exists() || !oldFile isFile() || !saveFile exists() || !saveFile isFile()){

return false

}

FileInputStream in = new FileInputStream(oldFile)

//读取待加密文件加密后写入加密存储文件中

FileOutputStream out = new FileOutputStream(saveFile)

int c pos keylen

pos =

keylen = KEYVALUE length

byte buffer[] = new byte[BUFFERLEN]

while ((c = in read(buffer)) != ) {

for (int i = i <ci++) {

buffer[i] ^= KEYVALUE[pos]

out write(buffer[i])

pos++

if (pos == keylen){

pos =

}

}

}

in close()

out close()

return true

}

/**

* 文件加密

* @param oldFile 待加密文件

* @return

* @throws IOException

*/

public static File encryptFile(File oldFile) throws IOException{

//如果传入的文件不存在或者不是文件则直接返回

if (!oldFile exists() || !oldFile isFile()){

return null

}

FileInputStream in = new FileInputStream(oldFile)

//临时加密文件存储目录

File dirFile = new File(TRANSIT_DIR_ENC)

//如果临时存储目录不存在或不是目录则直接返回

if (!dirFile exists() || !dirFile isDirectory()){

return null

}

//加密后存储的文件

File file = new File(dirFile enc_ + TimeHandler getInstance() getTimeInMillis() + _ + oldFile getName())

if (!file exists()){

file createNewFile()

}

//读取待加密文件加密后写入加密存储文件中

FileOutputStream out = new FileOutputStream(file)

int c pos keylen

pos =

keylen = KEYVALUE length

byte buffer[] = new byte[BUFFERLEN]

while ((c = in read(buffer)) != ) {

for (int i = i <ci++) {

buffer[i] ^= KEYVALUE[pos]

out write(buffer[i])

pos++

if (pos == keylen){

pos =

}

}

}

in close()

out close()

//返回加密后的文件

return file

}

/**

* 文件加密

* @param oldFileName 待加密文件路径

* @return

* @throws IOException

* @throws Exception

*/

public static File encryptFile(String oldFileName) throws IOException {

//如果待加密文件路径不正确则直接返回

if (oldFileName == null || oldFileName trim() equals( )){

return null

}

//待加密文件

File oldFile = new File(oldFileName)

//如果传入的文件不存在或者不是文件则直接返回

if (!oldFile exists() || !oldFile isFile()){

return null

}

//调用文件加密方法并返回结果

return encryptFile(oldFile)

}

/**

* 文件解密

* @param oldFile 待解密文件

* @return

* @throws IOException

*/

public static File decryptFile(File oldFile) throws IOException{

//如果待解密文件不存在或者不是文件则直接返回

if (!oldFile exists() || !oldFile isFile()){

return null

}

FileInputStream in = new FileInputStream(oldFile)

//临时解密文件存储目录

File dirFile = new File(TRANSIT_DIR_DEC)

//如果临时解密文件存储目录不存在或不是目录则返回

if (!dirFile exists() || !dirFile isDirectory()){

return null

}

//解密存储文件

File file = new File(dirFile dec_ + TimeHandler getInstance() getTimeInMillis() + _ + oldFile getName() substring(oldFile getName() lastIndexOf( )))

if (!file exists()){

file createNewFile()

}

//读取待解密文件并进行解密存储

FileOutputStream out = new FileOutputStream(file)

int c pos keylen

pos =

keylen = KEYVALUE length

byte buffer[] = new byte[BUFFERLEN]

while ((c = in read(buffer)) != ) {

for (int i = i <ci++) {

buffer[i] ^= KEYVALUE[pos]

out write(buffer[i])

pos++

if (pos == keylen){

pos =

}

}

}

in close()

out close()

//返回解密结果文件

return file

}

/**

* 文件解密

* @param oldFileName 待解密文件路径

* @return

* @throws Exception

*/

public static File decryptFile(String oldFileName) throws Exception {

//如果待解密文件路径不正确则直接返回

if (oldFileName == null || oldFileName trim() equals( )){

return null

}

//待解密文件

File oldFile = new File(oldFileName)

//如果待解密文件不存在或不是文件则直接返回

if (!oldFile exists() || !oldFile isFile()){

return null

}

//调用文件解密方法并返回结果

return decryptFile(oldFile)

}

lishixinzhi/Article/program/Java/hx/201311/26983

Java基本的单向加密算法:

1.BASE64 严格地说,属于编码格式,而非加密算法

2.MD5(Message Digest algorithm 5,信息摘要算法)

3.SHA(Secure Hash Algorithm,安全散列算法)

4.HMAC(Hash Message Authentication Code,散列消息鉴别码)

按 照RFC2045的定义,Base64被定义为:Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)

常见于邮件、http加密,截取http信息,你就会发现登录 *** 作的用户名、密码字段通过BASE64加密的。

主要就是BASE64Encoder、BASE64Decoder两个类,我们只需要知道使用对应的方法即可。另,BASE加密后产生的字节位数是8的倍数,如果不够位数以=符号填充。

MD5

MD5 -- message-digest algorithm 5 (信息-摘要算法)缩写,广泛用于加密和解密技术,常用于文件校验。校验?不管文件多大,经过MD5后都能生成唯一的MD5值。好比现在的ISO校验,都 是MD5校验。怎么用?当然是把ISO经过MD5后产生MD5的值。一般下载linux-ISO的朋友都见过下载链接旁边放着MD5的串。就是用来验证文 件是否一致的。

HMAC

HMAC(Hash Message Authentication Code,散列消息鉴别码,基于密钥的Hash算法的认证协议。消息鉴别码实现鉴别的原理是,用公开函数和密钥产生一个固定长度的值作为认证标识,用这个 标识鉴别消息的完整性。使用一个密钥生成一个固定大小的小数据块,即MAC,并将其加入到消息中,然后传输。接收方利用与发送方共享的密钥进行鉴别认证 等。

基本的单向加密算法:

BASE64 严格地说,属于编码格式,而非加密算法

MD5(Message Digest algorithm 5,信息摘要算法)

SHA(Secure Hash Algorithm,安全散列算法)

HMAC(Hash Message Authentication Code,散列消息鉴别码)

复杂的对称加密(DES、PBE)、非对称加密算法:

DES(Data Encryption Standard,数据加密算法)

PBE(Password-based encryption,基于密码验证)

RSA(算法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)

DH(Diffie-Hellman算法,密钥一致协议)

DSA(Digital Signature Algorithm,数字签名)

ECC(Elliptic Curves Cryptography,椭圆曲线密码编码学)

代码参考:

/**

* BASE64加密

*

* @param key

* @return

* @throws Exception

*/

public static String encryptBASE64(byte[] key) throws Exception {

return (new BASE64Encoder()).encodeBuffer(key)

}

/**

* MD5加密

*

* @param data

* @return

* @throws Exception

*/

public static byte[] encryptMD5(byte[] data) throws Exception {

MessageDigest md5 = MessageDigest.getInstance(KEY_MD5)

md5.update(data)

return md5.digest()

}

/**

* SHA加密

*

* @param data

* @return

* @throws Exception

*/

public static byte[] encryptSHA(byte[] data) throws Exception {

MessageDigest sha = MessageDigest.getInstance(KEY_SHA)

sha.update(data)

return sha.digest()

}

}

/**

* 初始化HMAC密钥

*

* @return

* @throws Exception

*/

public static String initMacKey() throws Exception {

KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC)

SecretKey secretKey = keyGenerator.generateKey()

return encryptBASE64(secretKey.getEncoded())

}

/**

* HMAC加密

*

* @param data

* @param key

* @return

* @throws Exception

*/

public static byte[] encryptHMAC(byte[] data, String key) throws Exception {

SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC)

Mac mac = Mac.getInstance(secretKey.getAlgorithm())

mac.init(secretKey)

return mac.doFinal(data)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11878200.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存