java实现aes和rsa加密数据_Java实现简单AES RSA加密

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值