org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey Java Examples

The following examples show how to use org.spongycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ECKey.java    From wkcwallet-java with Apache License 2.0 6 votes vote down vote up
/**
 * Signs the given hash and returns the R and S components as BigIntegers
 * and put them in ECDSASignature
 *
 * @param input
 *            to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
    if (input.length != 32) {
        throw new IllegalArgumentException("Expected 32 byte input to ECDSA signature, not " + input.length);
    }
    // No decryption of private key required.
    if (privKey == null)
        throw new MissingPrivateKeyException();
    if (privKey instanceof BCECPrivateKey) {
        ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
        ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE);
        signer.init(true, privKeyParams);
        BigInteger[] components = signer.generateSignature(input);
        return new ECDSASignature(components[0], components[1]).toCanonicalised();
    } else {
        try {
            final Signature ecSig = ECSignatureFactory.getRawInstance(provider);
            ecSig.initSign(privKey);
            ecSig.update(input);
            final byte[] derSignature = ecSig.sign();
            return ECDSASignature.decodeFromDER(derSignature).toCanonicalised();
        } catch (SignatureException | InvalidKeyException ex) {
            throw new RuntimeException("ECKey signing error", ex);
        }
    }
}
 
Example #2
Source File: ECKey.java    From wkcwallet-java with Apache License 2.0 5 votes vote down vote up
/**
 * Produce a string rendering of the ECKey INCLUDING the private key. Unless
 * you absolutely need the private key it is better for security reasons to
 * just use toString().
 *
 *
 * @return -
 */
public String toStringWithPrivate() {
    StringBuilder b = new StringBuilder();
    b.append(toString());
    if (privKey != null && privKey instanceof BCECPrivateKey) {
        b.append(" priv:").append(Hex.toHexString(((BCECPrivateKey) privKey).getD().toByteArray()));
    }
    return b.toString();
}
 
Example #3
Source File: private_key.java    From bitshares_wallet with MIT License 5 votes vote down vote up
private private_key(KeyPair ecKey){
    BCECPrivateKey privateKey = (BCECPrivateKey) ecKey.getPrivate();
    byte[] privateKeyGenerate = privateKey.getD().toByteArray();
    if (privateKeyGenerate.length == 33) {
        System.arraycopy(privateKeyGenerate, 1, key_data, 0, key_data.length);
    } else {
        System.arraycopy(privateKeyGenerate, 0, key_data, 0, key_data.length);
    }
}
 
Example #4
Source File: ECKey.java    From asf-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a 32 byte array containing the private key, or null if the key is encrypted or
 * public only
 *
 * @return -
 */
@Nullable public byte[] getPrivKeyBytes() {
  if (privKey == null) {
    return null;
  } else if (privKey instanceof BCECPrivateKey) {
    return bigIntegerToBytes(((BCECPrivateKey) privKey).getD(), 32);
  } else {
    return null;
  }
}
 
Example #5
Source File: ECKey.java    From asf-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Signs the given hash and returns the R and S components as BigIntegers
 * and put them in ECDSASignature
 *
 * @param input to sign
 *
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
  if (input.length != 32) {
    throw new IllegalArgumentException(
        "Expected 32 byte input to ECDSA signature, not " + input.length);
  }
  // No decryption of private key required.
  if (privKey == null) throw new MissingPrivateKeyException();
  if (privKey instanceof BCECPrivateKey) {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
    ECPrivateKeyParameters privKeyParams =
        new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE);
    signer.init(true, privKeyParams);
    BigInteger[] components = signer.generateSignature(input);
    return new ECDSASignature(components[0], components[1]).toCanonicalised();
  } else {
    try {
      Signature ecSig = ECSignatureFactory.getRawInstance(provider);
      ecSig.initSign(privKey);
      ecSig.update(input);
      byte[] derSignature = ecSig.sign();
      return ECDSASignature.decodeFromDER(derSignature)
          .toCanonicalised();
    } catch (SignatureException | InvalidKeyException ex) {
      throw new RuntimeException("ECKey signing error", ex);
    }
  }
}
 
