I am in process of testing out several encryption methods and running into an issue I'm hoping someone can help me with. I am getting this error:
JCE cannot authenticate the provider BC
I installed the bcprov-jdk15-138.jar file in the ext folder in the JRE and made the following entry in the java.security file:
security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider
This is the last provider in the provider list. I have another little program that tests if BC is installed and it returns "BC is installed.". Here is the provider test program:
package com.trial.encrypt;
import java.security.Security;
public class SimpleProviderTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String providerName = "BC";
if (Security.getProvider(providerName)== null)
{
System.out.println(providerName + " provider not installed");
}
else
{
System.out.println(providerName + " is installed.");
}
}
}
And here is the code that is throwing the error:
package com.trial.encrypt;
import java.security.InvalidKeyException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;
import javax.crypto.NoSuchPaddingException;
import java.security.NoSuchProviderException;
public class SimpleAESSymmetric
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
aesEncrypt();
}
public static void aesEncrypt()
{
byte[] input = new byte[] {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte)0x88, (byte)0x99, (byte)0xaa,
(byte)0xbb, (byte)0xcc, (byte)0xdd, (byte)0xee, (byte)0xff };
byte[] keyBytes = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
try
{
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
System.out.println("input text: " + Utils.toHex(input));
//encryption pass
byte[] cipherText = new byte[input.length];
cipher.init(Cipher.ENCRYPT_MODE, key);
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println("Cipher text: " + Utils.toHex(cipherText) + " bytes: " + ctLength);
}
catch(InvalidKeyException ike)
{
ike.printStackTrace();
}
catch(IllegalBlockSizeException ibse)
{
ibse.printStackTrace();
}
catch(BadPaddingException bpe)
{
bpe.printStackTrace();
}
catch(ShortBufferException sbe)
{
sbe.printStackTrace();
}
catch(NoSuchProviderException nspe)
{
nspe.printStackTrace();
}
catch(NoSuchAlgorithmException nsae)
{
nsae.printStackTrace();
}
catch(NoSuchPaddingException nspe)
{
nspe.printStackTrace();
}
}
}