MD5
import java.security.MessageDigest;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class MD5 {
static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch (Exception e) {
e.printStackTrace();
}
}
public static String byteToHexString(byte[] by){
StringBuffer SB = new StringBuffer();
int i = 0;
while(i < by.length){
int k = by[i];
int j = k;
if(k < 0){
j = k + 256;
}
if(j < 16){
SB.append("0");
}
SB.append(Integer.toHexString(j));
i += 1;
}
return SB.toString();
}
public static void main(String[] args) throws Exception {
String pwd = "a12345678";
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] res = md.digest(pwd.getBytes());
System.out.println(byteToHexString(res));
}
}
SHA
import java.security.MessageDigest;
import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class SHA {
static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch (Exception e) {
e.printStackTrace();
}
}
public static String byteToHexString(byte[] by){
StringBuffer SB = new StringBuffer();
int i = 0;
while(i < by.length){
int k = by[i];
int j = k;
if(k < 0){
j = k + 256;
}
if(j < 16){
SB.append("0");
}
SB.append(Integer.toHexString(j));
i += 1;
}
return SB.toString();
}
public static void main(String[] args) throws Exception {
String pwd = "a12345678";
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] res = md.digest(pwd.getBytes());
System.out.println(byteToHexString(res));
}
}
DES
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
public class DES {
public static void main(String[] args) throws Exception {
DESKeySpec desKey = new DESKeySpec("12345678".getBytes());
SecretKeyFactory key = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = key.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(1, secretKey);
byte[] res = cipher.doFinal("a12345678".getBytes());
System.out.println(new BASE64Encoder().encode(res));
}
}
DESede
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Encoder;
public class DESede {
public static void main(String[] args) throws Exception {
DESedeKeySpec desKey = new DESedeKeySpec("123456781234567812345678".getBytes());
SecretKeyFactory key = SecretKeyFactory.getInstance("DESede");
SecretKey secretKey = key.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(1, secretKey);
byte[] res = cipher.doFinal("a12345678".getBytes());
System.out.println(new BASE64Encoder().encode(res));
}
}
AES
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
public class AES {
public static void main(String[] args) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec("1234567890abcdef".getBytes(), "AES");
AlgorithmParameterSpec iv = new IvParameterSpec("1234567890abcdef".getBytes());
Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
aes.init(1, keySpec, iv);
byte[] res = aes.doFinal("a12345678".getBytes());
System.out.println(new BASE64Encoder().encode(res));
}
}
RSA
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.security.Signature;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class RSA {
static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch (Exception e) {
e.printStackTrace();
}
}
public static BASE64Decoder Base64Decoder;
public static BASE64Encoder Base64Encoder;
public static String str_pubK = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdHAt0u/N5LWnjHdUmg6G/bBI6e96mLCaRGLwuLFoBU3cs/tdDn01PrPAmVL5msfdRfLY/8HKpS8AYpuUovbThuMD7hlLQ892b4NDDLE5aad/ApsmhmCNHJ75yfqad+cQekS9k6HeG00x4FFfHbte84v1W/J2vgI+TDXEJfpbLvQIDAQAB";
public static String str_priK = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ0cC3S783ktaeMd1SaDob9sEjp73qYsJpEYvC4sWgFTdyz+10OfTU+s8CZUvmax91F8tj/wcqlLwBim5Si9tOG4wPuGUtDz3Zvg0MMsTlpp38CmyaGYI0cnvnJ+pp35xB6RL2Tod4bTTHgUV8du17zi/Vb8na+Aj5MNcQl+lsu9AgMBAAECgYBmpCvX1/L49/J1rsfSoazKopBGJScQ6UdJBB/tCozC0QTLILMCOtEHfLRoVnUOIraxRgjLPQusDxExCdgtBcNlrmdav6owCTM9kQDwM4AxnWn26slVW5mjoXurpnc6+rb+VtprSEdOhv0Kc0nTk4p26FI/aUzcsBGABFk/VowDAQJBAM5Z7PpZszGOaReBUGuDo+qeilENr/Lqyd6msNtONTx0xL/JWZzN7DCCNsdqq0nYMnENi96PSUNowIX7yuMnSYMCQQDC6RtS7VnpgzMkxnLSfM+TXvAvFXxUlLcYsL39cdjxb4kvYpZgaAhfwVmAdtOlZIAxNlkcc8TGQCxS4eXtQdG/AkAoHqfKjQIgEnYwUb4XCUMnbKer1qNs8WVKWtJr8Sq8lF9kgntf11GipM6NuxmOg/Batq2JGga+inFUG35457yNAkBAW8hx8X2m5XOVyRFQ/3VrLAYTBf0v0ItE09doQJdp4XJi+yK8Yp2hQK7VBGeOTUkm9tMl2fQ3oikAlSbe2MTxAkEAlcdr3WvEh3MWZ2q3d0agzEmKnsanLJyhLTmRT5ncqgROh3yMP6llxHJjNCBliCFmXe9g71QWlqUPXvLRNt27yw==";
static{
Base64Decoder = new BASE64Decoder();
Base64Encoder = new BASE64Encoder();
}
public static PublicKey getPublicKey(String key) throws Exception{
byte[] keyBytes = Base64Decoder.decodeBuffer(key);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
public static PrivateKey getPrivateKey(String key) throws Exception{
byte[] keyBytes = Base64Decoder.decodeBuffer(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
public static byte[] sign(byte[] data) throws Exception{
PrivateKey priK = getPrivateKey(str_priK);
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initSign(priK);
sig.update(data);
return sig.sign();
}
public static boolean verify(byte[] data, byte[] sign) throws Exception{
PublicKey pubK = getPublicKey(str_pubK);
Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(pubK);
sig.update(data);
return sig.verify(sign);
}
public static byte[] encrypt(byte[] bt_plaintext) throws Exception{
PublicKey publicKey = getPublicKey(str_pubK);
Cipher cipher = Cipher.getInstance("RSA", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bt_encrypted = cipher.doFinal(bt_plaintext);
return bt_encrypted;
}
public static byte[] decrypt(byte[] bt_encrypted) throws Exception{
PrivateKey privateKey = getPrivateKey(str_priK);
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding", "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bt_original = cipher.doFinal(bt_encrypted);
return bt_original;
}
public static String byteToHexString(byte[] by){
StringBuffer SB = new StringBuffer();
int i = 0;
while(i < by.length){
int k = by[i];
int j = k;
if(k < 0){
j = k + 256;
}
if(j < 16){
SB.append("0");
}
SB.append(Integer.toHexString(j));
i += 1;
}
return SB.toString();
}
public static void main(String[] args) throws Exception{
String str_plaintextString = "jiamisuanfa";
byte[] bt_cipher = encrypt(str_plaintextString.getBytes());
System.out.println("明文:" + str_plaintextString);
System.out.println("加密后:" + byteToHexString(bt_cipher));
byte[] bt_original = decrypt(bt_cipher);
String str_original = new String(bt_original);
System.out.println("解密结果:" + str_original);
String str = "my sign data";
byte[] signature = sign(str.getBytes());
System.out.println("原文:" + str);
System.out.println("产生签名:" + Base64Encoder.encodeBuffer(signature));
boolean status = verify(str.getBytes(), signature);
System.out.println("验证情况:" + status);
}
}