Back to project page bitcoin-wallet.
The source code is released under:
Copyright (C) 2011 by Caleb Anderson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the ...
If you think the Android project bitcoin-wallet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.dirtyfilthy.bitcoin.util; /* www . j av a 2 s. com*/ import java.io.FileNotFoundException; import java.io.IOException; import java.math.BigInteger; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.security.Security; import java.security.Signature; import java.security.SignatureException; import java.security.cert.CertificateException; import java.security.interfaces.ECPrivateKey; import java.security.interfaces.ECPublicKey; import java.security.spec.ECPoint; import java.security.spec.EncodedKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import net.dirtyfilthy.bouncycastle.jce.ECNamedCurveTable; import net.dirtyfilthy.bouncycastle.jce.ECPointUtil; import net.dirtyfilthy.bouncycastle.jce.provider.BouncyCastleProvider; import net.dirtyfilthy.bouncycastle.jce.provider.JCEECPublicKey; import net.dirtyfilthy.bouncycastle.jce.provider.asymmetric.ec.EC5Util; import net.dirtyfilthy.bouncycastle.jce.spec.ECNamedCurveParameterSpec; import net.dirtyfilthy.bouncycastle.jce.spec.ECParameterSpec; import net.dirtyfilthy.bouncycastle.math.ec.ECCurve; import net.dirtyfilthy.bouncycastle.util.encoders.Hex; public class KeyTools { // encodes a public key into the form [0x04][32 bytes 'x' value][32 bytes 'y' value] public static byte[] encodePublicKey(ECPublicKey key){ if(Security.getProvider("DFBC")==null){ Security.addProvider(new BouncyCastleProvider()); } JCEECPublicKey key2=(JCEECPublicKey) key; key2.setPointFormat("UNCOMPRESSED"); net.dirtyfilthy.bouncycastle.math.ec.ECPoint p=EC5Util.convertPoint(key.getParams(), key.getW(), false); return p.getEncoded(); } // decodes a raw public key in the form [0x04][32 bytes 'x' value][32 bytes 'y' value] public static ECPublicKey decodePublicKey(byte[] encoded){ if(Security.getProvider("DFBC")==null){ Security.addProvider(new BouncyCastleProvider()); } ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1"); KeyFactory fact; try { fact = KeyFactory.getInstance("ECDSA", "DFBC"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchProviderException e) { throw new RuntimeException(e); } ECCurve curve = params.getCurve(); java.security.spec.EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, params.getSeed()); java.security.spec.ECPoint point=ECPointUtil.decodePoint(ellipticCurve, encoded); java.security.spec.ECParameterSpec params2=EC5Util.convertSpec(ellipticCurve, params); java.security.spec.ECPublicKeySpec keySpec = new java.security.spec.ECPublicKeySpec(point,params2); try { return (ECPublicKey) fact.generatePublic(keySpec); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } } public static byte[] signData(ECPrivateKey key,byte data[]){ if(Security.getProvider("DFBC")==null){ Security.addProvider(new BouncyCastleProvider()); } Signature s; try { s = Signature.getInstance("NONEwithECDSA", "DFBC"); s.initSign(key); s.update(data); return s.sign(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchProviderException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (SignatureException e) { throw new RuntimeException(e); } } public static KeyPair generateKeyPair() { if(Security.getProvider("DFBC")==null){ Security.addProvider(new BouncyCastleProvider()); } ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256k1"); KeyPairGenerator generator; try { generator = KeyPairGenerator.getInstance("ECDSA", "DFBC"); generator.initialize(ecSpec, new SecureRandom()); KeyPair pair = generator.generateKeyPair(); return pair; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchProviderException e) { throw new RuntimeException(e); } catch (InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } } public static ECPublicKey decodeDerPublicKey(byte[] encoded){ if(Security.getProvider("DFBC")==null){ Security.addProvider(new BouncyCastleProvider()); } EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encoded); KeyFactory keyFactory; ECPublicKey pubKey; try { keyFactory = KeyFactory.getInstance("ECDSA", "DFBC"); pubKey = (ECPublicKey) keyFactory.generatePublic(publicKeySpec); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchProviderException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } return pubKey; } public static ECPrivateKey decodeDerPrivateKey(byte[] encoded){ if(Security.getProvider("DFBC")==null){ Security.addProvider(new BouncyCastleProvider()); } EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encoded); KeyFactory keyFactory; ECPrivateKey privKey; try { keyFactory = KeyFactory.getInstance("ECDSA", "DFBC"); privKey = (ECPrivateKey) keyFactory.generatePrivate(privateKeySpec); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchProviderException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } return privKey; } public static boolean verifySignedData(ECPublicKey key,byte data[], byte sig[]){ if(Security.getProvider("DFBC")==null){ Security.addProvider(new BouncyCastleProvider()); } Signature s; try { s= Signature.getInstance("NONEwithECDSA", "DFBC"); s.initVerify(key); s.update(data); return s.verify(sig); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchProviderException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (SignatureException e) { throw new RuntimeException(e); } } }