Example #6
Source File: ECKey.java    From asf-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Produce a string rendering of the ECKey INCLUDING the private key.
 * Unless you absolutely need the private key it is better for security reasons to just use
 * toString().
 *
 * @return -
 */
public String toStringWithPrivate() {
  StringBuilder b = new StringBuilder();
  b.append(toString());
  if (privKey != null && privKey instanceof BCECPrivateKey) {
    b.append(" priv:")
        .append(Hex.toHexString(((BCECPrivateKey) privKey).getD()
            .toByteArray()));
  }
  return b.toString();
}
 
Example #7
Source File: ECKey.java    From asf-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the private key in the form of an integer field element. The public key is derived by
 * performing EC
 * point addition this number of times (i.e. point multiplying).
 *
 * @return -
 *
 * @throws IllegalStateException if the private key bytes are not available.
 */
public BigInteger getPrivKey() {
  if (privKey == null) {
    throw new MissingPrivateKeyException();
  } else if (privKey instanceof BCECPrivateKey) {
    return ((BCECPrivateKey) privKey).getD();
  } else {
    throw new MissingPrivateKeyException();
  }
}
 
Example #8
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a 32 byte array containing the private key, or null if the key is encrypted or public
 * only
 *
 * @return -
 */
@Nullable
public byte[] getPrivKeyBytes() {
  if (privKey == null) {
    return null;
  } else if (privKey instanceof BCECPrivateKey) {
    return bigIntegerToBytes(((BCECPrivateKey) privKey).getD(), 32);
  } else {
    return null;
  }
}
 
Example #9
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 5 votes vote down vote up
/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher -proper cipher
 * @return decrypted cipher, equal length to the cipher.
 * @deprecated should not use EC private scalar value as an AES key
 */
public byte[] decryptAES(byte[] cipher) {

  if (privKey == null) {
    throw new MissingPrivateKeyException();
  }
  if (!(privKey instanceof BCECPrivateKey)) {
    throw new UnsupportedOperationException("Cannot use the private " +
        "key as an AES key");
  }

  AESFastEngine engine = new AESFastEngine();
  SICBlockCipher ctrEngine = new SICBlockCipher(engine);

  KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray((
      (BCECPrivateKey) privKey).getD()));
  ParametersWithIV params = new ParametersWithIV(key, new byte[16]);

  ctrEngine.init(false, params);

  int i = 0;
  byte[] out = new byte[cipher.length];
  while (i < cipher.length) {
    ctrEngine.processBlock(cipher, i, out, i);
    i += engine.getBlockSize();
    if (cipher.length - i < engine.getBlockSize()) {
      break;
    }
  }

  // process left bytes
  if (cipher.length - i > 0) {
    byte[] tmpBlock = new byte[16];
    System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
    ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
    System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
  }

  return out;
}
 
Example #10
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 5 votes vote down vote up
/**
 * Signs the given hash and returns the R and S components as BigIntegers and putData them in
 * ECDSASignature
 *
 * @param input to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
  if (input.length != 32) {
    throw new IllegalArgumentException("Expected 32 byte input to " +
        "ECDSA signature, not " + input.length);
  }
  // No decryption of private key required.
  if (privKey == null) {
    throw new MissingPrivateKeyException();
  }
  if (privKey instanceof BCECPrivateKey) {
    ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new
        SHA256Digest()));
    ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters
        (((BCECPrivateKey) privKey).getD(), CURVE);
    signer.init(true, privKeyParams);
    BigInteger[] components = signer.generateSignature(input);
    return new ECDSASignature(components[0], components[1])
        .toCanonicalised();
  } else {
    try {
      final Signature ecSig = ECSignatureFactory.getRawInstance
          (provider);
      ecSig.initSign(privKey);
      ecSig.update(input);
      final byte[] derSignature = ecSig.sign();
      return ECDSASignature.decodeFromDER(derSignature)
          .toCanonicalised();
    } catch (SignatureException | InvalidKeyException ex) {
      throw new RuntimeException("ECKey signing error", ex);
    }
  }
}
 
Example #11
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 5 votes vote down vote up
/**
 * Produce a string rendering of the ECKey INCLUDING the private key. Unless you absolutely need
 * the private key it is better for security reasons to just use toString().
 *
 * @return -
 */
