使用java实现对称加密解密(AES),非对称加密解密(RSA)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用java实现对称加密解密(AES),非对称加密解密(RSA),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2779字,纯文字阅读大概需要4分钟。
内容图文
对称加密:双方采用同样的秘钥进行加密和解密。特点是速度快,但是安全性没有非对称加密高
非对称加密:接收方生成的公有秘钥公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解密 如果接收方需要返回消息给发送方,同样也可以接受发送方生成的公有秘钥,使用它加密后发送给发送方。
(对称加密和非对称加密都是可逆加密,像SHA,MD4,MD5都是不可逆加密,通常用来检验数据在传输的过程中是否被修改)
AES,RSA加密及解密代码如下:
import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.annotation.adapters.HexBinaryAdapter; public class DigestTest{ public static void main(String[] args){ try { AESEncodeTest(); RSAEncodeTest(); } catch (Exception e){ e.printStackTrace(); } } /** * AES对称加密 * @throws Exception */ private static void AESEncodeTest() throws Exception{ String content = "content"; String key = "key"; //可以将该key存入本地文件,相当于一个秘钥。这个秘钥是不会通过网络传播的。 //加密 KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(128, new SecureRandom(key.getBytes())); SecretKey secretKey = kg.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES"); Cipher enCipher = Cipher.getInstance("AES"); enCipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] result = enCipher.doFinal(content.getBytes()); System.out.println("AES加密后:" + new HexBinaryAdapter().marshal(result)); //解密 //keyGenerator可以重复使用,但是每次使用前都需要调用init方法 kg.init(128, new SecureRandom(key.getBytes())); SecretKeySpec dekeySpec = new SecretKeySpec(kg.generateKey().getEncoded(),"AES"); Cipher deCipher = Cipher.getInstance("AES"); deCipher.init(Cipher.DECRYPT_MODE, dekeySpec); System.out.println("AES解密后:" + new String(deCipher.doFinal(result))); } /** * RSA非对称加密</br> * 接收方生成的publicKey公布给发送方,发送方使用该公有秘钥加密之后,发送给接收方,然后接收方使用私有秘钥解密</br> * 如果接收方需要返回消息给发送方,同样也可以接受发送方生成的公有key,使用它加密后发送给发送方 */privatestaticvoid RSAEncodeTest() throws Exception{ String content = "content"; KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); //这儿模拟接收方生成了公、私有key。这些key可以存到文件里,要使用的时候读取出来即可 Key privateKey = keyPair.getPrivate(); Key publicKey = keyPair.getPublic(); //这儿模拟发送方已经获取了接收方给的publicKey,并且使用它来加密要发送的数据 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(cipher.ENCRYPT_MODE, publicKey); byte[] result = cipher.doFinal(content.getBytes()); System.out.println("RSA加密后:" + new HexBinaryAdapter().marshal(result)); //这儿模拟接收方使用自己生成的私钥进行解密 Cipher deCipher = Cipher.getInstance("RSA"); deCipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] deResult = deCipher.doFinal(result); System.out.println("RSA解密后:" + new String(deResult)); } }
原文:http://www.cnblogs.com/hithlb/p/4357749.html
内容总结
以上是互联网集市为您收集整理的使用java实现对称加密解密(AES),非对称加密解密(RSA)全部内容,希望文章能够帮你解决使用java实现对称加密解密(AES),非对称加密解密(RSA)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。