Java实现简单AES RSA加密
来源:互联网 发布:非诚勿扰全灭灯的软件 编辑:程序博客网 时间:2024/06/01 07:41
package com.lutai.util;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectOutputStream;import java.security.*;/** * CipherUtil * * @author FD * @date 2016/5/21 */public class CipherUtil { private static final int KEY_SIZE = 512; public static Key getSecretKey() { try { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = new SecureRandom(); keyGenerator.init(secureRandom); Key key = keyGenerator.generateKey(); return key; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } public static String encrypt(Key key, String text) { int mode = Cipher.ENCRYPT_MODE; try { Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, key); int blockSize = cipher.getBlockSize(); byte[] inBytes = text.getBytes(); byte[] outBytes = cipher.doFinal(inBytes); return new BASE64Encoder().encode(outBytes); } catch (Exception e) { e.printStackTrace(); return ""; } } public static String decrypt(Key key, String text) { int mode = Cipher.DECRYPT_MODE; try { Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, key); int blockSize = cipher.getBlockSize(); byte[] inBytes = new BASE64Decoder().decodeBuffer(text); byte[] outBytes = cipher.doFinal(inBytes); return new String(outBytes); } catch (Exception e) { e.printStackTrace(); return ""; } } public static KeyPair getRSAKey() { try { KeyPairGenerator pairGenerator = KeyPairGenerator.getInstance("RSA"); SecureRandom secureRandom = new SecureRandom(); pairGenerator.initialize(KEY_SIZE, secureRandom); KeyPair keyPair = pairGenerator.generateKeyPair(); return keyPair; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } public static String encryptPublicKey(KeyPair keyPair, Key key, String text) { try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.WRAP_MODE, keyPair.getPublic()); byte[] keyBytes = cipher.wrap(key); byte[] inBytes = text.getBytes(); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write(keyBytes.length); System.out.println(keyBytes.length+"XXXXX"); os.write(keyBytes); os.write(inBytes); byte[] outBytes = cipher.doFinal(os.toByteArray()); return new BASE64Encoder().encode(outBytes); } catch (Exception e) { e.printStackTrace(); return ""; } } public static String decryptPublicKey(KeyPair keyPair, String text) { try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.UNWRAP_MODE, keyPair.getPrivate()); byte[] inBytes = new BASE64Decoder().decodeBuffer(text); ByteArrayInputStream is = new ByteArrayInputStream(inBytes); int length = is.read(); byte[] keyBytes = new byte[length]; is.read(keyBytes, 0, length); System.out.println(length+"XXX"); Key key = cipher.unwrap(keyBytes, "AES", Cipher.SECRET_KEY); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] outBytes = new byte[is.available()]; is.read(outBytes); return new String(outBytes); } catch (Exception e) { e.printStackTrace(); return ""; } } public static void main(String[] args) {// Key key = getSecretKey();// String text = "fengdui";// String encode = encrypt(key, text);// System.out.println(encode);// String decode = decrypt(key, encode);// System.out.println(decode); KeyPair keyPair = getRSAKey(); Key key = getSecretKey(); String text = "fengdui"; String encode = encryptPublicKey(keyPair, key, text); System.out.println(encode); String decode = decryptPublicKey(keyPair, encode); System.out.println(decode); }}
0 0
- Java实现简单AES RSA加密
- java实现RSA和AES加密(一)
- Android DES,AES,RSA加密实现
- java 加密技术AES、MD5、RSA
- java RSA和AES加密解密工具
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- Java AES加密实现
- JAVA实现AES加密
- JAVA实现AES加密
- JAVA实现AES加密
- .NET AOP(一)七种AOP实现方法
- odoo中根据模型生成viwe视图的框架代码
- 安卓开发板——网口通信
- 桥接模式
- mysql储存引擎innodb
- Java实现简单AES RSA加密
- SpringMVC的Controller方法参数
- 浅尝JMX
- FreeMarker中<#include>和<#import>标签的区别
- document.ready和window.onload的区别
- MapperScannerConfigurer处理过程源码分析(转)
- JQuery cxSelect联动下拉菜单
- NSMutableArray复杂对象排序
- android studio启动模拟器失败