在Java的加密和解密,有两种区分。一种被称作为对称加密。就是加密者和解密者都要知道一个固定密码。加入加密者,加密密码为123456.那么解密者就必须知道这个密码,以作为解密的时候使用的密码。说点白话,就是密码要一致。就是对称加密。当然也可以不知道密码,我们日常的电子银行中的U盾,其实就是一个密码。只不过保存在制定文件种了。
不对称加密,就是我们有一个公共的加密方式,大家都是知道的并按照这个规范来我加密。但是只有一个人才可以解密。而且解密的密码是一个。这样的加密方式,我们在给银行发送数据的时候使用。因为我们发送的信息,使用了银行的统一的加密方式来加密,而且能够解密的密码,也只有银行拥有。这样就保证了我们发送的数据不会被别人恶意的截取。即使截取了他也没有办法看明白我们发送的数据。
package cn.csdn.hm;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
public class NewMyCipher {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// 對稱加密
// myDncrypt();
// 對稱解密
// myEncrpt();
// 非对称的加密
// mySecretPariEncrypt();
// 非对称解密
//mySecretPairDncrypt();
// 對稱加密 根據用戶提供的密碼,來進行加密解密,雙方只是知道密碼。 这个密码的长度只能是8的倍数这点需要注意
//otherMySecretEncrypt("12345679");
//對稱解密
//otherMySecretDncrypt("12345679");
//数字签名
//mySignatureToByte();
//验证数字签名
//mySignatureVerify();
}
@SuppressWarnings("unused")
private static void mySignatureVerify()throws Exception {
PublicKey publicKey=(PublicKey) readerKey("mySignaturePublicKey.key");
Signature signature=Signature.getInstance("MD5withRSA");
signature.initVerify(publicKey);
signature.update(readerDat("src.dat"));
boolean fag=signature.verify(readerDat("mySignatrue.dat"));
System.out.println(fag);
}
/**
* 数字签名的,是通过不对称加密实现的。首先我们要创建一个钥匙包工厂或者说代理。创建一个钥匙包,在钥匙包里面取出唯一对应的
* 一对钥匙,一个是公用钥匙,一个是私有钥匙。Signature这个类就是签名类,通过开发文档我们理解了解到,签名的算法,我们要通过
* 与创建钥匙包的书法相互匹配。
* @throws Exception
*/
@SuppressWarnings("unused")
private static void mySignatureToByte() throws Exception {
KeyPair keyPair=KeyPairGenerator.getInstance("RSA").generateKeyPair();
PublicKey publicKey=keyPair.getPublic();
PrivateKey privateKey=keyPair.getPrivate();
byte[] src="田智文".getBytes();
Signature signature=Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(src);
byte[] result=signature.sign();
System.out.println(new String(result));
writeDat(result, "mySignatrue.dat");
writeDat(src, "src.dat");
writeKey(publicKey, "mySignaturePublicKey.key");
}
/**
* 根據用戶提供的密碼,來進行加密解密,雙方只是知道密碼。
* @param passWord 这个密码的长度只能是8的倍数这点需要注意
* @throws Exception
*/
@SuppressWarnings("unused")
private static void otherMySecretEncrypt(String passWord)
throws Exception {
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = factory.generateSecret(new PBEKeySpec(passWord
.toCharArray()));
// 这行代码,没有弄的太明白。具体的作用不是分了解。只是感觉这个对象会参与加密工作
PBEParameterSpec parameterSpec = new PBEParameterSpec(new byte[] { 1,
2, 3, 4, 5, 6, 7, 8 }, 1000);
cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
byte[] result = cipher.doFinal("根据个人密码加密的测试".getBytes());
System.out.println(new String(result));
writeDat(result, "otherMySecretEncrypt.dat");
}
@SuppressWarnings("unused")
private static void otherMySecretDncrypt(String passWord)throws Exception{
Cipher cipher=Cipher.getInstance("PBEWithMD5AndDES");
SecretKey key=SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(passWord.toCharArray()));
// 这行代码,没有弄的太明白。具体的作用不是分了解。只是感觉这个对象会参与加密工作
PBEParameterSpec parameterSpec = new PBEParameterSpec(new byte[] { 1,
2, 3, 4, 5, 6, 7, 8 }, 1000);
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
byte[] result=cipher.doFinal(readerDat("otherMySecretEncrypt.dat"));
System.out.println(new String(result));
}
@SuppressWarnings("unused")
private static void mySecretPariEncrypt() throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = generator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal("测试,非对称密码绑定".getBytes());
System.out.println(new String(result));
writeDat(result, "SecretPair_Encrpt.dat");
writeKey(privateKey, "SecretPairPrivate.key");
}
@SuppressWarnings("unused")
private static void mySecretPairDncrypt() throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
PrivateKey privateKey = (PrivateKey) readerKey("SecretPairPrivate.key");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(readerDat("SecretPair_Encrpt.dat"));
System.out.println(new String(result));
}
@SuppressWarnings("unused")
private static void myEncrpt() throws Exception {
Cipher cipher = Cipher.getInstance("DES");
Key secretKey = readerKey("tianzhw.key");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(readerDat("tianzhw.dat"));
System.out.println(new String(result));
}
@SuppressWarnings("unused")
private static void myDncrypt() throws Exception {
Cipher cipher = Cipher.getInstance("DES");
KeyGenerator generator = KeyGenerator.getInstance("DES");
Key secretKey = generator.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] result = cipher.doFinal("extra rich".getBytes("gbk"));
System.out.println(new String(result));
writeDat(result, "tianzhw.dat");
writeKey(secretKey, "tianzhw.key");
}
@SuppressWarnings("unused")
private static void writeDat(byte[] src, String fileName) throws Exception {
FileOutputStream outputStream = new FileOutputStream(fileName);
outputStream.write(src);
outputStream.close();
}
private static void writeKey(Key key, String fileName) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
fileName));
oos.writeObject(key);
oos.close();
}
@SuppressWarnings("unused")
private static byte[] readerDat(String fileName) throws Exception {
File file = new File(fileName);
byte[] result = null;
if (!file.exists()) {
System.out.println("文件不存在");
return null;
} else {
result = new byte[(int) file.length()];
FileInputStream inputStream = new FileInputStream(file);
int len = inputStream.read(result);
int total = 0;
while (total < result.length) {
total += len;
inputStream.read(result, total, (result.length - total));
}
inputStream.close();
}
return result;
}
private static Key readerKey(String fileName) throws Exception {
ObjectInputStream stream = new ObjectInputStream(new FileInputStream(
fileName));
Object obj = stream.readObject();
stream.close();
return (Key) obj;
}
}