Java中常用加密/解密方法详解
程序员文章站
2024-03-07 13:30:33
安全问题已经成为一个越来越重要的问题,在java中如何对重要数据进行加密解密是本文的主要内容。
一、常用的加密/解密算法
1.base64
严格来说base64并不是...
安全问题已经成为一个越来越重要的问题,在java中如何对重要数据进行加密解密是本文的主要内容。
一、常用的加密/解密算法
1.base64
严格来说base64并不是一种加密/解密算法,而是一种编码方式。base64不生成密钥,通过base64编码后的密文就可以直接“翻译”为明文,但是可以通过向明文中添加混淆字符来达到加密的效果。
2.des
des是一种基于56位密钥的对称算法,1976年被美国联邦*的国家标准局确定为联邦资料处理标准(fips),随后在国际上广泛流传开来。现在des已经不是一种安全的加密算法,已被公开破解,现在des已经被高级加密标准(aes)所代替。
3.3des
3des是des的一种派生算法,主要提升了des的一些实用所需的安全性。
4.aes
aes是现在对称加密算法中最流行的算法之一。
二、实现所需的一些库
为了实现上述的算法,我们可以实用jdk自带的实现,也可以使用一些开源的第三方库,例如bouncy castle()和comnons codec()。
三、具体实现
1.base64
package com.tancky.security; import java.io.ioexception; import sun.misc.base64decoder; import sun.misc.base64encoder; public class base64demo { private static string src = "testbase64"; public static void main(string[] args) { base64demo.jdkbase64(); base64demo.commonscodecbase64 (); base64demo.bouncycastlebase64 (); } //使用jdk的base64实现, public static void jdkbase64 (){ base64encoder encoder = new base64encoder(); string encode = encoder.encode(base64demo.src.getbytes()); system.out.println("encode: " + encode); base64decoder decoder = new base64decoder(); try { string decode = new string ( decoder.decodebuffer(encode)); system.out.println("decode: " + decode); } catch (ioexception e) { e.printstacktrace(); } } //使用apache的commonscodec实现 public static void commonscodecbase64 (){ byte[] encodebytes = org.apache.commons.codec.binary.base64.encodebase64(base64demo.src.getbytes()); string encode = new string (encodebytes); system.out.println("encode: " + encode); byte[] decodebytes = org.apache.commons.codec.binary.base64.decodebase64(encode); string decode = new string(decodebytes); system.out.println("decode: " + decode); } //使用bouncycastlede实现 public static void bouncycastlebase64 () { byte[] encodebytes = org.bouncycastle.util.encoders.base64.encode(base64demo.src.getbytes()) ; string encode = new string (encodebytes); system.out.println("encode: " + encode); byte[] decodebytes = org.bouncycastle.util.encoders.base64.decode(encode); string decode = new string(decodebytes); system.out.println("decode: " + decode); } }
2.des
package com.tancky.security; import java.security.invalidkeyexception; import java.security.key; import java.security.nosuchalgorithmexception; import java.security.nosuchproviderexception; import java.security.security; import java.security.spec.invalidkeyspecexception; import javax.crypto.badpaddingexception; import javax.crypto.cipher; import javax.crypto.illegalblocksizeexception; import javax.crypto.keygenerator; import javax.crypto.nosuchpaddingexception; import javax.crypto.secretkey; import javax.crypto.secretkeyfactory; import javax.crypto.spec.deskeyspec; import org.bouncycastle.jce.provider.bouncycastleprovider; import org.bouncycastle.util.encoders.hex; public class desdemo { private static string src = "testdes"; public static void jdkdes () { try { //生成密钥key keygenerator keygenerator = keygenerator.getinstance("des"); keygenerator.init(56); secretkey secretkey = keygenerator.generatekey(); byte[] byteskey = secretkey.getencoded(); //key转换 deskeyspec desedekeyspec = new deskeyspec(byteskey); secretkeyfactory factory = secretkeyfactory.getinstance("des"); key convertsecretkey = factory.generatesecret(desedekeyspec); //加密 cipher cipher = cipher.getinstance("des/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, convertsecretkey); byte[] encoderesult = cipher.dofinal(desdemo.src.getbytes()); system.out.println("desencode :" + hex.tohexstring(encoderesult)); //解密 cipher.init(cipher.decrypt_mode,convertsecretkey); byte[] decoderesult = cipher.dofinal(encoderesult); system.out.println("desdncode :" + new string (decoderesult)); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (invalidkeyspecexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (illegalblocksizeexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (badpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void bcdes (){ try { //使用bouncycastle 的des加密 security.addprovider(new bouncycastleprovider()); //生成密钥key keygenerator keygenerator = keygenerator.getinstance("des","bc"); keygenerator.init(56); secretkey secretkey = keygenerator.generatekey(); byte[] byteskey = secretkey.getencoded(); //key转换 deskeyspec desedekeyspec = new deskeyspec(byteskey); secretkeyfactory factory = secretkeyfactory.getinstance("des"); key convertsecretkey = factory.generatesecret(desedekeyspec); //加密 cipher cipher = cipher.getinstance("des/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, convertsecretkey); byte[] encoderesult = cipher.dofinal(desdemo.src.getbytes()); system.out.println("desencode :" + hex.tohexstring(encoderesult)); //解密 cipher.init(cipher.decrypt_mode,convertsecretkey); byte[] decoderesult = cipher.dofinal(encoderesult); system.out.println("desdncode :" + new string (decoderesult)); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (invalidkeyspecexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (illegalblocksizeexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (badpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchproviderexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void main(string[] args) { desdemo.jdkdes (); desdemo.bcdes(); } }
3.3des
package com.tancky.security; import java.security.invalidkeyexception; import java.security.key; import java.security.nosuchalgorithmexception; import java.security.nosuchproviderexception; import java.security.security; import java.security.spec.invalidkeyspecexception; import javax.crypto.badpaddingexception; import javax.crypto.cipher; import javax.crypto.illegalblocksizeexception; import javax.crypto.keygenerator; import javax.crypto.nosuchpaddingexception; import javax.crypto.secretkey; import javax.crypto.secretkeyfactory; import javax.crypto.spec.desedekeyspec; import org.bouncycastle.jce.provider.bouncycastleprovider; import org.bouncycastle.util.encoders.hex; public class tripledesdemo { private static string src = "testtripledes"; public static void jdktripledes () { try { //生成密钥key keygenerator keygenerator = keygenerator.getinstance("desede"); keygenerator.init(168); secretkey secretkey = keygenerator.generatekey(); byte[] byteskey = secretkey.getencoded(); //key转换 desedekeyspec desedekeyspec = new desedekeyspec(byteskey); secretkeyfactory factory = secretkeyfactory.getinstance("desede"); key convertsecretkey = factory.generatesecret(desedekeyspec); //加密 cipher cipher = cipher.getinstance("desede/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, convertsecretkey); byte[] encoderesult = cipher.dofinal(tripledesdemo.src.getbytes()); system.out.println("tripledesencode :" + hex.tohexstring(encoderesult)); //解密 cipher.init(cipher.decrypt_mode,convertsecretkey); byte[] decoderesult = cipher.dofinal(encoderesult); system.out.println("tripledesdncode :" + new string (decoderesult)); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (invalidkeyspecexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (illegalblocksizeexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (badpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void bctripledes () { try { security.addprovider(new bouncycastleprovider()); //生成密钥key keygenerator keygenerator = keygenerator.getinstance("desede","bc"); keygenerator.getprovider(); keygenerator.init(168); secretkey secretkey = keygenerator.generatekey(); byte[] byteskey = secretkey.getencoded(); //key转换 desedekeyspec desedekeyspec = new desedekeyspec(byteskey); secretkeyfactory factory = secretkeyfactory.getinstance("desede"); key convertsecretkey = factory.generatesecret(desedekeyspec); //加密 cipher cipher = cipher.getinstance("desede/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, convertsecretkey); byte[] encoderesult = cipher.dofinal(tripledesdemo.src.getbytes()); system.out.println("tripledesencode :" + hex.tohexstring(encoderesult)); //解密 cipher.init(cipher.decrypt_mode,convertsecretkey); byte[] decoderesult = cipher.dofinal(encoderesult); system.out.println("tripledesdncode :" + new string (decoderesult)); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (invalidkeyspecexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (illegalblocksizeexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (badpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchproviderexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void main(string[] args) { jdktripledes (); bctripledes (); } }
4.aes
package com.tancky.security; import java.security.invalidkeyexception; import java.security.key; import java.security.nosuchalgorithmexception; import java.security.nosuchproviderexception; import java.security.securerandom; import java.security.security; import javax.crypto.badpaddingexception; import javax.crypto.cipher; import javax.crypto.illegalblocksizeexception; import javax.crypto.keygenerator; import javax.crypto.nosuchpaddingexception; import javax.crypto.secretkey; import javax.crypto.spec.secretkeyspec; import org.bouncycastle.jce.provider.bouncycastleprovider; import org.bouncycastle.util.encoders.hex; public class aesdemo { private static string src = "testaes"; public static void jdkaes (){ try { //生成key keygenerator keygenerator = keygenerator.getinstance("aes"); keygenerator.init(128); //keygenerator.init(128, new securerandom("seedseedseed".getbytes())); //使用上面这种初始化方法可以特定种子来生成密钥,这样加密后的密文是唯一固定的。 secretkey secretkey = keygenerator.generatekey(); byte[] keybytes = secretkey.getencoded(); //key转换 key key = new secretkeyspec(keybytes, "aes"); //加密 cipher cipher = cipher.getinstance("aes/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, key); byte[] encoderesult = cipher.dofinal(aesdemo.src.getbytes()); system.out.println("aesencode : " + hex.tohexstring(encoderesult) ); //解密 cipher.init(cipher.decrypt_mode, key); byte[] decoderesult = cipher.dofinal(encoderesult); system.out.println("aesdecode : " + new string (decoderesult)); } catch (nosuchalgorithmexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (invalidkeyexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (illegalblocksizeexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (badpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void bcaes (){ try { //使用bouncycastle 的des加密 security.addprovider(new bouncycastleprovider()); //生成key keygenerator keygenerator = keygenerator.getinstance("aes","bc"); keygenerator.getprovider(); keygenerator.init(128); secretkey secretkey = keygenerator.generatekey(); byte[] keybytes = secretkey.getencoded(); //key转换 key key = new secretkeyspec(keybytes, "aes"); //加密 cipher cipher = cipher.getinstance("aes/ecb/pkcs5padding"); cipher.init(cipher.encrypt_mode, key); byte[] encoderesult = cipher.dofinal(aesdemo.src.getbytes()); system.out.println("aesencode : " + hex.tohexstring(encoderesult) ); //解密 cipher.init(cipher.decrypt_mode, key); byte[] decoderesult = cipher.dofinal(encoderesult); system.out.println("aesdecode : " + new string (decoderesult)); } catch (nosuchalgorithmexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (invalidkeyexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (illegalblocksizeexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (badpaddingexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } catch (nosuchproviderexception e) { // todo 自动生成的 catch 块 e.printstacktrace(); } } public static void main(string[] args) { jdkaes(); bcaes(); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
上一篇: C++ 异常小记
下一篇: Integer(自动拆箱装箱)