java通过mysql的加解密函数实现敏感字段存储
1.AES加解密工具类:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
public class AESUtils {
public static String encrypt(String password, String strKey) { try { SecretKey key = generateMySQLAESKey(strKey, "ASCII" ); Cipher cipher = Cipher.getInstance( "AES" ); cipher.init(Cipher.ENCRYPT_MODE, key); byte [] cleartext = password.getBytes( "UTF-8" ); byte [] ciphertextBytes = cipher.doFinal(cleartext); return new String(Hex.encodeHex(ciphertextBytes));
} catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null ; }
public static String decrypt(String content, String aesKey){ try { SecretKey key = generateMySQLAESKey(aesKey, "ASCII" ); Cipher cipher = Cipher.getInstance( "AES" ); cipher.init(Cipher.DECRYPT_MODE, key); byte [] cleartext = Hex.decodeHex(content.toCharArray()); byte [] ciphertextBytes = cipher.doFinal(cleartext); return new String(ciphertextBytes, "UTF-8" );
} catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (DecoderException e) { e.printStackTrace(); } return null ; }
public static SecretKeySpec generateMySQLAESKey( final String key, final String encoding) { try { final byte [] finalKey = new byte [ 16 ]; int i = 0 ; for ( byte b : key.getBytes(encoding)) finalKey[i++% 16 ] ^= b; return new SecretKeySpec(finalKey, "AES" ); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
public static String getAesKey(){ StringBuilder sb = new StringBuilder(); Random random = new Random(); for ( int i = 0 ; i < 16 ; i++){ sb.append(random.nextInt( 10 )); } return sb.toString(); }
public static void main(String[] args){ String abc = "1" ; String aeskey = "3532263592381276" ; String a1= encrypt(abc, aeskey); System.out.println( "加密后:" + a1); System.out.println( "解密后:" +decrypt(a1, aeskey)); }
} |
运行main方法结果:
加密后:62b778a8ccaa40cce4c9e4e42c693665
解密后:1
2.mysql的sql加解密:
生成16随机盐:3532263592381276
|
1 |
select concat(( SELECT CEILING(RAND()*9000000000000000+1000000000000000)), '' ); |
加密:62B778A8CCAA40CCE4C9E4E42C693665
|
1 |
SELECT (HEX(AES_ENCRYPT(1, "3532263592381276" ))) |
解密:1
|
1 |
SELECT AES_DECRYPT(UNHEX( "62B778A8CCAA40CCE4C9E4E42C693665" ), "3532263592381276" ) |
3.实现效果:
java工具类加解密和mysql的加解密效果是一样的。
知识点补充
mysql下的加密函数有如下几个
PASSWORD(): 创建一个经过加密的密码字符串,适合于插入到MySQL的安全系
统。该加密过程不可逆,和unix密码加密过程使用不同的算法。主要用于MySQL的认证系统。
ENCRYPT(,): 使用UNIX crypt()系统加密字符串,ENCRYPT()函数接收要加密的字符串和(可选的)用于加密过程的salt(一个可以唯一确定口令的字符串,就像钥匙一样),注意,windows上不支持
ENCODE(,) DECODE(,): 加密解密字符串。该函数有两个参数:被加密或解密的字符串和作为加密或解密基础的密钥。Encode结果是一个二进制字符串,以BLOB类型存储。加密成都相对比较弱
MD5() :计算字符串的MD5校验和(128位)
SHA5(): 计算字符串的SHA5校验和(160位)
以上两个函数返回的校验和是16进制的,适合与认证系统中使用的口令。
到此这篇关于Java通过MySQL的加解密函数实现敏感字段存储的文章就介绍到这了,更多相关Java敏感字段存储内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/weixin_39220472/article/details/123178397
查看更多关于Java通过MySQL的加解密函数实现敏感字段存储的详细内容...