public String toStringWithPrivate() {
  StringBuilder b = new StringBuilder();
  b.append(toString());
  if (privKey != null && privKey instanceof BCECPrivateKey) {
    b.append(" priv:").append(Hex.toHexString(((BCECPrivateKey)
        privKey).getD().toByteArray()));
  }
  return b.toString();
}
 
Example #12
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the private key in the form of an integer field element. The public key is derived by
 * performing EC point addition this number of times (i.e. point multiplying).
 *
 * @return -
 * @throws IllegalStateException if the private key bytes are not available.
 */
public BigInteger getPrivKey() {
  if (privKey == null) {
    throw new MissingPrivateKeyException();
  } else if (privKey instanceof BCECPrivateKey) {
    return ((BCECPrivateKey) privKey).getD();
  } else {
    throw new MissingPrivateKeyException();
  }
}
 
Example #13
Source File: ECKey.java    From wkcwallet-java with Apache License 2.0 5 votes vote down vote up
/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher
 *            -proper cipher
 * @return decrypted cipher, equal length to the cipher.
 * @deprecated should not use EC private scalar value as an AES key
 */
public byte[] decryptAES(byte[] cipher) {

    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (!(privKey instanceof BCECPrivateKey)) {
        throw new UnsupportedOperationException("Cannot use the private key as an AES key");
    }

    AESFastEngine engine = new AESFastEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);

    KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD()));
    ParametersWithIV params = new ParametersWithIV(key, new byte[16]);

    ctrEngine.init(false, params);

    int i = 0;
    byte[] out = new byte[cipher.length];
    while (i < cipher.length) {
        ctrEngine.processBlock(cipher, i, out, i);
        i += engine.getBlockSize();
        if (cipher.length - i < engine.getBlockSize())
            break;
    }

    // process left bytes
    if (cipher.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
    }

    return out;
}
 
Example #14
Source File: private_key.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
private private_key(KeyPair ecKey) {
    BCECPrivateKey privateKey = (BCECPrivateKey) ecKey.getPrivate();
    byte[] privateKeyGenerate = privateKey.getD().toByteArray();
    if (privateKeyGenerate.length == 33) {
        System.arraycopy(privateKeyGenerate, 1, key_data, 0, key_data.length);
    } else {
        System.arraycopy(privateKeyGenerate, 0, key_data, 0, key_data.length);
    }
}
 
Example #15
Source File: ECKey.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a 32 byte array containing the private key, or null if the key is encrypted or public
 * only
 *
 * @return -
 */
@Nullable
public byte[] getPrivKeyBytes() {
    if (privKey == null) {
        return null;
    } else if (privKey instanceof BCECPrivateKey) {
        return ByteUtil.bigIntegerToBytes(((BCECPrivateKey) privKey).getD(), 32);
    } else {
        return null;
    }
}
 
Example #16
Source File: ECKey.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher -proper cipher
 * @return decrypted cipher, equal length to the cipher.
 * @deprecated should not use EC private scalar value as an AES key
 */
public byte[] decryptAES(byte[] cipher) {

    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (!(privKey instanceof BCECPrivateKey)) {
        throw new UnsupportedOperationException("Cannot use the private " +
                "key as an AES key");
    }

    AESEngine engine = new AESEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);

    KeyParameter key = new KeyParameter(BigIntegers.asUnsignedByteArray((
            (BCECPrivateKey) privKey).getD()));
    ParametersWithIV params = new ParametersWithIV(key, new byte[16]);

    ctrEngine.init(false, params);

    int i = 0;
    byte[] out = new byte[cipher.length];
    while (i < cipher.length) {
        ctrEngine.processBlock(cipher, i, out, i);
        i += engine.getBlockSize();
        if (cipher.length - i < engine.getBlockSize()) {
            break;
        }
    }

    // process left bytes
    if (cipher.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
    }

    return out;
}
 
