【问题标题】:Load RSA public key Error DerInputStream.getLength(): lengthTag=109, too big加载 RSA 公钥错误 DerInputStream.getLength(): lengthTag=109, too big
【发布时间】:2019-12-04 22:27:39
【问题描述】:

我需要加密 JSON 数据以使用公共 RSA 密钥在 http 正文中发送它,我加载公共 RSA 密钥文件 (.der) 并且它工作正常。

客户端向我发送一个公共 RSA 密钥文件 (.bin)。所以当我运行我的程序时,我得到了这个错误

Caused by: java.security.InvalidKeyException: IOException: DerInputStream.getLength(): lengthTag=109, too big.
    at sun.security.x509.X509Key.decode(X509Key.java:380) ~[na:1.6.0_45]
    at sun.security.x509.X509Key.decode(X509Key.java:386) ~[na:1.6.0_45]
    at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:66) ~[na:1.6.0_45

我的 PublicKeyReader 类:

public class PublicKeyReader {

  public static PublicKey getpublicKey(String filename)
    throws Exception {

      File file = new File(filename);
      FileInputStream fis = new FileInputStream(filename);
      DataInputStream dis = new DataInputStream(fis);
      byte[] keyBytes = new byte[(int) file.length()];
      dis.readFully(keyBytes);
      dis.close();


    X509EncodedKeySpec spec =
      new X509EncodedKeySpec(keyBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");

    return kf.generatePublic(spec);
  }
}

我的主要课程中的加密部分是:

    // Encrypt Data with AES
        byte[] keyData = random.generateSeed(16);
        SecretKey skeySpec = new SecretKeySpec(keyData, "AES");
        Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] ivParams = new byte[aes.getBlockSize()];
        IvParameterSpec iv = new IvParameterSpec(ivParams);
        aes.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        // Lecture du certificat (cle publique RSA)
        PublicKey clePublique = PublicKeyReader.getpublicKey("./src/main/resources/publique.bin");
        //String clePublique1 = Base64.encodeBase64String(clePublique.getEncoded()).replaceAll(
        //      "(\\r|\\n)", "");
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.WRAP_MODE, clePublique);
        byte[] wrappedKey = cipher.wrap(skeySpec);

        // encodedToken : Mot de passe symétrique crypté avec le certificat
        // public (RSA) mis à la disposition de la banque par BAM
        String encodedToken = Base64.encodeBase64String(wrappedKey).replaceAll(
                "(\\r|\\n)", "");
        ;

【问题讨论】:

    标签: java spring-boot file base64 rsa


    【解决方案1】:

    经过多次研究,我找到了答案,也找到了我弄乱的代码部分。

    在我的 PublicReaderKey 类中,我更改了读取 RSA 公钥的方式

    使用这部分代码:

     public static PublicKey getpublicKey(String filename)
        throws Exception {
    
          InputStream in = new FileInputStream(filename);
            ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(
                    in));
            try {
                BigInteger m = (BigInteger) oin.readObject();
                BigInteger e = (BigInteger) oin.readObject();
                KeyFactory fact = KeyFactory.getInstance("RSA");
                    return fact.generatePublic(new RSAPublicKeySpec(m, e));
    
            } catch (Exception e) {
                throw new RuntimeException("Erreur de sérialisation parasite", e);
            } finally {
                oin.close();
                System.out.println("Fermeture de lecture fichier .");
            }
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-15
      • 1970-01-01
      • 2018-03-22
      • 2014-06-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      相关资源
      最近更新 更多