java 对称加密——密钥与加密后的数据存盘方式
来源:互联网 发布:神机妙算软件介绍 编辑:程序博客网 时间:2024/04/30 05:27
http://blog.csdn.net/sdefzhpk/article/details/7569023 http://chrui.iteye.com/blog/1018746
package com.cipher.test;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
*
* @ClassName: SimpleTest2
* @Description: 简单的对称加密(二)
* @author 我夕
* @date 2012-5-15
*/
public class SimpleTest2 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//加密
simpleSecret();
//解密
simpleDecret();
}
/**
* 加密方法
* @throws Exception
*/
public static void simpleSecret()throws Exception{
//创建cipher对象实例
Cipher cipher = Cipher.getInstance("AES");
//创建key
SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
//将key保存到磁盘
keySavaData(secretKey);
//初始化
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
//加密数据
byte[] result=cipher.doFinal("hello java!".getBytes());
System.out.println("数据加密的结果:"+new String(result));
//讲加密的数据存到硬盘
FileOutputStream fileOutputStream = new FileOutputStream("simple.data");
fileOutputStream.write(result);
fileOutputStream.close();
}
/**
* 解密方法
* @throws Exception
*/
public static void simpleDecret()throws Exception{
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, keyReadData());
//从磁盘中读取数据的犯非法
byte[] result = cipher.doFinal(readData(new FileInputStream("simple.data")));
System.out.println("解密后的数据:"+new String(result));
}
/**
* 将产生的密钥写到磁盘上
* @param key
* @throws Exception
*/
public static void keySavaData(SecretKey key)throws Exception{
FileOutputStream fileOutputStream = new FileOutputStream("simple.key");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(key);
objectOutputStream.close();
fileOutputStream.close();
}
/**
* 从磁盘中读取密钥
* @throws Exception
*/
public static Key keyReadData() throws Exception{
FileInputStream fileInputStream = new FileInputStream("simple.key");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Key key = (Key)objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
return key;
}
/**
* 从磁盘中读取数据
* @param inputStream
* @throws Exception
*/
public static byte[] readData(FileInputStream inputStream) throws Exception{
ByteArrayOutputStream arrayOutputStream=new ByteArrayOutputStream();
int len=0;
byte[] data=new byte[1024];
while((len=inputStream.read(data))!=-1){
arrayOutputStream.write(data, 0, len);
}
byte[] result=arrayOutputStream.toByteArray();
arrayOutputStream.close();
inputStream.close();
return result;
}
}
package dd;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class TestRSAEncryDecry {
private static String str = "I have a friend !";
public static void main(String[] args) throws Exception {
// try {
// System.out.println("===============明文===============");
// System.out.println(str);
// System.out.println();
// byte [] bytes = str.getBytes("utf-8");
// KeyPairGenerator key = KeyPairGenerator.getInstance("RSA");
// key.initialize(1024);
// KeyPair pair = key.genKeyPair();
// PublicKey pub = pair.getPublic();
// PrivateKey pri = pair.getPrivate();
// //保存公钥
// FileOutputStream fos = new FileOutputStream("pub.dat");
// ObjectOutputStream os = new ObjectOutputStream(fos);
// os.writeObject(pub);
// //保存私钥
// FileOutputStream fos2 = new FileOutputStream("pri.dat");
// ObjectOutputStream os2 = new ObjectOutputStream(fos2);
// os2.writeObject(pri);
// // RSAPublicKey rpub = (RSAPublicKey) pair.getPublic();
// // BigInteger e = rpub.getPublicExponent();
// // BigInteger n = rpub.getModulus();
// // System.out.println("e = "+e+"\r\nn = "+n);
// // BigInteger m = new BigInteger(bytes);
// // BigInteger bi = m.modPow(e, n);
// // System.out.println("=================密文==================");
// // System.out.println(bi);
// // RSAPrivateKey rpri = (RSAPrivateKey) pair.getPrivate();
// // BigInteger e2 = rpri.getPrivateExponent();
// // BigInteger n2 = rpri.getModulus();
// // BigInteger bi2 = bi.modPow(e2, n2);
// // System.out.println("===============解密后的原文==================");
// // byte [] chars = bi2.toByteArray();
// // for (int j = 0 ; j < chars.length ; j++) {
// // System.out.print((char)chars[j]);
// // }
// } catch (NoSuchAlgorithmException e) {
// e.printStackTrace();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
simpleSecret();
}
/**
* 从磁盘中读取密钥
*
* @throws Exception
*/
public static Key keyReadData(String path) throws Exception {
FileInputStream fileInputStream = new FileInputStream(path);
ObjectInputStream objectInputStream = new ObjectInputStream(
fileInputStream);
Key key = (Key) objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
return key;
}
/**
* 加密方法
*
* @throws Exception
*/
public static void simpleSecret() throws Exception {
// 创建cipher对象实例
Cipher cipher = Cipher.getInstance("RSA");
// 初始化
cipher.init(Cipher.ENCRYPT_MODE, keyReadData("pub.dat"));
// 加密数据
byte[] result = cipher.doFinal("hello java!".getBytes());
System.out.println("数据加密的结果:" + new String(result));
simpleDecret(result);
}
/**
* 解密方法
*
* @throws Exception
*/
public static void simpleDecret(byte[] data) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, keyReadData("pri.dat"));
// 从磁盘中读取数据的犯非法
byte[] result = cipher.doFinal(data);
System.out.println("解密后的数据:" + new String(result));
}
}
- java 对称加密——密钥与加密后的数据存盘方式
- java 对称加密——密钥与加密后的数据存盘方式
- 对称加密的密钥交换
- 以DES的方式实现对称加密,并提供密钥
- 对称加密的对称密钥分发
- Java加密技术——对称加密与非对称加密
- 使用对称密钥加密数据示例
- java对称加密与非对称加密
- 对称加密与非对称加密理解和非对称加密的java例子
- java 对称加密——基于口令的加密与解密
- 加密解密---非对称密钥/对称密钥
- 算法整理——非对称密钥加密RSA数学相关与简单实现
- Java加密与解密的艺术-非对称加密
- 加密方式-非对称加密(RSA加密与签名)
- Android与服务器交互的方式中的对称加密和非对称加密
- Java与对称加密、非对称加密算法
- Android的对称加密与Base64加密
- Android的对称加密与Base64加密
- 使用反射及依赖注入实现灵活的插件模式
- 数组作数据成员(1)
- (7.1.2)数据库原理之关系规范化
- java.io.FileNotFoundException: /mnt/sdcard/xxxx.mp3
- 原型污染和猴子补丁 Prototype Pollution and Monkey-Patching
- java 对称加密——密钥与加密后的数据存盘方式
- Macbook Vim高亮
- boundingRectWithSize 的使用, 计算UILable高度, 包含Emoji及多属性string
- java快速排序
- caffe架构学习之(一)--基于google protocol buffer开源项目的深度网络定义
- 第四周 项目一-三角形类的构造函数(二)
- 山寨免费软件的阴谋!
- 终于找到了-----------让工资再涨的编程方式
- 将ppt文件如何转换成pdf文件