Java中有哪些加密与解密的方法-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

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(https://www.bouncycastle.org/)和comnons codec(https://commons.apache.org/proper/commons-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);
}
} 本文名称:Java中有哪些加密与解密的方法-创新互联
网页地址:http://www.scyingshan.cn/article/cdsgce.html


咨询
建站咨询
