java 对称加密 非对称加密_Java安全之对称加密、非对称加密、数字签名

Java中加密分为两种方式一个是对称加密,另一个是非对称加密。对称加密是因为加密和解密的钥匙相同,而非对称加密是加密和解密的钥匙不同。

对称加密与非对称加密的区别:

对称加密称为密钥加密,速度快,但加密和解密的钥匙必须相同,只有通信双方才能知道密钥。

非对称加密称为公钥加密,算法更加复杂,速度慢,加密和解密钥匙不相同,任何人都可以知道公钥,只有一个人持有私钥可以解密。

对称加密解密:

/*

* 对称加密

*/

privatestaticvoidsecretEncrypt()throwsException {

//使用Cipher的实例

Cipher cipher =Cipher.getInstance("AES");

//得到加密的钥匙

SecretKey key =KeyGenerator.getInstance("AES").generateKey();

//初始化加密操作,传递加密的钥匙

cipher.init(Cipher.ENCRYPT_MODE,key);

//将加密的钥匙写入secretKey.key文件中

FileOutputStream fosKey=newFileOutputStream("secretKey.key");

ObjectOutputStream oosSecretKey =newObjectOutputStream(fosKey);

oosSecretKey.writeObject(key);

oosSecretKey.close();

fosKey.close();

//将加密的内容传递进去,返回加密后的二进制数据

byte[] results =cipher.doFinal("哈哈哈哈哈".getBytes());

//将加密后的二进制数据写入到secretContent.dat文件中

FileOutputStream fosData=newFileOutputStream("secretContent.dat");

fosData.write(results);

fosData.close();

}

/*

* 对称解密

*/

privatestaticvoidsecretDecrypt()throwsException{

Cipher cipher =Cipher.getInstance("AES");

//获取文件中的key进行解密

FileInputStream fisKey=newFileInputStream("secretKey.key");

ObjectInputStream oisKey =newObjectInputStream(fisKey);

Key key =(Key)oisKey.readObject();

oisKey.close();

fisKey.close();

//初始化解密操作,传递加密的钥匙

cipher.init(Cipher.DECRYPT_MODE,key);

//获取文件中的二进制数据

FileInputStream fisDat=newFileInputStream("secretContent.dat");

//获取数据第一种方式

byte[] src=newbyte[fisDat.available()];

intlen =fisDat.read(src);

inttotal =0;

while(total

total +=len;

len=fisDat.read(src,total,src.length-total);

}

//执行解密

byte[] result=cipher.doFinal(src);

fisDat.close();

System.out.println(newString(result));

//      读文件中的数据第二种方式

//      ByteArrayOutputStream baos =new ByteArrayOutputStream();

//      copyStream(fisDat, baos);

//      byte [] result=cipher.doFinal(baos.toByteArray());

//      fisDat.close();

//      baos.close();

}

//  private static void copyStream(InputStream ips,OutputStream ops) throws Exception{

//      byte [] buf =new byte[1024];

//      int len=ips.read(buf);

//      while(len!=-1){

//          ops.write(buf,0,len);

//          len  =ips.read(buf);

//      }

//  }

基于口令的对称加密与解密

系统自动生成的Key不容易记忆,我们可以使用我们容易记忆的口令同过java自带的一个工具将它转换成Key,在解密的时候我们就可以通过口令进行解密。

/*

* 基于口令的对称加密

*/

privatestaticvoidsecretEncrypt()throwsException {

//实例化工具

Cipher cipher2=Cipher.getInstance("PBEWithMD5AndDES");

//使用该工具将基于密码的形式生成Key

SecretKey key2=SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(newPBEKeySpec("123".toCharArray()));

PBEParameterSpec parameterspec=newPBEParameterSpec(newbyte[]{1,2,3,4,5,6,7,8},1000);

//初始化加密操作,同时传递加密的算法

cipher2.init(Cipher.ENCRYPT_MODE,key2,parameterspec);

//将要加密的数据传递进去,返回加密后的数据

byte[] results =cipher2.doFinal("哈哈哈哈哈".getBytes());

//将加密后的数据写入到文件中

FileOutputStream fosData=newFileOutputStream("zxx.dat");

fosData.write(results);

fosData.close();

}

/*

* 基于口令的对称解密

*/

privatestaticvoidsecretDecrypt()throwsException{

Cipher cipher2=Cipher.getInstance("PBEWithMD5AndDES");

SecretKey key2=SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(newPBEKeySpec("123".toCharArray()));

PBEParameterSpec parameterspec=newPBEParameterSpec(newbyte[]{1,2,3,4,5,6,7,8},1000);

cipher2.init(Cipher.DECRYPT_MODE,key2,parameterspec);

FileInputStream fisDat=newFileInputStream("zxx.dat");

byte[] src=newbyte[fisDat.available()];

intlen =fisDat.read(src);

inttotal =0;

while(total

total +=len;

len=fisDat.read(src,total,src.length-total);

}

byte[] result=cipher2.doFinal(src);

fisDat.close();

System.out.println(newString(result));

}

