I am trying to implement ECC algorithm on Android. I am currently using spongy castle to implement it.
The key generation cone snippet is as follows :
KeyPairGenerator kpg = null;
try {
kpg = KeyPairGenerator.getInstance("ECIES");// Do i have to do any changes here?
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
ECGenParameterSpec brainpoolP160R1 = new ECGenParameterSpec("brainpoolP160R1");
try {
kpg.initialize(brainpoolP160R1);
} catch (InvalidAlgorithmParameterException) {
}
KeyPair kp = kpg.generateKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
The encrypt/decrypt code is as follows
Cipher c = null;
try {
c =Cipher.getInstance("ECIES", "SC"); //Cipher.getInstance("ECIESwithAES/DHAES/PKCS7Padding", "BC");
} catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e) {
e.printStackTrace();
}
try {
c.init(Cipher.ENCRYPT_MODE,(IESKey)publicKey , new SecureRandom());
} catch (InvalidKeyException e) {
e.printStackTrace();
}
byte[] message = "hello world -- a nice day today".getBytes();
byte[] cipher = new byte[0];
try {
cipher = c.doFinal(message,0,message.length);
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
// System.out.println("Ciphertext : "+ Base64.encode(cipher));
TextView eccencoded = (TextView) findViewById(R.id.eccencoded);
eccencoded.setText("[ENCODED]:\n" +
Base64.encodeToString(cipher, Base64.DEFAULT) + "\n");
try {
c.init(Cipher.DECRYPT_MODE,(IESKey) privateKey, new SecureRandom());
} catch (InvalidKeyException e) {
e.printStackTrace();
}
byte[] plaintext = new byte[0];
try {
plaintext = c.doFinal(cipher,0,cipher.length);
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
TextView eccdecoded = (TextView) findViewById(R.id.eccdecoded);
eccdecoded.setText("[DECODED]:\n" +
Base64.encodeToString(plaintext, Base64.DEFAULT) + "\n");
Here since i use (IESKey) casting for private and public key in c.init() i get the error
java.lang.ClassCastException:org.spongycastle.jce.provider.JCEECPublicKey cannot be cast to org.spongycastle.jce.interfaces.IESKey
and if i remove the cast i get error like this
c.init(Cipher.DECRYPT_MODE, privateKey, new SecureRandom());
i get error
java.security.InvalidKeyException: must be passed IES key
I get algorithm not found error if i use
Cipher.getInstance("ECIESwithAES/DHAES/PKCS7Padding", "SC");
I am using scprov-jdk15-1.46.99.3-UNOFFICIAL-ROBERTO-RELEASE.jar. Is this the right jar to use? If not please suggest a better one.
Also how should I rectify my code to get it working?