nistelrooy41001662 nistelrooy41001662 - 1 month ago 10
Java Question

Elliptic Curve Cryptography (ECC) encrypt/decrypt using Bouncy Castle in Java

I try to write code to encrypt/decrypt string using Elliptic Curve Cryptography (ECC) algorithm with Bouncy Castle.

Exactly I want to encrypt string by Java and decrypt by Android.

I complete write code to encrypt/decrypt local in Android, but when using Java I have a problem.

Here is my Java code:

Security.addProvider(new BouncyCastleProvider());
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1");
try
{
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA","BC");
g.initialize(spec, new SecureRandom());
KeyPair keyPair = g.generateKeyPair();
privateKey = keyPair.getPrivate();
publicKey = keyPair.getPublic();
System.out.println("PublicKey:"+publicKey+"\n");
System.out.println("PrivateKey:"+privateKey+"\n");

}
catch (Exception e)
{
e.printStackTrace();
}
String origin = "This is origin string";
try {
Cipher c = Cipher.getInstance("ECIES","BC");
c.init(Cipher.ENCRYPT_MODE,publicKey);
encodeBytes = c.doFinal(origin.getBytes());

String encrypt = encodeBytes.toString();
System.out.println("Encrypt:"+ encrypt+"\n");
} catch (Exception e) {
e.printStackTrace();
}


When I run code, it has error:

java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1021)
at javax.crypto.Cipher.init(Cipher.java:1225)
at javax.crypto.Cipher.init(Cipher.java:1166)
at lam.bk.HocECC.main(HocECC.java:44)


I don't know what wrong in Java, because I have similarly encrypt/decrypt code in android (Using spongycastle API) , my code works very well:

Here is my android code:

btnGenKey.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("prime256v1");
try {
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA","SC");
g.initialize(spec, new SecureRandom());
KeyPair keyPair = g.generateKeyPair();
privateKey = keyPair.getPrivate();
publicKey = keyPair.getPublic();
Toast.makeText(MainActivity.this, "GEN KEY SUCCESS!!", Toast.LENGTH_SHORT).show();

} catch (Exception e) {
e.printStackTrace();
}
}
});
btnEncrypt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String origin = txtOrigin.getText().toString();
try {
Cipher c = Cipher.getInstance("ECIES","SC");
c.init(Cipher.ENCRYPT_MODE,publicKey);
encodeBytes = c.doFinal(origin.getBytes());
txtEncrypt.setText(Base64.encodeToString(encodeBytes,Base64.DEFAULT));
Toast.makeText(MainActivity.this, "ENCRYPT SUCCESS!!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
btnDecrypt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
byte[] decodeBytes = null;
try
{
Cipher c = Cipher.getInstance("ECIES","SC");
c.init(Cipher.DECRYPT_MODE,privateKey);
decodeBytes = c.doFinal(encodeBytes);
String deCrypt = new String(decodeBytes,"UTF-8");
txtDecrypt.setText(deCrypt);
Toast.makeText(MainActivity.this, "DECRYPT SUCCESS!!", Toast.LENGTH_SHORT).show();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});

Answer Source

I think you have some problem. You need download: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

And add to ${java.home}/jre/lib/security/.