Skip to content

Instantly share code, notes, and snippets.

@easternHong
Created January 13, 2015 10:31
Show Gist options
  • Save easternHong/5d591f6030a7140970f7 to your computer and use it in GitHub Desktop.
Save easternHong/5d591f6030a7140970f7 to your computer and use it in GitHub Desktop.
java中常用的加密算法使用
package com.hunt.utils;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
/**
* 2015-01-13
*
* @author cylan-hunt
*
*/
public class Encryption {
/**
*
* @author cylan-hunt MD5 return String
*
*/
private static final char HEXDIGITS[] = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
public static class EncrypMD5 {
public static String encryptS(String info)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] srcBytes = info.getBytes();
// 使用srcBytes更新摘要
md5.update(srcBytes);
// 完成哈希计算,得到result
byte[] resultBytes = md5.digest();
char str[] = new char[16 * 2];
int k = 0;
for (int i = 0; i < 16; i++) {
byte byte0 = resultBytes[i];
str[k++] = HEXDIGITS[byte0 >>> 4 & 0xf];
str[k++] = HEXDIGITS[byte0 & 0xf];
}
return new String(str);
}
/**
*
* @param info
* @return encrypt byte[]
* @throws NoSuchAlgorithmException
*/
public static byte[] encryptB(String info)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] srcBytes = info.getBytes();
// 使用srcBytes更新摘要
md5.update(srcBytes);
// 完成哈希计算,得到result
byte[] resultBytes = md5.digest();
return resultBytes;
}
}
public static class EncrypRSA {
/**
* 加密
*
* @param publicKey
* @param srcBytes
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
protected static byte[] encryptB(RSAPublicKey publicKey, byte[] srcBytes)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
if (publicKey != null) {
// Cipher负责完成加密或解密工作,基于RSA
Cipher cipher = Cipher.getInstance("RSA");
// 根据公钥,对Cipher对象进行初始化
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] resultBytes = cipher.doFinal(srcBytes);
return resultBytes;
}
return null;
}
/**
* 解密
*
* @param privateKey
* @param srcBytes
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
protected static byte[] decrypt(RSAPrivateKey privateKey,
byte[] srcBytes) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
if (privateKey != null) {
// Cipher负责完成加密或解密工作,基于RSA
Cipher cipher = Cipher.getInstance("RSA");
// 根据公钥,对Cipher对象进行初始化
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] resultBytes = cipher.doFinal(srcBytes);
return resultBytes;
}
return null;
}
/**
* @param args
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
*/
// public static void main(String[] args) throws
// NoSuchAlgorithmException,
// InvalidKeyException, NoSuchPaddingException,
// IllegalBlockSizeException, BadPaddingException {
// EncrypRSA rsa = new EncrypRSA();
// String msg = "郭德纲-精品相声";
// // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
// KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// // 初始化密钥对生成器,密钥大小为1024位
// keyPairGen.initialize(1024);
// // 生成一个密钥对,保存在keyPair中
// KeyPair keyPair = keyPairGen.generateKeyPair();
// // 得到私钥
// RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// // 得到公钥
// RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
//
// // 用公钥加密
// byte[] srcBytes = msg.getBytes();
// byte[] resultBytes = rsa.encrypt(publicKey, srcBytes);
//
// // 用私钥解密
// byte[] decBytes = rsa.decrypt(privateKey, resultBytes);
//
// System.out.println("明文是:" + msg);
// System.out.println("加密后是:" + new String(resultBytes));
// System.out.println("解密后是:" + new String(decBytes));
}
public static class EncrypSHA {
public static byte[] eccryptB(String info)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("SHA");
byte[] srcBytes = info.getBytes();
// 使用srcBytes更新摘要
md5.update(srcBytes);
// 完成哈希计算,得到result
byte[] resultBytes = md5.digest();
return resultBytes;
}
/**
*
* @param info
* @return String
* @throws NoSuchAlgorithmException
*/
public static String eccryptS(String info)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("SHA");
byte[] srcBytes = info.getBytes();
// 使用srcBytes更新摘要
md5.update(srcBytes);
// 完成哈希计算,得到result
byte[] resultBytes = md5.digest();
char str[] = new char[16 * 2];
int k = 0;
for (int i = 0; i < 16; i++) {
byte byte0 = resultBytes[i];
str[k++] = HEXDIGITS[byte0 >>> 4 & 0xf];
str[k++] = HEXDIGITS[byte0 & 0xf];
}
return new String(str);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment