/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */package dbmpparser.msgutil;import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;import org.apache.log4j.Logger;import java.security.Key;
import java.security.KeyFactory;   
import java.security.KeyPair;   
import java.security.KeyPairGenerator;   
import java.security.NoSuchAlgorithmException;   
import java.security.PrivateKey;   
import java.security.PublicKey;   
import java.security.SecureRandom;   
import java.security.interfaces.RSAPrivateKey;   
import java.security.interfaces.RSAPublicKey;   
import java.security.spec.InvalidKeySpecException;   
import java.security.spec.PKCS8EncodedKeySpec;   
import java.security.spec.X509EncodedKeySpec;  
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.Cipher;   
import org.apache.commons.configuration.ConfigurationException;   
import org.apache.commons.configuration.PropertiesConfiguration;   
import org.bouncycastle.jce.provider.BouncyCastleProvider;  
import  java.security.*;
import java.math.BigInteger;   
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;import dbmpparser.*;/**
 *  各种静态处理方法集合
 * @author yangchenghao
 */
public class Utils {
            // 写日志文件
        static Logger logger = Logger.getLogger(Utils.class.getName());
             
     /**
     * 用公钥对进行MD5加密过的约定字符串,进行再一次加密
     * 
     * 说明:
     * @param originalString
     * @param publicKeyArray
     * @return
     * @throws Exception
     * 创建时间:2010-12-1 下午06:29:51
     */
  public static byte[] encrypt(byte[] publicKey1, byte[] data) {
      if (publicKey1 != null) {   
      try {
            byte[] keyBytes;
            //keyBytes = Base64.decode(new String(publicKey1));
             System.out.println("获取公钥之前:--"+new String(publicKey1));//"65fbf22ba7be1c28a5a31036956fee8f2912e1f8e06c91674c3057bee666828f656befd5434a2b7d58f62637972731b45d087d23a7747e8417b3c6a2aa8b590c3ae5aafa90e97979569fadaa6f6bac556f04f897afefb9040067abf259f4d37f487fbea21a55b89be203f3cedfd45517d5dfab52096119fdfb0c48d4bbed"
             //String teststr = new String(publicKey1,"UTF-16");
             //keyBytes = (new BASE64Encoder()).encodeBuffer(publicKey1);
           // String teststr = (new BASE64Encoder()).encodeBuffer(publicKey1);
             
           //  keyBytes = (new BASE64Decoder()).decodeBuffer(new String(publicKey1));            // X509EncodedKeySpec keySpec=new X509EncodedKeySpec(teststr.getBytes());
             X509EncodedKeySpec keySpec=new X509EncodedKeySpec(publicKey1);
           // PKCS8EncodedKeySpec  keySpec = new PKCS8EncodedKeySpec();
            System.out.println("编码格式:"+keySpec.getFormat());
            
             KeyFactory keyFactory=KeyFactory.getInstance("RSA");
             PublicKey publickey=keyFactory.generatePublic(keySpec);  //生成公钥时报错
               Cipher cipher = Cipher.getInstance("RSA",   
              new BouncyCastleProvider());   
             cipher.init(Cipher.ENCRYPT_MODE, publickey);   
             return cipher.doFinal(data);                 } catch (Exception e) {   
              e.printStackTrace();   
              }            
         }   
      return null;   
      } 
}报错问题:java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: insufficient data
        at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:163)
        at java.security.KeyFactory.generatePublic(KeyFactory.java:304)
        at dbmpparser.msgutil.Utils.encrypt(Utils.java:114)
        at dbmpparser.msgutil.DESCipher.main(DESCipher.java:120)
Caused by: java.security.InvalidKeyException: IOException: insufficient data
        at sun.security.x509.X509Key.decode(X509Key.java:380)
        at sun.security.x509.X509Key.decode(X509Key.java:386)
        at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:65)
        at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:256)
        at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:159)
        ... 3 more

解决方案 »

  1.   

    仅供参考 /** 
     * 初始化RSA密钥 
     *  
     */
    static{
    KeyPairGenerator keyPairGen;
    try {
    keyPairGen = KeyPairGenerator.getInstance("RSA");
    keyPairGen.initialize(1024);
    KeyPair keyPair = keyPairGen.generateKeyPair();  privateKey = (RSAPrivateKey) keyPair.getPrivate();
     publicKey = (RSAPublicKey) keyPair.getPublic();
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    }

    }
      

  2.   

    我这里是要将 提供的公钥字符串16进制 转化成publickey对象,怎么转化呢?大哥大姐门帮帮忙
      

  3.   

    public static byte[] encrypt(byte[] publicKey1, byte[] data) {
    keyBytes = Base64.decode(new String(publicKey1));
    System.out.println("获取公钥之前:--"+new String(publicKey1));
    说实话我是真的看不懂这些是在干什么!
      

  4.   

    按照你 new String 出来的字符串来还原调用参数中的 byte[] publicKey,那个 publicKey 根本就不是 RSA 的公钥。
      

  5.   

    但是我得到的string类型公钥就是这个呀0093a065fbf22ba7be1c28a5a31036956fee8f2912e1f8e06c91674c3057bee666828f656befd5434a2b7d58f62637972731b45d087d23a7747e8417b3c6a2aa8b590c3ae5aafa90e97979569fadaa6f6bac556f04f897afefb9040067abf259f4d37f487fbea21a55b89be203f3cedfd45517d5dfab52096119fdfb0c48d4bbed