http://www.cnblogs.com/JCSU/articles/2803598.html
http://www.open-open.com/lib/view/open1397274257325.html
一、代码:
package com.haha.ciper.test; import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Random; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; public class CoderTest {
private static String message;
@Before
public void before(){
message="你好吗?";
System.out.println("-------------------------------");
}
@After
public void after(){
System.out.println("-------------------------------");
}
@Test
/**
* BASE64编码格式
*/
public void test_Base64Coder(){
try {
BASE64Encoder baseEncoder=new BASE64Encoder();
BASE64Decoder baseDecoder=new BASE64Decoder();
byte[] byte1=message.getBytes();
String str2=baseEncoder.encode(byte1);
System.out.println("Base64编码后:"+str2);//5a2Z5oKf56m6
byte[] byte2 = baseDecoder.decodeBuffer(str2);
System.out.println("Base64解码后:"+new String(byte2));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
/**
* 单向加密,摘要
*/
public void test_OneWay(){
try {
//MD5
MessageDigest md5=MessageDigest.getInstance("MD5");
byte[] b = md5.digest(message.getBytes("utf-8"));
System.out.println("md5摘要后转成16进制:"+byteArrayToString(b));
//SHA
MessageDigest sha=MessageDigest.getInstance("SHA");
byte[] bsha=sha.digest(message.getBytes("utf-8"));
System.out.println("sha散列后转成16进制:"+byteArrayToString(bsha));
//HMAC
KeyGenerator keyGenerator=KeyGenerator.getInstance("HmacMD5");//HmacMD5
SecretKey secreKey = keyGenerator.generateKey();
Mac mac=Mac.getInstance(secreKey.getAlgorithm());
mac.init(secreKey);
byte[] byteMac = mac.doFinal();
System.out.println("mac散列鉴别转成16进制:"+byteArrayToString(byteMac));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
/**
* 对称加密,
* 注意:Input length must be multiple of 8
*/
public void test_sym(){
try {
//DES加密
String pass="12345678";//注意此长度应该大于8位,否则运行时出错
DESKeySpec keySpec=new DESKeySpec(pass.getBytes());
SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher=Cipher.getInstance("DES");
SecureRandom random = new SecureRandom();
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
byte[] enByte=cipher.doFinal(message.getBytes());
System.out.println("des加密后转成16进制:"+byteArrayToString(enByte));
cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
byte[] deByte=cipher.doFinal(enByte);
System.out.println("des解密后:"+new String(deByte));
//PBE加密
pass="abc";
PBEKeySpec pbeSpec=new PBEKeySpec(pass.toCharArray());
SecretKeyFactory pbeFactory=SecretKeyFactory.getInstance("PBEWITHMD5andDES");
SecretKey pbeSecretKey=pbeFactory.generateSecret(pbeSpec);
byte[] salt=new byte[8];
Random rand=new Random();
rand.nextBytes(salt);
PBEParameterSpec paramSpec=new PBEParameterSpec(salt,100);
Cipher cipherPbe=Cipher.getInstance("PBEWITHMD5andDES");
cipherPbe.init(Cipher.ENCRYPT_MODE, pbeSecretKey, paramSpec);
byte[] byteEnPBE=cipherPbe.doFinal(message.getBytes());
System.out.println("PBE加密后转成16进制:"+byteArrayToString(byteEnPBE));
cipherPbe.init(Cipher.DECRYPT_MODE, pbeSecretKey, paramSpec);
byte[] byteDePBE=cipherPbe.doFinal(byteEnPBE);
System.out.println("PBE解密后:"+new String(byteDePBE));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
/**
* 非对称加密
*/
public void test_Asym(){
try {
//RSA :据说如果明文长度大于模长度则要分组加密,此处暂不实现
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(1024);
KeyPair keyPair=keyPairGen.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
String pubStr=byteArrayToString(rsaPublicKey.getEncoded());
pubStr=new BASE64Encoder().encode(rsaPublicKey.getEncoded());
System.out.println("RSA公钥:"+pubStr);
String priStr=byteArrayToString(rsaPrivateKey.getEncoded());
priStr=new BASE64Encoder().encode(rsaPrivateKey.getEncoded());
System.out.println("RSA私钥:"+priStr);
//私钥加密-公钥解密
String modules=rsaPublicKey.getModulus().toString();
String public_exponent=rsaPublicKey.getPublicExponent().toString();
BigInteger bipub=new BigInteger(public_exponent);
String private_exponent=rsaPrivateKey.getPrivateExponent().toString();
BigInteger bim=new BigInteger(modules);
BigInteger bipri=new BigInteger(private_exponent);
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
Cipher cipher=Cipher.getInstance("RSA");
RSAPrivateKeySpec privateSpec=new RSAPrivateKeySpec(bim,bipri);
Key privateKey=(RSAPrivateKey) keyFactory.generatePrivate(privateSpec);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encdata=cipher.doFinal(message.getBytes());
System.out.println("私钥加密后的数据为:"+byteArrayToString(encdata));
RSAPublicKeySpec publicSpec=new RSAPublicKeySpec(bim,bipub);
Key publicKey=(RSAPublicKey) keyFactory.generatePublic(publicSpec);
cipher.init(Cipher.DECRYPT_MODE,publicKey);
byte[] decdata=cipher.doFinal(encdata);
System.out.println("公钥解密后的数据为:"+new String(decdata));
//公钥加密-私钥解密
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] pubEnc=cipher.doFinal("我很好,你呢?".getBytes());
System.out.println("公钥加密后的数据为:"+byteArrayToString(pubEnc));
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] priDec=cipher.doFinal(pubEnc);
System.out.println("私钥解密后的数据为:"+new String(priDec,"UTF-8"));
//数字签名:私钥签名——公钥验证
PKCS8EncodedKeySpec pkcs8EnSpec=new PKCS8EncodedKeySpec(new BASE64Decoder().decodeBuffer(priStr));
PrivateKey signKey=keyFactory.generatePrivate(pkcs8EnSpec);
Signature signature=Signature.getInstance("MD5withRSA");
signature.initSign(signKey);
signature.update(encdata);
byte[] signdata=signature.sign();
System.out.println("签名后:"+byteArrayToString(signdata));
X509EncodedKeySpec x509EncSpec=new X509EncodedKeySpec(new BASE64Decoder().decodeBuffer(pubStr));
PublicKey veryKey=keyFactory.generatePublic(x509EncSpec);
signature.initVerify(veryKey);
signature.update(encdata);
Boolean isValid=signature.verify(signdata);
System.out.println("验证签名:"+isValid);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 该方法为二进制转16进制
* 可以用new BASE64Encoder().encode(byte)替换,然后用new BASE64Decoder().decodeBuffer(str)逆转
* @param arrays
* @return
*/
public String byteArrayToString(byte[] arrays){
StringBuffer sb=new StringBuffer();
for(int i=0;i<arrays.length;i++){
String str=Integer.toHexString(0xFF&arrays[i]);
if(1==str.length()){
sb.append("0").append(str);
}else{
sb.append(str);
}
// sb.append(",");
}
return sb.toString();
} }
二、结果:
-------------------------------
Base64编码后:5L2g5aW95ZCX77yf
Base64解码后:你好吗?
-------------------------------
-------------------------------
md5摘要后转成16进制:bb0b6bc45375143826f72439e050743e
sha散列后转成16进制:45ae21511874936d98a8f48d00dd7496ce650094
mac散列鉴别转成16进制:12238759ada6954dcea7bad8cb756e40
-------------------------------
-------------------------------
des加密后转成16进制:316d7895d0e3822fd3511480a4ac0f28
des解密后:你好吗?
PBE加密后转成16进制:3d97bceeda52b23726a7cf683dd71f04
PBE解密后:你好吗?
-------------------------------
-------------------------------
RSA公钥:MIGfMA0GCSqGSIb3DQE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027a1d3fc928a51d92e6a8141cd2ef234c5fe01b155bb0c927dc4357f1165101df339dc603b26d12375b67bd42a125c15605f135aea7b3098cb0c3e2a045ed5d4635340b762af0488334309460d05cb14f33d280a28158fbcdfec2fa1360d057c9ffe52529459fb054e0a003648e97bda688efb7001e5a5f48735c8a
公钥解密后的数据为:你好吗?
公钥加密后的数据为:40ef781411a6cb1147e987db924de00636cac4092ce178003c6c45d91f1d7aa3f929d3cfcfeb18d97a435eb8deff4b00e9924f049af92082b26ffc4cc6259c49a6661599bb50cf0e05da7fdd103074a7225c8a30225773fec0d6a7c9fac5108e41e2d2828c7e54617ea802d6598699e58aca068d874feb0be920aded79000f8a
私钥解密后的数据为:我很好,你呢?
签名后:8d30a274d4f9ebea1b6e7aacfff8cdfc4938516eb0658b953dab845d7d3c7505126823c56844320b7fd251d47e1f2bb7a9ee9233867e4f12a1237a61151526f23a60ebbf6fdbc2f6e9b3406e1ed9039b61e67e637874b765f150257edeaf3afe85a563d912cd30dcc06c7a38bf8987fc6f023077cc64bdf520e7f50a72cd2ec6
验证签名:true
-------------------------------