首页编程java编程java对图片加密是什么意思?用Java对图片进行加密

java对图片加密是什么意思?用Java对图片进行加密

编程之家 2023-10-10 110次浏览

大家好,java对图片加密是什么意思相信很多的网友都不是很明白,包括用Java对图片进行加密也是一样,不过没有关系,接下来就来为大家分享关于java对图片加密是什么意思和用Java对图片进行加密的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

java对图片加密是什么意思?用Java对图片进行加密

java加密的几种方式

基本的单向加密算法:

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

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

java对图片加密是什么意思?用Java对图片进行加密

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

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

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

java对图片加密是什么意思?用Java对图片进行加密

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加密

*

*@paramkey

*@return

*@throwsException

*/

publicstaticStringencryptBASE64(byte[]key)throwsException{

return(newBASE64Encoder()).encodeBuffer(key);

}

/**

*MD5加密

*

*@paramdata

*@return

*@throwsException

*/

publicstaticbyte[]encryptMD5(byte[]data)throwsException{

MessageDigestmd5=MessageDigest.getInstance(KEY_MD5);

md5.update(data);

returnmd5.digest();

}

/**

*SHA加密

*

*@paramdata

*@return

*@throwsException

*/

publicstaticbyte[]encryptSHA(byte[]data)throwsException{

MessageDigestsha=MessageDigest.getInstance(KEY_SHA);

sha.update(data);

returnsha.digest();

}

}

/**

*初始化HMAC密钥

*

*@return

*@throwsException

*/

publicstaticStringinitMacKey()throwsException{

KeyGeneratorkeyGenerator=KeyGenerator.getInstance(KEY_MAC);

SecretKeysecretKey=keyGenerator.generateKey();

returnencryptBASE64(secretKey.getEncoded());

}

/**

*HMAC加密

*

*@paramdata

*@paramkey

*@return

*@throwsException

*/

publicstaticbyte[]encryptHMAC(byte[]data,Stringkey)throwsException{

SecretKeysecretKey=newSecretKeySpec(decryptBASE64(key),KEY_MAC);

Macmac=Mac.getInstance(secretKey.getAlgorithm());

mac.init(secretKey);

returnmac.doFinal(data);

}

用Java对图片进行加密

System.out.println(newFileEncryptAndDecrypt().decrypt("D:\\a\\"

+filename,"D:\\a\\"+filename,6));

这就是解密啊,第一个参数是密文路径+文件名第二个参数是解密后文件存放的路径+文件名

第三个参数是密码长度+1

你密文和解密后文件指定成一个了,这样解密后的文件无法写入会导致异常的。

这种加密方式也太幼稚了,文件字节+1就算加密这已经让人不想吐槽了。

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符号++什么意思谁能给详细讲述一下 java的数组是什么结构(在Java中数组属于什么类型)