InvalidKeyException DER输入不是位字符串

时间:2016-03-03 09:22:13

标签: java

import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class DJExampleDecryptCode
{

  public static void main(String[] args)
  {

    try
    {
      String encryptedText = "q3sEN1NZZyseoFy9H3WIwNf9jpGrDTTqh/AticRV2pnb1KZ5lieuK5jw3JgctgYUnTE03xnMcOL50UGKZ4dbYEt5XGCZyNVgh1qVGF7Vgnvi5PKndnpKLcoSUJCcbu/lyLI2P+Zd7ZH0/tRKRn1zqrPAWUH3VjtUt7qkIcdIYyaoHP5I7eiZRk6FL9ugUQJnz8WFgM4mcRJ5Zs/NLdaXKeHMO4nPQBTOLNaPdNxW2MM+qlv0HN/fs4rPMRGUw0xXhjWsmSNqadASfn7UX4fL79CmGyKfm8ol4njZakZbsfes/zstc5Su0swycfFSkjXAjPjvMGdBs5/HSLXYAvQPoA==";

      String TextToEncrypt = "Test";

      String decryptedString = decrypt(encryptedText);
      String encryptedString = encrypt(TextToEncrypt);

      //Printing
      System.out.println("Decrypted Text: " + decryptedString);
      System.out.println("Encrypted Text: " + encryptedString);

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  //public static String decrypt(String text, String privateKeyString) throws GeneralSecurityException
  public static String decrypt(String text) throws GeneralSecurityException
  {
    String DJPrivateKey = ".....";  
    PrivateKey privateKey = stringToPrivateKey(DJPrivateKey);

    byte[] dectyptedText = null;
    try
    {
      final Cipher cipher = Cipher.getInstance("RSA");
      cipher.init(Cipher.DECRYPT_MODE, privateKey);
      dectyptedText = cipher.doFinal(base64Decode(text));

    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }

    return new String(dectyptedText);
  }

  public static String encrypt(String data) throws GeneralSecurityException 
  {
      String PCMSPublicKey = "MIIFDjBABgkqhki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u/atc6HaPc6sfrhLF6RWyhIYKyqoM2dyLFC3hi+N0ZLBZwp/tnIou24dtwlJnLvKCinzO0pUTJC2yOwsnfL57h+ikdd3xS9fMWwpiSdNps086japrp12GU9VyBZX8b9QEip/Hxw778OK1x853+WYM978wNPrFwIfWtQpvNZMi8Mt0WXDvfHiG8JK9PKDoS25iV8SrwZScfBTMIi95j419BuhcVca1fi0keEEKaqMzBus4Mgz421Qcy0xv2u81w90qoMyXBwadRODtrJBQIovHBCKVRkxEm64Rr3fCWGjralnKcjxDKa77qakFlFOyNJsplnlC0mc229E19JXlxpcdDlivscE727KLLYu8rPUEWMZY/PA0D9JH+u+52A81ur0vuCnTLF1V0WG8ECJwgVTbIfByPoi+MKuvmW1pvixwBXiIh7as95gVl47HAXmCYOj0bnD0yO/pFSLoiAURl0j2R/c/NtKjz5TQ9F3O3U83UojatwtIyc7xN6Bs+iwTPaBOJrI6Sbgc5yDJPFczhPQDSpFZVtTUSeq1UA7N7mogwQLAqawGBHQJ9JIapMl6uC7Y2nN9O4lYRtbQLBKpZ3xzIk9LrDyT3F/w+c3l/lVUz0X6lz746zNutl/6f6XKI5oeis7/b5rMHHYYE=";
      PublicKey publicKey = stringToPublicKey(PCMSPublicKey);


      byte[] enctyptedText = null;
        try
        {
          final Cipher cipher = Cipher.getInstance("RSA");
          cipher.init(Cipher.ENCRYPT_MODE, publicKey);
          enctyptedText = cipher.doFinal(base64Decode(data));

        }
        catch (Exception ex)
        {
          ex.printStackTrace();
        }

        return new String(enctyptedText);

  }    

  public static byte[] base64Decode(String encodedString)
  {
    Base64 myBase64 = new Base64();

    return myBase64.decode(encodedString);
  }



  public static PrivateKey stringToPrivateKey(String privateKeyString) throws GeneralSecurityException
  {
    byte[] clear = base64Decode(privateKeyString);
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PrivateKey priv = fact.generatePrivate(keySpec);
    Arrays.fill(clear, (byte) 0);
    return priv;
  }

  public static PublicKey stringToPublicKey(String publicKeyString) throws GeneralSecurityException
  {

      byte[] clear = base64Decode(publicKeyString);
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(clear);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PublicKey pub = fact.generatePublic(keySpec);
    Arrays.fill(clear, (byte) 0);
    return pub;
  }

}

我有上面的java代码进行加密和解密。解密工作正常,使用私钥,但是当我使用公钥运行加密部分时我硬编码,我得到了以下错误,

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: DER input not a bit string
    at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:205)
    at java.security.KeyFactory.generatePublic(KeyFactory.java:334)
    at DJExampleDecryptCode.stringToPublicKey(DJExampleDecryptCode.java:109)
    at DJExampleDecryptCode.encrypt(DJExampleDecryptCode.java:64)
    at DJExampleDecryptCode.main(DJExampleDecryptCode.java:26)
Caused by: java.security.InvalidKeyException: IOException: DER input not a bit string
    at sun.security.x509.X509Key.decode(X509Key.java:397)
    at sun.security.x509.X509Key.decode(X509Key.java:403)
    at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:83)

任何人都可以给我一些建议吗?非常感谢

0 个答案:

没有答案