-1

my code is

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class apiKeyGenerate {
  public static void main(String[] args) throws Exception {
   // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "input".getBytes();
    byte[] ivBytes = "1234567812345678".getBytes();

    Cipher cipher =  Cipher.getInstance("DES/CBC/PKCS5Padding");
    KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
    generator.init(128);
    Key encryptionKey = generator.generateKey();
    System.out.println("key : " + new String(encryptionKey.getEncoded()));
   }
}

In above code fire Exception is : - java.security.NoSuchProviderException: that is

Exception in thread "main" java.security.NoSuchProviderException: no such provider: BC
    at sun.security.jca.GetInstance.getService(Unknown Source)
    at javax.crypto.SunJCE_b.a(DashoA13*..)
    at javax.crypto.KeyGenerator.getInstance(DashoA13*..)
    at apiKeyGenerate.main(apiKeyGenerate.java:17)

How can I do that ?? Thanks in advance

Hardik Lotiya
  • 371
  • 3
  • 9
  • 28

2 Answers2

1

In the line

KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");

the BC would indicate BouncyCastle? Why have you commented out Security.addProvider( new BouncyCastleProvider() )? You will need to add that provider in the Java security policy file then.

Have a look at the getInstance JavaDoc - the NoSuchProviderException is thrown exactly when the provider hasn't been configured. Have you tried the getInstance(...) method without the provider argument?

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • Also have a look at this SO topic: http://stackoverflow.com/questions/3711754/why-java-security-nosuchproviderexception-no-such-provider-bc – Anders R. Bystrup Oct 16 '12 at 11:59
0
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Test1 {
  public static void main(String[] args) throws Exception {
   // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

      byte[] input = "input".getBytes();
        byte[] ivBytes = "1234567812345678".getBytes();

        Cipher cipher =  Cipher.getInstance("DES/CBC/PKCS5Padding");
        **KeyGenerator generator = KeyGenerator.getInstance("AES");**
        generator.init(128);
        Key encryptionKey = generator.generateKey();
        System.out.println("key : " + new String(encryptionKey.getEncoded()));
       }

}

Instead of

KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");

this line you can try this

KeyGenerator generator = KeyGenerator.getInstance("AES");

I think this works fine.

Ami
  • 4,241
  • 6
  • 41
  • 75