Note that there are some explanatory texts on larger screens.

plurals
  1. POjavax.crypto.BadPaddingException: Data must start with zero
    text
    copied!<p>First of all, this is not a duplicate question. I am facing a very strange issue.</p> <p>Following is what I do.</p> <p><strong>Case 1:</strong></p> <ol> <li>Generate Key Pair</li> <li>Encrypt Using Private Key</li> <li>Decrypt Using Public Key</li> </ol> <p>Everything works fine.</p> <p><strong>Case 2:</strong></p> <ol> <li>Load Certificate from Mozila Firefox Key Store</li> <li>Use Certificate A</li> <li>Encrypt Using Private Key of Certificate A</li> <li>Decrypt Using Public Keu of Certificate A</li> </ol> <p>Everything works fine.</p> <p><strong>Case 3:</strong></p> <ol> <li>Load Certificate from Internet Explorer Key Store</li> <li>Use Certificate A</li> <li>Encrypt Using Private Key of Certificate A</li> <li>Decrypt Using Public Keu of Certificate A</li> </ol> <p>At Decrypt time, I get error of BadPadding exception</p> <p>Following is snippet of each of my codes.</p> <p><strong>Generating Key Pair</strong></p> <pre><code> KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); </code></pre> <p><strong>Load Mozilla KeyStore</strong></p> <pre><code> 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()); </code></pre> <p><strong>Content of Config file</strong></p> <pre><code>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" </code></pre> <p><strong>Load IE KeyStore</strong></p> <pre><code> keyStore = KeyStore.getInstance("Windows-MY"); keyStore.load(null, null); </code></pre> <p><strong>Get Public and Private key from KeyStore</strong></p> <pre><code> if (keyStore != null) { Enumeration&lt;String&gt; enumaration = null; try { enumaration = keyStore.aliases(); } catch (KeyStoreException e1) { e1.printStackTrace(); } ArrayList&lt;String&gt; certiList; while (enumaration.hasMoreElements()) { String aliases = enumaration.nextElement(); certiList = new ArrayList&lt;String&gt;(); 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(); } } </code></pre> <p><strong>Encryption</strong></p> <pre><code>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; } </code></pre> <p><strong>Decryption</strong></p> <pre><code>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; } </code></pre> <p><strong>Exception Stacktrace</strong></p> <pre><code>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) </code></pre> <p>I have been working on this for a long time. This is very important. Do let me know if any further information is required.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload