对称加密使用盐
package com.agri.oauth.utils;
import com.alibaba.fastjson.JSONObject;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
public class DseCodeUtils {
public final static String ALGORITHM = "DES";
public final static String encrypt(String data, String key) {
try {
return byte2hex(encrypt(data.getBytes(), key.getBytes()));
} catch (Exception e) {
e.printStackTrace();
return "-1";
}
}
public final static String decrypt(String data, String key)
throws Exception {
return new String(decrypt(hex2byte(data.getBytes()), key.getBytes()));
}
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0) {
throw new IllegalArgumentException("长度不是偶数");
}
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp;
}
}
return hs.toUpperCase();
}
public static void main(String[] args) throws Exception {
String key = "znxd07670323-2633-459e-9395-47f2426bd9db";
JSONObject userInfoToJson = new JSONObject();
userInfoToJson.put("name", "中农_隋");
userInfoToJson.put("paperstype", "1");
userInfoToJson.put("papersnumber", "000000199803140000");
String data = userInfoToJson.toString();
System.out.println("原数据:"+data);
String encrypt = DseCodeUtils.encrypt(data, key);
System.out.println("加密后的数据:"+encrypt);
String decrypt = DseCodeUtils.decrypt("D49ABFFC26C5035E781C4016D59F486EFD20DC31CFB600A115EA32063EC86A0905B1146B5D2EAED6EC2BD672D404821A80B3AA8A16C7557CE004B35DB9075C85B5DBDCC4413841FB5126CAAE9CC81A65", key);
System.out.println("解密后的数据: " + decrypt);
}
}
非对称加密RSA算法
package com.zh.sui.utils.data;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public class RSACoder {
public static final String KEY_ALGORITHM = "RSA";
private static final int KEY_SIZE = 512;
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGenerator.initialize(KEY_SIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<String, Object>();
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return cipher.doFinal(data);
}
public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, pubKey);
return cipher.doFinal(data);
}
public static byte[] getPrivateKey(Map<String, Object> keyMap) {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return key.getEncoded();
}
public static byte[] getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return key.getEncoded();
}
}
package com.zh.sui.utils.data;
import org.apache.tomcat.util.codec.binary.Base64;
import java.util.Map;
public class RSACoderUtils {
public static void getKey() throws Exception {
Map<String, Object> keyMap = RSACoder.initKey();
byte[] publicKey = RSACoder.getPublicKey(keyMap);
byte[] privateKey = RSACoder.getPrivateKey(keyMap);
System.out.println("公钥:" + Base64.encodeBase64String(publicKey));
System.out.println("私钥:" + Base64.encodeBase64String(privateKey));
}
private static String coder(String data,String privateKey) throws Exception {
byte[] getCode = RSACoder.encryptByPrivateKey(data.getBytes(), Base64.decodeBase64(privateKey));
return Base64.encodeBase64String(getCode);
}
private static String deCoder(String data,String publicKey) throws Exception {
byte[] getDecode = RSACoder.decryptByPublicKey(Base64.decodeBase64(data), Base64.decodeBase64(publicKey));
return new String(getDecode);
}
public static void main(String[] args) throws Exception {
getKey();
String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAkeCS8gVNezDHmJJg/xTMmd8z8BgeNUpHvNGH9YDXJfrjTRRPqqE4Xm4qdl/2dcXmA78vqFrc82oE7Uzt5mCP6QIDAQABAkBeRA7xEnglOTph34WKkHg+nrvDVPKAYUXGQGhpPx7IzT1PxRKkBGpcgyHdeMlbVITmD7LP9iKrQdw8GRwYQsoZAiEA8mFE1K5W2/pA9scO/cwQ0fejXXCjcHT0vKFYitbFFg8CIQCaExNZg7muSyp909XeZCfnuQxzKH1jqPl5fXQ14q0yhwIhAMqaoCyf5p9O2wrtbgycjdrVg8tRom0YdpNO03KLAIflAiBWdzwVsUoEGWTYRoiwjM6kSYJGIkP0KQEQgCo2j2aZgwIgCzY/JcoLb9JnWQUCqpJ0v9gLMlKEmQ45Lo7++eU7+QA=";
String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJHgkvIFTXswx5iSYP8UzJnfM/AYHjVKR7zRh/WA1yX6400UT6qhOF5uKnZf9nXF5gO/L6ha3PNqBO1M7eZgj+kCAwEAAQ==";
String data = "suixiaobai";
System.out.println("原数据");
System.out.println(data);
String coder = coder(data, privateKey);
System.out.println("加密后");
System.out.println(coder);
String deCoder = deCoder(coder, publicKey);
System.out.println("解密后");
System.out.println(deCoder);
}
}
|