1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java 加密 AES 对称加密算法

Java 加密 AES 对称加密算法

时间:2020-09-18 04:32:44

相关推荐

Java 加密 AES 对称加密算法

版权声明:本文为博主原创文章,未经博主允许。

【AES】

一种对称加密算法,DES的取代者。

加密相关文章见:Java 加密解密 对称加密算法 非对称加密算法 MD5 BASE64 AES RSA

【代码】

代码比较多,有一部分非本文章内容代码,具体自己看吧。

[java]view plaincopy packagecom.uikoo9.util.encrypt;importjava.math.BigInteger;importjava.security.MessageDigest;importjava.security.SecureRandom;importjavax.crypto.Cipher;importjavax.crypto.KeyGenerator;importjavax.crypto.spec.SecretKeySpec;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;importcom.uikoo9.util.QStringUtil;/***编码工具类*1.将byte[]转为各种进制的字符串*2.base64encode*3.base64decode*4.获取byte[]的md5值*5.获取字符串md5值*6.结合base64实现md5加密*7.AES加密*8.AES加密为base64code*9.AES解密*10.将base64codeAES解密*@authoruikoo9*@version0.0.7.0601*/publicclassQEncodeUtil{publicstaticvoidmain(String[]args)throwsException{Stringcontent="我爱你";System.out.println("加密前:"+content);Stringkey="123456";System.out.println("加密密钥和解密密钥:"+key);Stringencrypt=aesEncrypt(content,key);System.out.println("加密后:"+encrypt);Stringdecrypt=aesDecrypt(encrypt,key);System.out.println("解密后:"+decrypt);}/***将byte[]转为各种进制的字符串*@parambytesbyte[]*@paramradix可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制*@return转换后的字符串*/publicstaticStringbinary(byte[]bytes,intradix){returnnewBigInteger(1,bytes).toString(radix);//这里的1代表正数}/***base64encode*@parambytes待编码的byte[]*@return编码后的base64code*/publicstaticStringbase64Encode(byte[]bytes){returnnewBASE64Encoder().encode(bytes);}/***base64decode*@parambase64Code待解码的base64code*@return解码后的byte[]*@throwsException*/publicstaticbyte[]base64Decode(Stringbase64Code)throwsException{returnQStringUtil.isEmpty(base64Code)?null:newBASE64Decoder().decodeBuffer(base64Code);}/***获取byte[]的md5值*@parambytesbyte[]*@returnmd5*@throwsException*/publicstaticbyte[]md5(byte[]bytes)throwsException{MessageDigestmd=MessageDigest.getInstance("MD5");md.update(bytes);returnmd.digest();}/***获取字符串md5值*@parammsg*@returnmd5*@throwsException*/publicstaticbyte[]md5(Stringmsg)throwsException{returnQStringUtil.isEmpty(msg)?null:md5(msg.getBytes());}/***结合base64实现md5加密*@parammsg待加密字符串*@return获取md5后转为base64*@throwsException*/publicstaticStringmd5Encrypt(Stringmsg)throwsException{returnQStringUtil.isEmpty(msg)?null:base64Encode(md5(msg));}/***AES加密*@paramcontent待加密的内容*@paramencryptKey加密密钥*@return加密后的byte[]*@throwsException*/publicstaticbyte[]aesEncryptToBytes(Stringcontent,StringencryptKey)throwsException{KeyGeneratorkgen=KeyGenerator.getInstance("AES");kgen.init(128,newSecureRandom(encryptKey.getBytes()));Ciphercipher=Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE,newSecretKeySpec(kgen.generateKey().getEncoded(),"AES"));returncipher.doFinal(content.getBytes("utf-8"));}/***AES加密为base64code*@paramcontent待加密的内容*@paramencryptKey加密密钥*@return加密后的base64code*@throwsException*/publicstaticStringaesEncrypt(Stringcontent,StringencryptKey)throwsException{returnbase64Encode(aesEncryptToBytes(content,encryptKey));}/***AES解密*@paramencryptBytes待解密的byte[]*@paramdecryptKey解密密钥*@return解密后的String*@throwsException*/publicstaticStringaesDecryptByBytes(byte[]encryptBytes,StringdecryptKey)throwsException{KeyGeneratorkgen=KeyGenerator.getInstance("AES");kgen.init(128,newSecureRandom(decryptKey.getBytes()));Ciphercipher=Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE,newSecretKeySpec(kgen.generateKey().getEncoded(),"AES"));byte[]decryptBytes=cipher.doFinal(encryptBytes);returnnewString(decryptBytes);}/***将base64codeAES解密*@paramencryptStr待解密的base64code*@paramdecryptKey解密密钥*@return解密后的string*@throwsException*/publicstaticStringaesDecrypt(StringencryptStr,StringdecryptKey)throwsException{returnQStringUtil.isEmpty(encryptStr)?null:aesDecryptByBytes(base64Decode(encryptStr),decryptKey);}}

【输出】

[java]view plaincopy 加密前:我爱你加密密钥和解密密钥:123456加密后:A63fa7DjAe3yYji44BTm1g==解密后:我爱你

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。