Example #17
Source File: ECKey.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Signs the given hash and returns the R and S components as BigIntegers and putData them in
 * ECDSASignature
 *
 * @param input to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
    if (input.length != 32) {
        throw new IllegalArgumentException("Expected 32 byte input to " +
                "ECDSA signature, not " + input.length);
    }
    // No decryption of private key required.
    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (privKey instanceof BCECPrivateKey) {
        ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new
                SHA256Digest()));
        ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters
                (((BCECPrivateKey) privKey).getD(), CURVE);
        signer.init(true, privKeyParams);
        BigInteger[] components = signer.generateSignature(input);
        return new ECDSASignature(components[0], components[1])
                .toCanonicalised();
    } else {
        try {
            final Signature ecSig = ECSignatureFactory.getRawInstance
                    (provider);
            ecSig.initSign(privKey);
            ecSig.update(input);
            final byte[] derSignature = ecSig.sign();
            return ECDSASignature.decodeFromDER(derSignature)
                    .toCanonicalised();
        } catch (SignatureException | InvalidKeyException ex) {
            throw new RuntimeException("ECKey signing error", ex);
        }
    }
}
 
Example #18
Source File: ECKey.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Produce a string rendering of the ECKey INCLUDING the private key. Unless you absolutely need
 * the private key it is better for security reasons to just use toString().
 *
 * @return -
 */
public String toStringWithPrivate() {
    StringBuilder b = new StringBuilder();
    b.append(toString());
    if (privKey != null && privKey instanceof BCECPrivateKey) {
        b.append(" priv:").append(Hex.toHexString(((BCECPrivateKey)
                privKey).getD().toByteArray()));
    }
    return b.toString();
}
 
Example #19
Source File: ECKey.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets the private key in the form of an integer field element. The public key is derived by
 * performing EC point addition this number of times (i.e. point multiplying).
 *
 * @return -
 * @throws IllegalStateException if the private key bytes are not available.
 */
public BigInteger getPrivKey() {
    if (privKey == null) {
        throw new MissingPrivateKeyException();
    } else if (privKey instanceof BCECPrivateKey) {
        return ((BCECPrivateKey) privKey).getD();
    } else {
        throw new MissingPrivateKeyException();
    }
}
 
Example #20
Source File: private_key.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
private private_key(KeyPair ecKey){
    BCECPrivateKey privateKey = (BCECPrivateKey) ecKey.getPrivate();
    byte[] privateKeyGenerate = privateKey.getD().toByteArray();
    if (privateKeyGenerate.length == 33) {
        System.arraycopy(privateKeyGenerate, 1, key_data, 0, key_data.length);
    } else {
        System.arraycopy(privateKeyGenerate, 0, key_data, 0, key_data.length);
    }
}
 
Example #21
Source File: ECKeySecp256k1.java    From aion with MIT License 5 votes vote down vote up
/**
 * Returns a 32 byte array containing the private key, or null if the key is encrypted or public
 * only
 *
 * @return -
 */
public byte[] getPrivKeyBytes() {
    if (privKey == null) {
        return null;
    } else if (privKey instanceof BCECPrivateKey) {
        return bigIntegerToBytes(((BCECPrivateKey) privKey).getD(), 32);
    } else {
        return null;
    }
}
 
Example #22
Source File: ECKeySecp256k1.java    From aion with MIT License 5 votes vote down vote up
/**
 * Decrypt cipher by AES in SIC(also know as CTR) mode
 *
 * @param cipher -proper cipher
 * @return decrypted cipher, equal length to the cipher.
 * @deprecated should not use EC private scalar value as an AES key
 */
