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

0 0
原创粉丝点击