Java tutorial
/* * Copyright 2012-2014 sammyun.com.cn. All rights reserved. * Support: http://www.sammyun.com.cn * License: http://www.sammyun.com.cn/license */ package com.sammyun.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.security.KeyFactory; 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 java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; import com.sammyun.plugin.alipayDirect.AlipayDirectPlugin; /** * Utils - RSA * * @author Sencloud Team * @version 3.0 */ public final class RSAUtils { /** logger */ private static final Logger logger = LoggerFactory.getLogger(AlipayDirectPlugin.class); /** ??? */ 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; } public static final String SIGN_ALGORITHMS = "SHA1WithRSA"; /** * RSA?? * * @param content ??? * @param privateKey ? * @param input_charset ?? * @return ?? */ public static String sign(String content, String privateKey, String input_charset) { try { PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64Util.decode(privateKey)); KeyFactory keyf = KeyFactory.getInstance("RSA"); PrivateKey priKey = keyf.generatePrivate(priPKCS8); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initSign(priKey); signature.update(content.getBytes(input_charset)); byte[] signed = signature.sign(); return Base64Util.encode(signed); } catch (Exception e) { e.printStackTrace(); } return null; } /** * RSA?? * * @param content ??? * @param sign ?? * @param ali_public_key ? * @param input_charset ?? * @return */ public static boolean verify(String content, String sign, String ali_public_key, String input_charset) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64Util.decode(ali_public_key); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); signature.update(content.getBytes(input_charset)); boolean bverify = signature.verify(Base64Util.decode(sign)); return bverify; } catch (Exception e) { e.printStackTrace(); } return false; } /** * RSA?? * * @param content ??? * @return */ public static String encryptContent(String content, String ali_public_key) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64Util.decode(ali_public_key); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); signature.update(content.getBytes("utf-8")); byte[] signed = signature.sign(); return Base64Util.encode(signed); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return ""; } } /** * * * @param content * @param private_key ? * @param input_charset ?? * @return ? */ public static String decrypt(String content, String private_key, String input_charset) throws Exception { PrivateKey prikey = getPrivateKey(private_key); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, prikey); InputStream ins = new ByteArrayInputStream(Base64Util.decode(content)); ByteArrayOutputStream writer = new ByteArrayOutputStream(); // rsa?128?128? byte[] buf = new byte[128]; int bufl; while ((bufl = ins.read(buf)) != -1) { byte[] block = null; if (buf.length == bufl) { block = buf; } else { block = new byte[bufl]; for (int i = 0; i < bufl; i++) { block[i] = buf[i]; } } writer.write(cipher.doFinal(block)); } return new String(writer.toByteArray(), input_charset); } /** * ? * * @param key ?base64? * @throws Exception */ public static PrivateKey getPrivateKey(String key) throws Exception { byte[] keyBytes; keyBytes = Base64Util.decode(key); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; } }