public byte[] decryptAES(byte[] cipher) {

    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (!(privKey instanceof BCECPrivateKey)) {
        throw new UnsupportedOperationException("Cannot use the private key as an AES key");
    }

    AESFastEngine engine = new AESFastEngine();
    SICBlockCipher ctrEngine = new SICBlockCipher(engine);

    KeyParameter key =
            new KeyParameter(
                    BigIntegers.asUnsignedByteArray(((BCECPrivateKey) privKey).getD()));
    ParametersWithIV params = new ParametersWithIV(key, new byte[16]);

    ctrEngine.init(false, params);

    int i = 0;
    byte[] out = new byte[cipher.length];
    while (i < cipher.length) {
        ctrEngine.processBlock(cipher, i, out, i);
        i += engine.getBlockSize();
        if (cipher.length - i < engine.getBlockSize()) {
            break;
        }
    }

    // process left bytes
    if (cipher.length - i > 0) {
        byte[] tmpBlock = new byte[16];
        System.arraycopy(cipher, i, tmpBlock, 0, cipher.length - i);
        ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0);
        System.arraycopy(tmpBlock, 0, out, i, cipher.length - i);
    }

    return out;
}
 
Example #23
Source File: ECKeySecp256k1.java    From aion with MIT License 5 votes vote down vote up
/**
 * Signs the given hash and returns the R and S components as BigIntegers and put them in
 * ECDSASignature
 *
 * @param input to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
    if (input.length != 32) {
        throw new IllegalArgumentException(
                "Expected 32 byte input to ECDSA signature, not " + input.length);
    }
    // No decryption of private key required.
    if (privKey == null) {
        throw new MissingPrivateKeyException();
    }
    if (privKey instanceof BCECPrivateKey) {
        ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
        ECPrivateKeyParameters privKeyParams =
                new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE);
        signer.init(true, privKeyParams);
        BigInteger[] components = signer.generateSignature(input);
        return new ECDSASignature(components[0], components[1]).toCanonicalised();
    } else {
        try {
            final Signature ecSig = ECSignatureFactory.getRawInstance(provider);
            ecSig.initSign(privKey);
            ecSig.update(input);
            final byte[] derSignature = ecSig.sign();
            return ECDSASignature.decodeFromDER(derSignature).toCanonicalised();
        } catch (SignatureException | InvalidKeyException ex) {
            throw new RuntimeException("ECKey signing error", ex);
        }
    }
}
 
Example #24
Source File: ECKeySecp256k1.java    From aion with MIT License 5 votes vote down vote up
/**
 * Produce a string rendering of the ECKey INCLUDING the private key. Unless you absolutely need
 * the private key it is better for security reasons to just use toString().
 *
 * @return -
 */
public String toStringWithPrivate() {
    StringBuilder b = new StringBuilder();
    b.append(toString());
    if (privKey != null && privKey instanceof BCECPrivateKey) {
        b.append(" priv:")
                .append(Hex.toHexString(((BCECPrivateKey) privKey).getD().toByteArray()));
    }
    return b.toString();
}
 
Example #25
Source File: ECKeySecp256k1.java    From aion with MIT License 5 votes vote down vote up
/**
 * Gets the private key in the form of an integer field element. The public key is derived by
 * performing EC point addition this number of times (i.e. point multiplying).
 *
 * @return -
 * @throws java.lang.IllegalStateException if the private key bytes are not available.
 */
public BigInteger getPrivKey() {
    if (privKey == null) {
        throw new MissingPrivateKeyException();
    } else if (privKey instanceof BCECPrivateKey) {
        return ((BCECPrivateKey) privKey).getD();
    } else {
        throw new MissingPrivateKeyException();
    }
}
 
Example #26
Source File: ECKey.java    From wkcwallet-java with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the private key in the form of an integer field element. The public
 * key is derived by performing EC point addition this number of times (i.e.
 * point multiplying).
 *
 *
 * @return -
 *
 * @throws IllegalStateException
 *             if the private key bytes are not available.
 */
public BigInteger getPrivKey() {
    if (privKey == null) {
        throw new MissingPrivateKeyException();
    } else if (privKey instanceof BCECPrivateKey) {
        return ((BCECPrivateKey) privKey).getD();
    } else {
        throw new MissingPrivateKeyException();
    }
}