实际开发过程中,我们可以使用Mysql的Base64编码与解码来保存加密数据。
第一种
这种方式是直接编码保存,这种方式只能保证数据库不是明文保存数据,加密作用不大。
插入密文数据:
insert into tableName ('name','password') values('杨家将', to_base64('12345')) ;
查询数据
--根据明文值查询--
select from_base64(password ) from tableName where password = to_base64('12345');
--根据密文值--
select from_base64(password ) from tableName where password = 'dddfdf34ED';
第二种
第二种方式是使用mysql的aes_encrypt函数加密,aes_decrypt函数解密。这种方式解密需要密匙,可以起到加密作用。
to_base64(AES_ENCRYPT(明文值,密钥))
AES_DECRYPT(from_base64(密文或者密文字段),密匙)
插入数据
// 13702111418为明文值,qwer为密匙
insert into tableName ('name','password') values('杨家将', to_base64(AES_ENCRYPT('13702111418','qwer'))) ;
查询数据
--根据明文值查询--
select AES_DECRYPT(from_base64(password),'qwer') from tableName where password = to_base64(AES_ENCRYPT('13702111418','qwer'));
--根据密文值--
select AES_DECRYPT(from_base64(password),'qwer') from tableName where password = 'fdfet5445';
这里只介绍数据库查询的sql,保存数据和查询数据的代码业务逻辑我们下次再说。
欢迎大佬和java学习者加入扣扣群:344635699