非对称加密解密:

非对称加密是公钥加密,私钥来解密,这个个人做用的少一点,主要针对于大型的网站大型的企业

/*

* 公钥加密

*/

privatestaticvoidPublicEnrypt()throwsException {

Cipher cipher =Cipher.getInstance("RSA");

//实例化Key

KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");

//获取一对钥匙

KeyPair keyPair=keyPairGenerator.generateKeyPair();

//获得公钥

Key publicKey=keyPair.getPublic();

//获得私钥

Key privateKey=keyPair.getPrivate();

//用公钥加密

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] result=cipher.doFinal("传智播客".getBytes("UTF-8"));

//将Key写入到文件

saveKey(privateKey,"zxx_private.key");

//加密后的数据写入到文件

saveData(result,"public_encryt.dat");

}

/*

* 私钥解密

*/

privatestaticvoidprivateDecrypt()throwsException {

Cipher cipher=Cipher.getInstance("RSA");

//得到Key

Key privateKey=readKey("zxx_private.key");

//用私钥去解密

cipher.init(Cipher.DECRYPT_MODE, privateKey);

//读数据源

byte[] src =readData("public_encryt.dat");

//得到解密后的结果

byte[] result=cipher.doFinal(src);

//二进制数据要变成字符串需解码

System.out.println(newString(result,"UTF-8"));

}

privatestaticvoidsaveData(byte[] result, String fileName)throwsException {

// TODO Auto-generated method stub

FileOutputStream fosData=newFileOutputStream(fileName);

fosData.write(result);

fosData.close();

}

publicstaticvoidsaveKey(Key key,String fileName)throwsException{

FileOutputStream fosKey=newFileOutputStream(fileName);

ObjectOutputStream oosSecretKey =newObjectOutputStream(fosKey);

oosSecretKey.writeObject(key);

oosSecretKey.close();

fosKey.close();

}

privatestaticKey readKey(String fileName)throwsException {

FileInputStream fisKey=newFileInputStream(fileName);

ObjectInputStream oisKey =newObjectInputStream(fisKey);

Key key=(Key)oisKey.readObject();

oisKey.close();

fisKey.close();

returnkey;

}

privatestaticbyte[] readData(String filename)throwsException {

FileInputStream fisDat=newFileInputStream(filename);

byte[] src=newbyte[fisDat.available()];

intlen =fisDat.read(src);

inttotal =0;

while(total

total +=len;

len=fisDat.read(src,total,src.length-total);

}

fisDat.close();

returnsrc;

}

数字签名:

数字签名的基础是公钥和私钥的非对称加密,发送者使用私钥加密的消息摘要(签名),接收者使用公钥解密消息摘要以验证签名是否是某个人。

要证明这段数据是你发过来的,并且没有被别人改过,这就需要用到数字签名,首先我们对整个文档进行md5加密得到16个字节,然后把消息摘要和文档发过去,解密者首先对发过来的文档进行解密,解密后得到一个摘要(md5),对接收的文档进行md5加密,得到的md5结果匹配解密后的摘要,如果匹配成功的话证明没有修改过,我们使用Signature进行签名

/*

* 使用私钥签名

*/

privatestaticvoidsign()throwsException {

//实例化Key

KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");

//获取一对钥匙

KeyPair keyPair=keyPairGenerator.generateKeyPair();

//获得公钥

PublicKey publicKey=keyPair.getPublic();

//获得私钥

PrivateKey privateKey=keyPair.getPrivate();

//数字签名

Signature signature =Signature.getInstance("SHA1withRSA");

signature.initSign(privateKey);//用私钥签名

signature.update("这里签名".getBytes());//对怎样的数据进行签名

byte[] sign=signature.sign();//获取签名的结果

//保存公钥并写入文件中

saveKey(publicKey,"zxx_private.key");

//将签名后的数据写入到文件

saveData(sign,"public_encryt.dat");

}

/*

* 公钥解密

*/

privatestaticvoidverify()throwsException {

Signature signture =Signature.getInstance("SHA1withRSA");

//获取到公钥

PublicKey publicKey=(PublicKey)readKey("zxx_private.key");

//初始化校验

signture.initVerify(publicKey);

//初始化签名对象

signture.update("这里签名".getBytes());

//读数据源

byte[] sign =readData("public_encryt.dat");

//返回匹配结果

booleanisYouSigned=signture.verify(sign);

//如果返回数据为true则数据没有发生修改,否则发生修改

System.out.println(isYouSigned);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值