Home java.security.spec.InvalidKeySpecException: java.lang.ClassCastException at the time off doing encryption of string
Reply: 0

java.security.spec.InvalidKeySpecException: java.lang.ClassCastException at the time off doing encryption of string

rk_1
1#
rk_1 Published in 2018-02-14 04:13:39Z

I'm trying to do a encryption and decryption using ecc algorithm, but when i run this code it shows error. I have add all required jars but still it wont run. I've successfully generated a public and private key and placed them in the given path. plz help

Error consol:

java.security.spec.InvalidKeySpecException: java.lang.ClassCastException: 
org.bouncycastle.jce.provider.JDKDSAPublicKey cannot be cast to 
org.bouncycastle.jce.provider.JCEECPublicKey at 
org.bouncycastle.jce.provider.JDKKeyFactory$EC.engineGeneratePublic(Unknown 
Source) at java.security.KeyFactory.generatePublic(Unknown Source)
at Encryec.main(Encryec.java:70)

Encrypt.java

public class Encryec {

KeyPairGenerator kpg;
EllipticCurve curve;
 ECParameterSpec ecSpec;
KeyPair aKeyPair;
static KeyAgreement aKeyAgree;
KeyPair bKeyPair;
KeyAgreement bKeyAgree;
KeyFactory keyFac; 
static String msg;

public static void main(String args[])
{
 Security.addProvider(new BouncyCastleProvider());
 Scanner ss =new Scanner(System.in);

try{
    String path = "G:\\A";   
 File filePublicKey = new File(path+"\\public.key");
 FileInputStream fis = new FileInputStream(path+"\\public.key");
 byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
 fis.read(encodedPublicKey);
 fis.close();

 // Read Private Key.
 File filePrivateKey = new File(path+"\\private.key");
 fis = new FileInputStream(path+"\\private.key");
 byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
 fis.read(encodedPrivateKey);
 fis.close();

 // Generate KeyPair.
 KeyFactory keyFactory = KeyFactory.getInstance("ECDH");
 X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
 encodedPublicKey);
 PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
 PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
 encodedPrivateKey);
 PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
  aKeyAgree = KeyAgreement.getInstance("ECDH", "BC");
  aKeyAgree.init(privateKey);
  aKeyAgree.doPhase(publicKey, true); 
  byte[] aBys = aKeyAgree.generateSecret(); 
  KeySpec aKeySpec = new DESKeySpec(aBys);
  SecretKeyFactory aFactory = SecretKeyFactory.getInstance("DES");
  Key aSecretKey = aFactory.generateSecret(aKeySpec);

  Cipher aCipher = Cipher.getInstance(aSecretKey.getAlgorithm());   
  aCipher.init(Cipher.ENCRYPT_MODE, aSecretKey);  
  byte[] encText = aCipher.doFinal("Its Rushi".getBytes());
  System.out.println(Base64.encodeBase64(encText));
  System.out.println(encText); 
}
 catch(Exception e){
   e.printStackTrace();
 }
 }
 }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298929 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO