首先需要一个加密的类,我们这里使用的是RSAUtil类:
package com.ebs.common.util;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
@Component
public class RSAUtil {
/**
* RSA最大加密明文大小
*/
private static final int MAX_ENCRYPT_BLOCK = 117;
/**
* RSA最大解密密文大小
*/
private static final int MAX_DECRYPT_BLOCK = 128;
/**
* 默认私钥
*/
private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAI27lI7b31tLw28Zcz53KkMs/QGPNy4Qm3C9PCPDeqQgUv82MtPuXZrThABlt27zwO0Lfj4oQR+T6B+F53mcMA8jNfJRGNtlei+IGKwnRNOgxkDYLdjjWt67p2oPdfLVHMtkxga3JMCpOPUtz1qvY7lv/ul3tRcDsF0nOeRn00DvAgMBAAECgYB/9XqJ/7gHlL887p09KBjfzR4e6k3HKPctMau6HiH7S7Oi9iutaubXgV8LfMm/QhJL8LRpKa6h88b9rsbs5DX+ESdg02ezhFZdScgt+TIfQz7nTBLd5jx3vOZXM3oOhvoP8fn/LBnXw2iMawxgR+/RE6mzPWVJJVUnUMlIw2lGuDLzWaXgX4ViQbcjFcV2fRIDYQ6CMH0MEuJMKNtkyQHUdsW2KPsLl4UUCQQCTEWNilQPxGJpkpJmTWuugDohQkhC8e0NHX8yPQzMgTSG+2Xcj+GJq1XrHKtC6AhXJfqhok4ZJJh8fWCu5uaqjAkBQ7czhuZsko76qdtH8FiYuE7d2EqRrCscO4t6yEFNRc/8vTuVDKs2T3l1AuvY+S7woL7+SO9O9F0JuhlzUHghpAkEAkeYWKp6aonw97so8cSspxkekhAnkAJhoTk6DqTyJY+ohKCylPVIUkNfoTd3z5BRdeN/mQpjYB4mCJp5aAo5m8QJBAJvuPA+e9cPfWE18khyrldpIcPgOwqv+RWiShnqUG6Lhxd5XjYmuyT3ymlza4a+QZ07/PuFs6EII5kPW4KSrzPw=";
/**
* 默认公钥
*/
private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCNu5SO299bS8NvGXM+dypDLP0BjzcuEJtwvTwjw3qkIFL/NjLT7l2a04QAZbdu88DtC34+gfhed5nDAPIzXyURjbZXoviBisJ0TToMZA2C3Y41reu6dqD3Xy1RzLZMYGtyTAqTj1Lc9ar2O5b/7pd7UXA7BdJznkZ9NA7wIDAQAB";
/**
* 获取密钥对
*
* @return 密钥对
*/
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
return generator.generateKeyPair();
}
/**
* 获取私钥
*
* @param
* @return
*/
public static PrivateKey getPrivateKey() throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] decodedKey = Base64.decodeBase64(PRIVATE_KEY);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
return keyFactory.generatePrivate(keySpec);
}
/**
* 获取公钥
*
* @param publicKey 公钥字符串
* @return
*/
public static PublicKey getPublicKey(String publicKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
return keyFactory.generatePublic(keySpec);
}
/**
* RSA加密
*
* @param data 待加密数据
* @param publicKey 公钥
* @return
*/
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int inputLen = data.getBytes().length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
// 加密后的字符串
return new String(Base64.encodeBase64String(encryptedData));
}
/**
* RSA解密
*
* @param data 待解密数据
* @param privateKey 私钥
* @return
*/
public static String decrypt(String data, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] dataBytes = Base64.decodeBase64(data);
int inputLen = dataBytes.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_DECRYPT_BLOCK;
}
byte[] decryptedData = out.toByteArray();
out.close();
// 解密后的内容
return new String(decryptedData, "UTF-8");
}
public static void main(String[] args) {
// 生成密钥对
KeyPair keyPair = null;
try {
keyPair = getKeyPair();
//生成私钥方法
//String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
String privateKey = PRIVATE_KEY;
//生成公钥方法
//String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
String publicKey = PUBLIC_KEY;
System.out.println("私钥"+privateKey);
System.out.println("公钥"+publicKey);
// RSA加密
String data = "{\"name\":\"admin\",\"pwd\":\"123456\",\"level\":\"1\"}";
String encryptData = encrypt(data, getPublicKey(publicKey));
System.out.println("加密后内容:" + encryptData);
// RSA解密
String decryptData = decrypt(encryptData, getPrivateKey());
System.out.println("解密后内容:" + decryptData);
} catch (Exception e) {
e.printStackTrace();
}
}
}
然后,可以将我们的数据库配置文件信息中的username和password分别放入上面的mian方法中,然后将生成的加密后的数据再填写到对应的uaername和password中,比如:
其次更重要的来了然后再配置连接的xml文件中
找到上面的class路径,然后创建一个类,继承class=“”中的类重写其加载username和password,然后在方法中进行解密赋值,就可以正常连接了。
如果没有xml这个配置类,可以使用继承PropertyPlaceholderConfiurer,然后在里面加载数据库配置文件信息,进行解密修改spring-jdbc文件数据库配置加密 - java程序猿的点滴记录 - ITeye博客