RSA加密解密

import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Map;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;

public class RSAUtil
{

    public RSAUtil()
    {
    }

    public static String encryptByPrivateKey(byte data[], String privKey)
        throws EncodeException
    {
        try
        {
            byte key[] = Base64.decodeBase64(privKey);
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            java.security.PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(1, privateKey);
            byte b[] = cipher.doFinal(data);
            return Base64.encodeBase64String(b);
        }
        catch(Exception e)
        {
            throw new EncodeException(e);
        }
    }

    public static String encryptByPublicKey(byte data[], String publicKey)
        throws EncodeException
    {
        try
        {
            byte key[] = Base64.decodeBase64(publicKey);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
            java.security.PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(1, pubKey);
            byte data2[] = cipher.doFinal(data);
            return Base64.encodeBase64String(data2);
        }
        catch(Exception e)
        {
            throw new EncodeException(e);
        }
    }

    public static byte[] decryptByPrivateKey(String content, String privKey)
        throws DecodeException
    {
        try
        {
            byte key[] = Base64.decodeBase64(privKey);
            byte data[] = Base64.decodeBase64(content);
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            java.security.PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(2, privateKey);
            return cipher.doFinal(data);
        }
        catch(Exception e)
        {
            throw new DecodeException(e);
        }
    }

    public static byte[] decryptByPublicKey(String content, String publicKey)
        throws DecodeException
    {
        try
        {
            byte key[] = Base64.decodeBase64(publicKey);
            byte data[] = Base64.decodeBase64(content);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
            java.security.PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(2, pubKey);
            return cipher.doFinal(data);
        }
        catch(Exception e)
        {
            throw new DecodeException(e);
        }
    }

    public static byte[] getPrivateKey(Map keyMap)
    {
        Key key = (Key)keyMap.get("RSAPrivateKey");
        return key.getEncoded();
    }

    public static byte[] getPublicKey(Map keyMap)
    {
        Key key = (Key)keyMap.get("RSAPublicKey");
        return key.getEncoded();
    }

    public static final String KEY_ALGORITHM = "RSA";
    private static final String PUBLIC_KEY = "RSAPublicKey";
    private static final String PRIVATE_KEY = "RSAPrivateKey";
}
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cmos.crmpfcore.util.RSAUtil;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Test {
    static String privateKey;
    static String publicKey;

    public static void main(String[] args) throws Exception {
        
        Map<String, Object> keyMap = RSACoder.initKey(); 
        publicKey = RSACoder.encryptBASE64(RSAUtil.getPublicKey(keyMap));
        privateKey = RSACoder.encryptBASE64(RSAUtil.getPrivateKey(keyMap));
        byte[] data = "123".getBytes();
        //加密
        String entryData = RSAUtil.encryptByPublicKey(data, publicKey);
        System.out.println("加密后:"+entryData);
        byte[] dydData = RSAUtil.decryptByPrivateKey(entryData, privateKey);
        System.out.println("解密后:"+new String(dydData));
    }

}

猜你喜欢

转载自www.cnblogs.com/lirenzhujiu/p/8983812.html