/*
 * 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.   

    怎样将以上公钥串转化成公钥对象0093a065fbf22ba7be1c28a5a31036956fee8f2912e1f8e06c91674c3057bee666828f656befd5434a2b7d58f62637972731b45d087d23a7747e8417b3c6a2aa8b590c3ae5aafa90e97979569fadaa6f6bac556f04f897afefb9040067abf259f4d37f487fbea21a55b89be203f3cedfd45517d5dfab52096119fdfb0c48d4bbed
      

  4.   

    字符串:  feff003000300039003300610030003600350066006200660032003200620061003700620065003100630032003800610035006100330031003000330036003900350036006600650065003800660032003900310032006500310066003800650030003600630039003100360037003400630033003000350037006200650065003600360036003800320038006600360035003600620065006600640035003400330034006100320062003700640035003800660036003200360033003700390037003200370033003100620034003500640030003800370064003200330061003700370034003700650038003400310037006200330063003600610032006100610038006200350039003000630033006100650035006100610066006100390030006500390037003900370039003500360039006600610064006100610036006600360062006100630035003500360066003000340066003800390037006100660065006600620039003000340030003000360037006100620066003200350039006600340064003300370066003400380037006600620065006100320031006100350035006200380039006200650032003000330066003300630065006400660064003400350035003100370064003500640066006100620035003200300039003600310031003900660064006600620030006300340038006400340062006200650064
      

  5.   

    上面那个怎么生成RSA公钥呀