Java tutorial
/* * Copyright 2005-2013 iterzp.com. All rights reserved. * Support: http://www.iterzp.com * License: http://www.iterzp.com/license */ package com.iterzp.momo.utils; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.Provider; import java.security.PublicKey; import java.security.SecureRandom; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.springframework.util.Assert; /** * Utils - RSA * * @author yd14 Team * @version 3.0 */ public final class RSAUtils { /** ??? */ private static final Provider PROVIDER = new BouncyCastleProvider(); /** ? */ private static final int KEY_SIZE = 1024; /** * ?? */ private RSAUtils() { } /** * ? * * @return */ public static KeyPair generateKeyPair() { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", PROVIDER); keyPairGenerator.initialize(KEY_SIZE, new SecureRandom()); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } } /** * * * @param publicKey * * @param data * ? * @return ?? */ public static byte[] encrypt(PublicKey publicKey, byte[] data) { Assert.notNull(publicKey); Assert.notNull(data); try { Cipher cipher = Cipher.getInstance("RSA", PROVIDER); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); return null; } } /** * * * @param publicKey * * @param text * * * @return Base64? */ public static String encrypt(PublicKey publicKey, String text) { Assert.notNull(publicKey); Assert.notNull(text); byte[] data = encrypt(publicKey, text.getBytes()); return data != null ? Base64.encodeBase64String(data) : null; } /** * * * @param privateKey * ? * @param data * ? * @return ?? */ public static byte[] decrypt(PrivateKey privateKey, byte[] data) { Assert.notNull(privateKey); Assert.notNull(data); try { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", PROVIDER); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } catch (Exception e) { return null; } } /** * * * @param privateKey * ? * @param text * Base64? * @return ?? */ public static String decrypt(PrivateKey privateKey, String text) { Assert.notNull(privateKey); Assert.notNull(text); byte[] data = decrypt(privateKey, Base64.decodeBase64(text)); return data != null ? new String(data) : null; } }