import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class Keypair {
public static void main(String[] args) {
String s="121313qwqee";
try {
KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
KeyPair kp=kpg.generateKeyPair();
kpg.initialize(1024);
PrivateKey pri=kp.getPrivate(); //生成私钥
PublicKey pub=kp.getPublic(); //生成公钥
//---------------------------------------------------------------------
try {
Cipher cipher=Cipher.getInstance("RSA"); //静态方法生成实例
cipher.init(Cipher.PUBLIC_KEY,pub); //实例初始化
byte[] enout=cipher.doFinal(s.getBytes()); //文本加密
//--------------------------------------------------------------------
cipher.init(Cipher.PRIVATE_KEY,pri); //实例初始化
byte[] deout=cipher.doFinal(enout); //解密
for(byte k:deout) {
System.out.print((char)k); //121313qwqee
}
//-----------------------------------------------------------------------
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
?JDK文档的标准使用步骤:
Cipher cipher=Cipher.getInstance("算法“):
GCMParameterSpec? ? s.....? ? ? ? 我理解s就是密钥
? ? ? ? cipher.init(mode,s)? ? ? ? ? mode为加解密模式
? ? ? ? cipher.updateAADC...
? ? ? ? cipher.update......
? ? ? ?cipher.doFinal(被加解密文本)? ?必须是 byte[]
此程序经验证,只能公钥加密,私钥解密,或者私钥加密,公钥解密。
Cipher cipher=Cipher.getInstance("RSA"); ? //静态方法生成实例 ?? ??? ??? ??? ??? ?cipher.init(Cipher.PUBLIC_KEY,pri); ? ? ? //实例初始化 ?? ??? ??? ??? ??? ?byte[] enout=cipher.doFinal(s.getBytes()); //文本加密 ?? ??? ??? ? ? ? ? ? cipher.init(Cipher.PRIVATE_KEY,pub); ? ? ? //实例初始化
以上几句是用私钥加密,公钥解密。
Cipher.PUBLIC_KEY? ?:加密模式? ? ? ? ? ? ? ? ? ? ? Cipher.PRIVATE_KEY? :解密模式
打开byte[] enout? ?是一堆乱码,证明已加密。
生成的公钥私钥为文本形式的十六进制(去除了0x)表示的一长串数字。它没有使用byte 数组,就不用Base64 转码了。我们可以把它作为字符串用FileOutputStream 把它读写入存为文件。现在我们可以用RSA编写自己的加解密程序了。首先生成密钥对,对想保护的文件用私密加密,把公钥存入U盘,解密时调入公钥解密。
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class Keypair {
public static void main(String[] args) {
String s="121313qwqee";
try {
MessageDigest md=MessageDigest.getInstance("SHA-256"); //静态方法生成实例
md.update(s.getBytes()); //初始化
//MessageDigest tc1= md.clone();
byte[] tomd=md.digest(); //计算 问题现在输出的是byte数字,大于127就是负数,如用Base64转换,也是把负数转化,必须把大于127的数转换为正数,很简单,加256就可以了
String sout="";
int z;
for(int n=0;n<tomd.length;n++) {
if((z=tomd[n])<0) { //大于127的数加256
z=z+256;
}
sout=sout+Integer.toHexString(z); //转为字符串形式
}
System.out.println(sout); //cf69dee551eb3ec14e1522bfc5d8de70538c9e640154e62781f838967e73
} catch (NoSuchAlgorithmException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
System.out.println();
//------------------------------------------------------------------------
try {
KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");
KeyPair kp=kpg.generateKeyPair();
kpg.initialize(1024);
PrivateKey pri=kp.getPrivate(); //生成私钥
PublicKey pub=kp.getPublic(); //生成公钥
//---------------------------------------------------------------
KeyGenerator kg=KeyGenerator.getInstance("AES");
kg.init(128);
SecretKey key=kg.generateKey(); //生成对称用密钥
//-------------------------------------------------
try {
Cipher cipher3=Cipher.getInstance("AES");
cipher3.init(Cipher.ENCRYPT_MODE, key);
byte[] denout=cipher3.doFinal(s.getBytes()); //对称加密
//-------------------------------------------------------
cipher3.init(Cipher.DECRYPT_MODE, key);
byte[] ddeout=cipher3.doFinal(denout); //对称解密
for(byte z:ddeout) {
System.out.print((char)z);
}
System.out.println();
//----------------------------------------------------
} catch (InvalidKeyException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (NoSuchPaddingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalBlockSizeException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (BadPaddingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//---------------------------------------------------------------------
try {
Cipher cipher=Cipher.getInstance("RSA"); //静态方法生成实例
cipher.init(Cipher.PUBLIC_KEY,pri); //实例初始化
byte[] enout=cipher.doFinal(s.getBytes()); //文本加密
//--------------------------------------------------------------------
cipher.init(Cipher.PRIVATE_KEY,pub); //实例初始化
byte[] deout=cipher.doFinal(enout); //解密
for(byte k:deout) {
System.out.print((char)k); //121313qwqee
}
//-----------------------------------------------------------------------
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|