user123
user123

Reputation: 69

How to Convert a byte array to DSA private key?

As a part of project implementation, i have done the following things: 1. Generete DSA keys 2. Encrypt the private key using AES 3. Save into the file 4. Open the file and read the encrypted private key 5. I tried to convert the read value into primary key format but error comes here. I am attaching the code of the above steps here :

public class Pgm {
public static void main(String[] args) { 
try {
            KeyPairGenerator dsa =  KeyPairGenerator.getInstance("DSA");
            SecureRandom random = new SecureRandom();
            dsa.initialize(1024, random);
            KeyPair keypair = dsa.generateKeyPair();
            PrivateKey privateKey = (PrivateKey) keypair.getPrivate();
            byte[] key = "�u���1�iw&a".getBytes();
            Key aesKey = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            String currentDir = System.getProperty("user.dir"); 
            // encrypt the text
            cipher.init(Cipher.ENCRYPT_MODE, aesKey);
            byte[] abc = privateKey.getEncoded();

            byte[] encrypted = cipher.doFinal(abc);
            // System.out.println("len="+encrypted.length());
            File dir=new File(currentDir);
            File private_file=new File(dir,"privatekey.txt");
            if(!private_file.exists()){
                private_file.createNewFile();
            }
            FileOutputStream fileos = new FileOutputStream(private_file); 
            ObjectOutputStream objectos = new ObjectOutputStream(fileos);
            objectos.writeObject(encrypted);
            objectos.close();
            fileos.close();

            File file_private = new File(dir,"privatekey.txt");
            FileInputStream fileo = new FileInputStream(file_private); 
            ObjectInputStream objos = new ObjectInputStream(fileo);
            Object obj = objos.readObject();
            byte[] encrypted1= (byte[] )obj;
            cipher.init(Cipher.DECRYPT_MODE, aesKey);
            String decrypted = new String(cipher.doFinal(encrypted1));
            if (decrypted.equals(new String(abc)))
               System.out.println("true");
            else
               System.out.println("false");
            Signature tosign = Signature.getInstance("DSA");
            byte[] val =  decrypted.getBytes();
            PrivateKey privatekey1 = (PrivateKey)val;
            tosign.initSign(privatekey1);   

      }
      catch(Exception e)
      {
            e.printStackTrace();
      }
  }

}

' PrivateKey privatekey1 = (PrivateKey)val;

shows the error as inconvertible types

Upvotes: 0

Views: 1125

Answers (2)

ares
ares

Reputation: 31

You can't simple convert byte array to PrivateKey instance. You can generate keys by KeyFactory

Upvotes: 2

Hessam
Hessam

Reputation: 1415

What if you replace explicit casting with this:

PrivateKey privateKey1 = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(val));

Upvotes: 0

Related Questions