「java文件流加密」Java对文件加密

博主:adminadmin 2022-11-30 22:41:10 65

今天给各位分享java文件流加密的知识,其中也会对Java对文件加密进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java加密的几种方式

基本的单向加密算法:

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);

}

java 对大文件怎么加密

选择一个加密算法

输入为bufferIn,输出为bufferOut

循环读取文件,读满缓冲区就调用加密算法,然后输出至目标文件

不可能一次性将几兆的文件一次性进行加密的

JAVA程序加密,怎么做才安全

程序加密?你说的是代码加密还是数据加密。我都说一下吧。

Java代码加密:

这点因为Java是开源的,想达到完全加密,基本是不可能的,因为在反编译的时候,虽然反编译回来的时候可能不是您原来的代码,但是意思是接近的,所以是不行的。

那么怎么增加反编译的难度(阅读难度),那么可以采用多层继承(实现)方式来解决,这样即使反编译出来的代码,可读性太差,复用性太差了。

Java数据加密:

我们一般用校验性加密,常用的是MD5,优点是速度快,数据占用空间小。缺点是不可逆,所以我们一般用来校验数据有没有被改动等。

需要可逆,可以选用base64,Unicode,缺点是没有密钥,安全性不高。

而我们需要可逆而且采用安全的方式是:对称加密和非堆成加密,我们常用的有AES、DES等单密钥和双密钥的方式。而且是各种语言通用的。

全部手动敲字,望采纳,下面是我用Javascript方式做的一系列在线加密/解密工具:

java怎么加密文件啊

这个我不清楚。

加密文件,我使用的是超级加密3000.

超级加密3000有超快和最强的文件、文件夹加密功能、数据保护功能,文件夹、文件的粉碎删除以及文件夹伪装等功能。

超级加密

3000采用先进的加密算法,使你的文件和文件夹加密后,真正的达到超高的加密强度,让你的加密数据无懈可击。

超级加密3000还支持加密文件的临时解密,文件加密后,双击加密文件,在弹出密码输入对话框输入正确的密码选择确定,该加密文件就处于临时解密,文件使用完毕退出以后,它自动恢复到加密状态,无需再加密。

超级加密3000是一款不可多得的文件加密软件,您可以到百度上搜索超级加密3000给您的文件加密试试看。

java中如何实现对文件和字符串加密. 解密?

DES 密钥生成,加解密方法,,你可以看一下

//DES 密钥生成工具

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import java.security.spec.InvalidKeySpecException;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

public class GenKey {

private static final String DES = "DES";

public static final String SKEY_NAME = "key.des";

public static void genKey1(String path) {

// 密钥

SecretKey skey = null;

// 密钥随机数生成

SecureRandom sr = new SecureRandom();

//生成密钥文件

File file = genFile(path);

try {

// 获取密钥生成实例

KeyGenerator gen = KeyGenerator.getInstance(DES);

// 初始化密钥生成器

gen.init(sr);

// 生成密钥

skey = gen.generateKey();

// System.out.println(skey);

ObjectOutputStream oos = new ObjectOutputStream(

new FileOutputStream(file));

oos.writeObject(skey);

oos.close();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* @param file : 生成密钥的路径

* SecretKeyFactory 方式生成des密钥

* */

public static void genKey2(String path) {

// 密钥随机数生成

SecureRandom sr = new SecureRandom();

// byte[] bytes = {11,12,44,99,76,45,1,8};

byte[] bytes = sr.generateSeed(20);

// 密钥

SecretKey skey = null;

//生成密钥文件路径

File file = genFile(path);

try {

//创建deskeyspec对象

DESKeySpec desKeySpec = new DESKeySpec(bytes,9);

//实例化des密钥工厂

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

//生成密钥对象

skey = keyFactory.generateSecret(desKeySpec);

//写出密钥对象

ObjectOutputStream oos = new ObjectOutputStream(

new FileOutputStream(file));

oos.writeObject(skey);

oos.close();

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (InvalidKeyException e) {

e.printStackTrace();

} catch (InvalidKeySpecException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

private static File genFile(String path) {

String temp = null;

File newFile = null;

if (path.endsWith("/") || path.endsWith("\\")) {

temp = path;

} else {

temp = path + "/";

}

File pathFile = new File(temp);

if (!pathFile.exists())

pathFile.mkdirs();

newFile = new File(temp+SKEY_NAME);

return newFile;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

genKey2("E:/a/aa/");

}

}

//DES加解密方法

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import javax.crypto.Cipher;

import javax.crypto.CipherInputStream;

import javax.crypto.SecretKey;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

/**

*制卡文件加/解密 加密方式DES

*/

public class SecUtil {

public static final Log log = LogFactory.getLog(SecUtil.class);

/**

* 解密

*

* @param keyPath

* 密钥路径

* @param source

* 解密前文件

* @param dest

* 解密后文件

*/

public static void decrypt(String keyPath, String source, String dest) {

SecretKey key = null;

try {

ObjectInputStream keyFile = new ObjectInputStream(

// 读取加密密钥

new FileInputStream(keyPath));

key = (SecretKey) keyFile.readObject();

keyFile.close();

} catch (FileNotFoundException ey1) {

log.info("Error when read keyFile");

throw new RuntimeException(ey1);

} catch (Exception ey2) {

log.info("error when read the keyFile");

throw new RuntimeException(ey2);

}

// 用key产生Cipher

Cipher cipher = null;

try {

// 设置算法,应该与加密时的设置一样

cipher = Cipher.getInstance("DES");

// 设置解密模式

cipher.init(Cipher.DECRYPT_MODE, key);

} catch (Exception ey3) {

log.info("Error when create the cipher");

throw new RuntimeException(ey3);

}

// 取得要解密的文件并解密

File file = new File(source);

String filename = file.getName();

try {

// 输出流,请注意文件名称的获取

BufferedOutputStream out = new BufferedOutputStream(

new FileOutputStream(dest));

// 输入流

CipherInputStream in = new CipherInputStream(

new BufferedInputStream(new FileInputStream(file)), cipher);

int thebyte = 0;

while ((thebyte = in.read()) != -1) {

out.write(thebyte);

}

in.close();

out.close();

} catch (Exception ey5) {

log.info("Error when encrypt the file");

throw new RuntimeException(ey5);

}

}

/**

* 加密

* @param keyPath 密钥路径

* @param source 加密前文件

* @param dest 加密后文件

*/

public static void encrypt(String keyPath, String source, String dest) {

SecretKey key = null;

try {

ObjectInputStream keyFile = new ObjectInputStream(

// 读取加密密钥

new FileInputStream(keyPath));

key = (SecretKey) keyFile.readObject();

keyFile.close();

} catch (FileNotFoundException ey1) {

log.info("Error when read keyFile");

throw new RuntimeException(ey1);

} catch (Exception ey2) {

log.info("error when read the keyFile");

throw new RuntimeException(ey2);

}

// 用key产生Cipher

Cipher cipher = null;

try {

// 设置算法,应该与加密时的设置一样

cipher = Cipher.getInstance("DES");

// 设置解密模式

cipher.init(Cipher.ENCRYPT_MODE, key);

} catch (Exception ey3) {

log.info("Error when create the cipher");

throw new RuntimeException(ey3);

}

// 取得要解密的文件并解密

File file = new File(source);

String filename = file.getName();

try {

// 输出流,请注意文件名称的获取

BufferedOutputStream out = new BufferedOutputStream(

new FileOutputStream(dest));

// 输入流

CipherInputStream in = new CipherInputStream(

new BufferedInputStream(new FileInputStream(file)), cipher);

int thebyte = 0;

while ((thebyte = in.read()) != -1) {

out.write(thebyte);

}

in.close();

out.close();

} catch (Exception ey5) {

log.info("Error when encrypt the file");

throw new RuntimeException(ey5);

}

}

}

关于java文件流加密和Java对文件加密的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-30,除非注明,否则均为首码项目网原创文章,转载请注明出处。