Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.tiphedor.tools;
- import android.content.Context;
- import org.bouncycastle.x509.X509V1CertificateGenerator;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.security.InvalidKeyException;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- import java.security.KeyStore;
- import java.security.KeyStoreException;
- import java.security.NoSuchAlgorithmException;
- import java.security.NoSuchProviderException;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.security.SignatureException;
- import java.security.UnrecoverableKeyException;
- import java.security.cert.Certificate;
- import java.security.cert.CertificateEncodingException;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import java.util.Date;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import javax.security.auth.x500.X500Principal;
- /**
- * Crypto manager
- */
- public class CryptoManager {
- /*
- * Context is required to access file input/output
- */
- Context ctx;
- /*
- * Location of the keystore on disk
- */
- private final String KEYSTORE_FILE = "bc.keystore";
- /*
- * Key's alias
- */
- private final String PRIVATE_KEY_ALIAS = "PRIVATEKEY";
- public CryptoManager(Context ctx) {
- this.ctx = ctx;
- }
- public KeyPair generateKeyPair() throws NoSuchAlgorithmException {
- KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
- keygen.initialize(2048);
- return keygen.generateKeyPair();
- }
- public KeyStore getKeyStore() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
- KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
- FileInputStream fis = null;
- try {
- fis = ctx.openFileInput(KEYSTORE_FILE);
- ks.load(fis, null);
- } catch (FileNotFoundException e) {
- ks.load(null, null);
- }
- if(fis != null) { fis.close(); }
- return ks;
- }
- public void saveKeyStore(KeyStore ks) throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
- FileOutputStream fos = ctx.openFileOutput(KEYSTORE_FILE, Context.MODE_PRIVATE);
- ks.store(fos, null);
- fos.close();
- }
- public void storeKeyPair(KeyPair kp) throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException, InvalidKeyException, NoSuchProviderException, SignatureException {
- KeyStore ks = getKeyStore();
- Certificate[] cert = new Certificate[1];
- cert[0] = getCertificate(kp);
- ks.setKeyEntry(PRIVATE_KEY_ALIAS, kp.getPrivate(), null, cert);
- saveKeyStore(ks);
- }
- public PrivateKey fetchPrivateKey() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException {
- return (PrivateKey) getKeyStore().getKey(PRIVATE_KEY_ALIAS, null);
- }
- public PublicKey fetchPublicKey() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
- return (PublicKey) getKeyStore().getCertificate(PRIVATE_KEY_ALIAS).getPublicKey();
- }
- public String fetchStringPrivateKey() throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
- return bytesToHex(fetchPrivateKey().getEncoded());
- }
- public String fetchStringPublicKey() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
- return bytesToHex(fetchPublicKey().getEncoded());
- }
- public byte[] encryptString(String plain, PublicKey publicKey) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
- return encryptByte(plain.getBytes(), publicKey);
- }
- public byte[] encryptByte(byte[] plain, PublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
- return cipher.doFinal(plain);
- }
- public byte[] decryptByte(byte[] crypted, PrivateKey privateKey) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- return cipher.doFinal(crypted);
- }
- public String decryptByteToString(byte[] crypted, PrivateKey privareKey) throws NoSuchAlgorithmException, BadPaddingException, NoSuchPaddingException, IllegalBlockSizeException, InvalidKeyException {
- return new String(decryptByte(crypted, privareKey));
- }
- final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
- public static String bytesToHex(byte[] bytes) {
- char[] hexChars = new char[bytes.length * 2];
- for ( int j = 0; j < bytes.length; j++ ) {
- int v = bytes[j] & 0xFF;
- hexChars[j * 2] = hexArray[v >>> 4];
- hexChars[j * 2 + 1] = hexArray[v & 0x0F];
- }
- return new String(hexChars);
- }
- X509Certificate getCertificate(KeyPair kp) throws NoSuchAlgorithmException, CertificateEncodingException, NoSuchProviderException, InvalidKeyException, SignatureException {
- Date startDate = new Date(System.currentTimeMillis());
- Date expiryDate = new Date(System.currentTimeMillis() + 631138519494l);
- BigInteger serialNumber = BigInteger.valueOf(1);
- KeyPair keyPair = kp;
- X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
- X500Principal dnName = new X500Principal("CN=Test CA Certificate");
- certGen.setSerialNumber(serialNumber);
- certGen.setIssuerDN(dnName);
- certGen.setNotBefore(startDate);
- certGen.setNotAfter(expiryDate);
- certGen.setSubjectDN(dnName);
- certGen.setPublicKey(keyPair.getPublic());
- certGen.setSignatureAlgorithm("SHA256withRSA");
- return certGen.generate(kp.getPrivate(), "BC");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement