javax.crypto.BadPaddingException:数据必须以零开头

javax.crypto.BadPaddingException:数据必须以零开头,java,security,rsa,encryption,Java,Security,Rsa,Encryption,首先,这不是一个重复的问题。我面临一个非常奇怪的问题 以下是我所做的 案例1: 生成密钥对 使用私钥加密 使用公钥解密 一切正常 案例2: 从Mozila Firefox密钥存储加载证书 使用证书A 使用证书A的私钥加密 使用证书A的公共Keu解密 一切正常 案例3: 从Internet Explorer密钥存储加载证书 使用证书A 使用证书A的私钥加密 使用证书A的公共Keu解密 在解密时,我得到一个异常错误 下面是我的每个代码的片段 生成密钥对 KeyPair keyPair = K

首先,这不是一个重复的问题。我面临一个非常奇怪的问题

以下是我所做的

案例1:

  • 生成密钥对
  • 使用私钥加密
  • 使用公钥解密
  • 一切正常

    案例2:

  • 从Mozila Firefox密钥存储加载证书
  • 使用证书A
  • 使用证书A的私钥加密
  • 使用证书A的公共Keu解密
  • 一切正常

    案例3:

  • 从Internet Explorer密钥存储加载证书
  • 使用证书A
  • 使用证书A的私钥加密
  • 使用证书A的公共Keu解密
  • 在解密时,我得到一个异常错误

    下面是我的每个代码的片段

    生成密钥对

        KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); 
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
    
    加载Mozilla密钥库

        String strCfg = System.getProperty("user.home")+ File.separator + "jdk6-nss-mozilla.cfg";
        Provider p1 = new sun.security.pkcs11.SunPKCS11(strCfg);
        Security.addProvider(p1);
        keyStore = KeyStore.getInstance("PKCS11");
        keyStore.load(null, "password".toCharArray());
    
    name=NSS
    slot=2
    library=C:/Program Files/Mozilla Firefox/softokn3.dll
    nssArgs="configDir='C:/Documents and Settings/pratik.vohera.DIGI-CORP/Application Data/Mozilla/Firefox/Profiles/t48xsipj.default' certPrefix='' keyPrefix=''     secmod='secmod.db' flags=readOnly"
    
        keyStore = KeyStore.getInstance("Windows-MY");
        keyStore.load(null, null);
    
        if (keyStore != null) {
        Enumeration<String> enumaration = null;
        try {
            enumaration = keyStore.aliases();
        } catch (KeyStoreException e1) {
            e1.printStackTrace();
        }
        ArrayList<String> certiList;
        while (enumaration.hasMoreElements()) {
            String aliases = enumaration.nextElement();
            certiList = new ArrayList<String>();
            certiList.add(aliases);
            try {
                selectedCert = keyStore.getCertificate(aliases);
                selectedpublickey = (RSAPublicKey) selectedCert.getPublicKey();
                selectedAlias = aliases;
                selectedprivateKey = (PrivateKey) keyStore.getKey(selectedAlias, null);}
            } catch (KeyStoreException e) {
                e.printStackTrace();
            }
        }
    
    javax.crypto.BadPaddingException: Data must start with zero
    at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
    at sun.security.rsa.RSAPadding.unpad(Unknown Source)
    at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at test.testclass.privateDecrypt(testclass.java:198)
    at test.testclass.test(testclass.java:137)
    at test.testclass.main(testclass.java:120)
    
    配置文件的内容

        String strCfg = System.getProperty("user.home")+ File.separator + "jdk6-nss-mozilla.cfg";
        Provider p1 = new sun.security.pkcs11.SunPKCS11(strCfg);
        Security.addProvider(p1);
        keyStore = KeyStore.getInstance("PKCS11");
        keyStore.load(null, "password".toCharArray());
    
    name=NSS
    slot=2
    library=C:/Program Files/Mozilla Firefox/softokn3.dll
    nssArgs="configDir='C:/Documents and Settings/pratik.vohera.DIGI-CORP/Application Data/Mozilla/Firefox/Profiles/t48xsipj.default' certPrefix='' keyPrefix=''     secmod='secmod.db' flags=readOnly"
    
        keyStore = KeyStore.getInstance("Windows-MY");
        keyStore.load(null, null);
    
        if (keyStore != null) {
        Enumeration<String> enumaration = null;
        try {
            enumaration = keyStore.aliases();
        } catch (KeyStoreException e1) {
            e1.printStackTrace();
        }
        ArrayList<String> certiList;
        while (enumaration.hasMoreElements()) {
            String aliases = enumaration.nextElement();
            certiList = new ArrayList<String>();
            certiList.add(aliases);
            try {
                selectedCert = keyStore.getCertificate(aliases);
                selectedpublickey = (RSAPublicKey) selectedCert.getPublicKey();
                selectedAlias = aliases;
                selectedprivateKey = (PrivateKey) keyStore.getKey(selectedAlias, null);}
            } catch (KeyStoreException e) {
                e.printStackTrace();
            }
        }
    
    javax.crypto.BadPaddingException: Data must start with zero
    at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
    at sun.security.rsa.RSAPadding.unpad(Unknown Source)
    at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at test.testclass.privateDecrypt(testclass.java:198)
    at test.testclass.test(testclass.java:137)
    at test.testclass.main(testclass.java:120)
    
    加载IE密钥库

        String strCfg = System.getProperty("user.home")+ File.separator + "jdk6-nss-mozilla.cfg";
        Provider p1 = new sun.security.pkcs11.SunPKCS11(strCfg);
        Security.addProvider(p1);
        keyStore = KeyStore.getInstance("PKCS11");
        keyStore.load(null, "password".toCharArray());
    
    name=NSS
    slot=2
    library=C:/Program Files/Mozilla Firefox/softokn3.dll
    nssArgs="configDir='C:/Documents and Settings/pratik.vohera.DIGI-CORP/Application Data/Mozilla/Firefox/Profiles/t48xsipj.default' certPrefix='' keyPrefix=''     secmod='secmod.db' flags=readOnly"
    
        keyStore = KeyStore.getInstance("Windows-MY");
        keyStore.load(null, null);
    
        if (keyStore != null) {
        Enumeration<String> enumaration = null;
        try {
            enumaration = keyStore.aliases();
        } catch (KeyStoreException e1) {
            e1.printStackTrace();
        }
        ArrayList<String> certiList;
        while (enumaration.hasMoreElements()) {
            String aliases = enumaration.nextElement();
            certiList = new ArrayList<String>();
            certiList.add(aliases);
            try {
                selectedCert = keyStore.getCertificate(aliases);
                selectedpublickey = (RSAPublicKey) selectedCert.getPublicKey();
                selectedAlias = aliases;
                selectedprivateKey = (PrivateKey) keyStore.getKey(selectedAlias, null);}
            } catch (KeyStoreException e) {
                e.printStackTrace();
            }
        }
    
    javax.crypto.BadPaddingException: Data must start with zero
    at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
    at sun.security.rsa.RSAPadding.unpad(Unknown Source)
    at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at test.testclass.privateDecrypt(testclass.java:198)
    at test.testclass.test(testclass.java:137)
    at test.testclass.main(testclass.java:120)
    
    从密钥库获取公钥和私钥

        String strCfg = System.getProperty("user.home")+ File.separator + "jdk6-nss-mozilla.cfg";
        Provider p1 = new sun.security.pkcs11.SunPKCS11(strCfg);
        Security.addProvider(p1);
        keyStore = KeyStore.getInstance("PKCS11");
        keyStore.load(null, "password".toCharArray());
    
    name=NSS
    slot=2
    library=C:/Program Files/Mozilla Firefox/softokn3.dll
    nssArgs="configDir='C:/Documents and Settings/pratik.vohera.DIGI-CORP/Application Data/Mozilla/Firefox/Profiles/t48xsipj.default' certPrefix='' keyPrefix=''     secmod='secmod.db' flags=readOnly"
    
        keyStore = KeyStore.getInstance("Windows-MY");
        keyStore.load(null, null);
    
        if (keyStore != null) {
        Enumeration<String> enumaration = null;
        try {
            enumaration = keyStore.aliases();
        } catch (KeyStoreException e1) {
            e1.printStackTrace();
        }
        ArrayList<String> certiList;
        while (enumaration.hasMoreElements()) {
            String aliases = enumaration.nextElement();
            certiList = new ArrayList<String>();
            certiList.add(aliases);
            try {
                selectedCert = keyStore.getCertificate(aliases);
                selectedpublickey = (RSAPublicKey) selectedCert.getPublicKey();
                selectedAlias = aliases;
                selectedprivateKey = (PrivateKey) keyStore.getKey(selectedAlias, null);}
            } catch (KeyStoreException e) {
                e.printStackTrace();
            }
        }
    
    javax.crypto.BadPaddingException: Data must start with zero
    at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
    at sun.security.rsa.RSAPadding.unpad(Unknown Source)
    at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at test.testclass.privateDecrypt(testclass.java:198)
    at test.testclass.test(testclass.java:137)
    at test.testclass.main(testclass.java:120)
    
    解密

    private static String publicEncrypt(String text, Key pubKey) throws Exception {
        BASE64Encoder bASE64Encoder = new BASE64Encoder();
        byte[] plainText = text.getBytes();
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    String encryptedText = bASE64Encoder.encode(cipher.doFinal(plainText));
    return encryptedText;
    }
    
    private static String privateDecrypt(String text, Key priKey)throws Exception     {
        BASE64Decoder base64Decoder = new BASE64Decoder();
    byte[] encryptText = base64Decoder.decodeBuffer(text);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, priKey);
    String decryptedString = new String(cipher.doFinal(encryptText));
    return decryptedString;
    }
    
    异常堆栈跟踪

        String strCfg = System.getProperty("user.home")+ File.separator + "jdk6-nss-mozilla.cfg";
        Provider p1 = new sun.security.pkcs11.SunPKCS11(strCfg);
        Security.addProvider(p1);
        keyStore = KeyStore.getInstance("PKCS11");
        keyStore.load(null, "password".toCharArray());
    
    name=NSS
    slot=2
    library=C:/Program Files/Mozilla Firefox/softokn3.dll
    nssArgs="configDir='C:/Documents and Settings/pratik.vohera.DIGI-CORP/Application Data/Mozilla/Firefox/Profiles/t48xsipj.default' certPrefix='' keyPrefix=''     secmod='secmod.db' flags=readOnly"
    
        keyStore = KeyStore.getInstance("Windows-MY");
        keyStore.load(null, null);
    
        if (keyStore != null) {
        Enumeration<String> enumaration = null;
        try {
            enumaration = keyStore.aliases();
        } catch (KeyStoreException e1) {
            e1.printStackTrace();
        }
        ArrayList<String> certiList;
        while (enumaration.hasMoreElements()) {
            String aliases = enumaration.nextElement();
            certiList = new ArrayList<String>();
            certiList.add(aliases);
            try {
                selectedCert = keyStore.getCertificate(aliases);
                selectedpublickey = (RSAPublicKey) selectedCert.getPublicKey();
                selectedAlias = aliases;
                selectedprivateKey = (PrivateKey) keyStore.getKey(selectedAlias, null);}
            } catch (KeyStoreException e) {
                e.printStackTrace();
            }
        }
    
    javax.crypto.BadPaddingException: Data must start with zero
    at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
    at sun.security.rsa.RSAPadding.unpad(Unknown Source)
    at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
    at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
    at javax.crypto.Cipher.doFinal(DashoA13*..)
    at test.testclass.privateDecrypt(testclass.java:198)
    at test.testclass.test(testclass.java:137)
    at test.testclass.main(testclass.java:120)
    

    我已经为此工作了很长时间。这是非常重要的。如果需要进一步的信息,请务必告诉我。

    在第三种情况下,问题是:您试图使用私钥加密,而使用公钥解密,这是错误的。您应该始终使用私钥进行解密。

    在第三种情况下,问题是:您尝试使用私钥进行加密,而使用公钥进行解密,这是错误的。您应该始终使用私钥进行解密。

    您不使用私钥进行加密。您使用某人的公钥加密消息,而他们使用私钥解密消息。这样私钥就可以保持私密性。您可以使用私钥对消息进行签名,但这与您的代码所做的不同。你想干什么?最后,在案例3中,看起来您说您使用一个证书进行加密,另一个证书进行解密。这是否准确?如果是,原因是什么?您能给我们一个示例,说明加密函数的输出是什么样子的吗?几乎可以肯定,您没有使用属于同一对的公钥/私钥。@imichaelmiers我使用私钥加密对数据进行签名。在我的申请中,我们有一个要求,即我们必须在开标时匹配我们要求的标志。其次,在案例3中,我使用同一证书的私钥进行加密和公钥解密。如果我使用的证书与案例1和案例2中的证书不同,那么我应该失败。@KeithRandall,我使用的是属于同一对的公钥/私钥,这就是为什么它在案例1和案例2中有效。您不使用私钥加密。您使用某人的公钥加密消息,而他们使用私钥解密消息。这样私钥就可以保持私密性。您可以使用私钥对消息进行签名,但这与您的代码所做的不同。你想干什么?最后,在案例3中,看起来您说您使用一个证书进行加密,另一个证书进行解密。这是否准确?如果是,原因是什么?您能给我们一个示例,说明加密函数的输出是什么样子的吗?几乎可以肯定,您没有使用属于同一对的公钥/私钥。@imichaelmiers我使用私钥加密对数据进行签名。在我的申请中,我们有一个要求,即我们必须在开标时匹配我们要求的标志。其次,在案例3中,我使用同一证书的私钥进行加密和公钥解密。如果我使用的证书与案例1和案例2中失败的证书不同。@KeithRandall,我使用的是属于同一对的公钥/私钥,这就是为什么它在案例1和案例2中有效。