Android Open Source - bitcoin-wallet Key Tools






From Project

Back to project page bitcoin-wallet.

License

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.

Java Source Code

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);
    }
  }

}




Java Source Code List

net.dirtyfilthy.bitcoin.core.Address.java
net.dirtyfilthy.bitcoin.core.Base58Hash160.java
net.dirtyfilthy.bitcoin.core.BlockChain.java
net.dirtyfilthy.bitcoin.core.BlockExistsException.java
net.dirtyfilthy.bitcoin.core.BlockStore.java
net.dirtyfilthy.bitcoin.core.Block.java
net.dirtyfilthy.bitcoin.core.BtcValue.java
net.dirtyfilthy.bitcoin.core.ByteArrayable.java
net.dirtyfilthy.bitcoin.core.InvalidBlockException.java
net.dirtyfilthy.bitcoin.core.OpCode.java
net.dirtyfilthy.bitcoin.core.OpData.java
net.dirtyfilthy.bitcoin.core.OrphanBlockException.java
net.dirtyfilthy.bitcoin.core.Script.java
net.dirtyfilthy.bitcoin.core.TxIn.java
net.dirtyfilthy.bitcoin.core.TxOut.java
net.dirtyfilthy.bitcoin.core.Tx.java
net.dirtyfilthy.bitcoin.protocol.AddressBook.java
net.dirtyfilthy.bitcoin.protocol.AddressPacket.java
net.dirtyfilthy.bitcoin.protocol.BlockPacket.java
net.dirtyfilthy.bitcoin.protocol.ConnectionHandler.java
net.dirtyfilthy.bitcoin.protocol.Connection.java
net.dirtyfilthy.bitcoin.protocol.GetAddressPacket.java
net.dirtyfilthy.bitcoin.protocol.GetBlocksPacket.java
net.dirtyfilthy.bitcoin.protocol.GetDataPacket.java
net.dirtyfilthy.bitcoin.protocol.GetHeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.HeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryVector.java
net.dirtyfilthy.bitcoin.protocol.IrcBootStrap.java
net.dirtyfilthy.bitcoin.protocol.MalformedPacketException.java
net.dirtyfilthy.bitcoin.protocol.PacketFactory.java
net.dirtyfilthy.bitcoin.protocol.PacketType.java
net.dirtyfilthy.bitcoin.protocol.Packet.java
net.dirtyfilthy.bitcoin.protocol.PingPacket.java
net.dirtyfilthy.bitcoin.protocol.ProtocolVersion.java
net.dirtyfilthy.bitcoin.protocol.ReplyPacket.java
net.dirtyfilthy.bitcoin.protocol.TxPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionAckPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionPacket.java
net.dirtyfilthy.bitcoin.util.Base58.java
net.dirtyfilthy.bitcoin.util.BigIntegerTools.java
net.dirtyfilthy.bitcoin.util.HashTools.java
net.dirtyfilthy.bitcoin.util.KeyTools.java
net.dirtyfilthy.bitcoin.util.MyHex.java
net.dirtyfilthy.bitcoin.wallet.ExposedSQLiteCursor.java
net.dirtyfilthy.bitcoin.wallet.InvalidPasswordException.java
net.dirtyfilthy.bitcoin.wallet.KeyRing.java
net.dirtyfilthy.bitcoin.wallet.SqlBlockStore.java
net.dirtyfilthy.bitcoin.wallet.Wallet.java