0

I'm trying to learn how to encrypt data in a text field using NetBeans, and created a test app. My coding does not show any errors but when I click the "Encrypt" button, I get presented with the error message: "java.lang.SecurityException: JCE cannot authenticate the provider BC".

I got the encryption coding from the this YouTube video (https://www.youtube.com/watch?v=A41LMCEXZBo&list=PLB04B4E5D9B58C13D&index=135) and the encryption works there. The encryption coding looks as follows:

private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try
        {
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            input = txtPlain.getText().getBytes();
            SecretKeySpec key = new SecretKeySpec(keyBytes, "DES"); 
            IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
            cipher = Cipher.getInstance("DES/CTR/NoPadding", "BC");
            cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
            cipherText = new byte[cipher.getOutputSize(input.length)];
            ctLength = cipher.update(input, 0, input.length, cipherText, 0);
            ctLength += cipher.doFinal(cipherText, ctLength);
            txtEncrypt.setText(new String(cipherText));
            System.out.println("cipher: " + new String(cipherText));
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    } 

Why would my encryption not work and how would I correct this error?

PS: The exact same error message comes in the NetBeans console but with errors regarding the lines:

cipher = Cipher.getInstance("DES/CTR/NoPadding", "BC"); and

System.out.println("cipher: " + new String(cipherText));

The full error that displays in the console is:

java.lang.SecurityException: JCE cannot authenticate the provider BC
    at javax.crypto.Cipher.getInstance(Cipher.java:657) at javax.crypto.Cipher.getInstance(Cipher.java:657)
    at javax.crypto.Cipher.getInstance(Cipher.java:596)
    at PasswordFrame.btnEncryptActionPerformed(PasswordFrame.java:91)
    at PasswordFrame.access$000(PasswordFrame.java:9)
    at PasswordFrame$1.actionPerformed(PasswordFrame.java:50)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.util.jar.JarException: file:/C:/Users/Osiris/Documents/NetBeansProjects/EncryptionTest/lib/bcprov-1.45.jar has unsigned entries - org/bouncycastle/i18n/LocalizedMessage$FilteredArguments.class
    at javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:464)
    at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:322)
    at javax.crypto.JarVerifier.verify(JarVerifier.java:250)
    at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:160)
    at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:186)
    at javax.crypto.Cipher.getInstance(Cipher.java:653)
    ... 40 more
Osiris93
  • 189
  • 1
  • 15
  • 1
    Would you mind adding the exact StackTrace? – Yassin Hajaj Oct 04 '15 at 11:27
  • Pardon my ignorance but what is a "StackTrace"? Are you referring to the YouTube link? – Osiris93 Oct 04 '15 at 11:29
  • StackTrace is the message printed into the console when an exception is thrown – Yassin Hajaj Oct 04 '15 at 11:30
  • That's another thing, there is no message appearing in the console. It comes up as a message dialog box. When I take the messageDialog out of my catch statement there is still no error in the console and the app does not experience runtime errors or anything – Osiris93 Oct 04 '15 at 11:32
  • @HovercraftFullOfEels I tried your suggestion and it comes up with the exact same error message as I mentioned in my question except with a long list of the lines that are affected (mostly generated code), but I have edited my question to point out which of my lines are causing the problem – Osiris93 Oct 04 '15 at 11:40
  • 1
    I fixed it. There was something wrong with my bcprov file. I downloaded an updated version and it worked perfectly fine. – Osiris93 Oct 04 '15 at 12:05

0 Answers0