Get data from KeyStore Java

Jak mohu získat data ze souboru s koncovkou *.keystore nutného k certefikaci – podepsání Android aplikace.
How can get data from file *.keystore Java exmaple.

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableEntryException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
 
 
public class main {
 
	/**
	 * @param args
	 * @throws FileNotFoundException 
	 * @throws KeyStoreException 
	 * @throws UnrecoverableEntryException 
	 */
	public static void main(String[] args) throws FileNotFoundException, KeyStoreException, UnrecoverableEntryException {
		// TODO Auto-generated method stub
 
		KeyStore ks;
 
	    // get user password and file input stream
	    //char[] password = getPassword();
	    //char[] password = { 'p','a','s','s','w','o','r','d'};
	    char[] password = "mypassvord".toCharArray(); 
 
/////////////////
	    java.io.FileInputStream fis = null;
	    try {
	        try {
				fis = new java.io.FileInputStream(
						"c:\\keyStoreName.keystore");
			} catch (Exception e) {
				// TODO: handle exception
				String s = e.toString();
			}
			try {
	        	ks = KeyStore.getInstance(KeyStore.getDefaultType());
				ks.load(fis, password);
 
				 // get my private key
			    KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
			        ks.getEntry("privateKeyAlias", new KeyStore.PasswordProtection(password));
			    PrivateKey myPrivateKey = pkEntry.getPrivateKey();
 
			    // save my secret key
			    javax.crypto.SecretKey mySecretKey = null;
			    KeyStore.SecretKeyEntry skEntry =
			        new KeyStore.SecretKeyEntry(mySecretKey);
			    ks.setEntry("secretKeyAlias", skEntry, 
			        new KeyStore.PasswordProtection(password));
 
 
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (CertificateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    } finally {
	        if (fis != null) {
	            try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	    }
 
 
 
 
	}
 
}
This entry was posted in Android Examples Code and tagged , , . Bookmark the permalink.


Comments are closed.



5,563 views