@Test public void testEncryptionIsReversible() throws Exception { ECKey originalUnencryptedKey = new ECKey(); EncryptedData encryptedPrivateKey = keyCrypter.encrypt(originalUnencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1)); ECKey encryptedKey = ECKey.fromEncrypted(encryptedPrivateKey, keyCrypter, originalUnencryptedKey.getPubKey()); assertTrue("Key not encrypted at start", encryptedKey.isEncrypted()); assertTrue("Key encryption is not reversible but it should be", ECKey.encryptionIsReversible(originalUnencryptedKey, encryptedKey, keyCrypter, keyCrypter.deriveKey(PASSWORD1))); assertTrue("Key encryption is reversible with wrong password", !ECKey.encryptionIsReversible(originalUnencryptedKey, encryptedKey, keyCrypter, keyCrypter.deriveKey(WRONG_PASSWORD))); byte[] goodEncryptedPrivateKeyBytes = encryptedPrivateKey.encryptedBytes; byte[] badEncryptedPrivateKeyBytes = new byte[goodEncryptedPrivateKeyBytes.length]; encryptedPrivateKey = new EncryptedData(encryptedPrivateKey.initialisationVector, badEncryptedPrivateKeyBytes); ECKey badEncryptedKey = ECKey.fromEncrypted(encryptedPrivateKey, keyCrypter, originalUnencryptedKey.getPubKey()); assertTrue("Key encryption is reversible with faulty encrypted bytes", !ECKey.encryptionIsReversible(originalUnencryptedKey, badEncryptedKey, keyCrypter, keyCrypter.deriveKey(PASSWORD1))); }
public static boolean encryptionIsReversible(ECKey originalKey, ECKey encryptedKey, KeyCrypter keyCrypter, KeyParameter aesKey) { try { ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, aesKey); byte[] originalPrivateKeyBytes = originalKey.getPrivKeyBytes(); byte[] rebornKeyBytes = rebornUnencryptedKey.getPrivKeyBytes(); if (!Arrays.equals(originalPrivateKeyBytes, rebornKeyBytes)) { log.error("The check that encryption could be reversed failed for {}", originalKey); return false; } return true; } catch (KeyCrypterException kce) { log.error(kce.getMessage()); return false; } }
ECKey implements EncryptableItem, Serializable { public static boolean encryptionIsReversible(ECKey originalKey, ECKey encryptedKey, KeyCrypter keyCrypter, KeyParameter aesKey) { try { ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, aesKey); byte[] originalPrivateKeyBytes = originalKey.getPrivKeyBytes(); byte[] rebornKeyBytes = rebornUnencryptedKey.getPrivKeyBytes(); if (!Arrays.equals(originalPrivateKeyBytes, rebornKeyBytes)) { log.error("The check that encryption could be reversed failed for {}", originalKey); return false; } return true; } catch (KeyCrypterException kce) { log.error(kce.getMessage()); return false; } } }
ECKey implements EncryptableItem, Serializable { public static boolean encryptionIsReversible(ECKey originalKey, ECKey encryptedKey, KeyCrypter keyCrypter, KeyParameter aesKey) { try { ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, aesKey); byte[] originalPrivateKeyBytes = originalKey.getPrivKeyBytes(); byte[] rebornKeyBytes = rebornUnencryptedKey.getPrivKeyBytes(); if (!Arrays.equals(originalPrivateKeyBytes, rebornKeyBytes)) { log.error("The check that encryption could be reversed failed for {}", originalKey); return false; } return true; } catch (KeyCrypterException kce) { log.error(kce.getMessage()); return false; } } ECKey();  ECKey(SecureRandom secureRandom); protected  ECKey(@Nullable BigInteger priv, ECPoint pub); protected  ECKey(@Nullable BigInteger priv, LazyECPoint pub); @Deprecated  ECKey(@Nullable byte[] privKeyBytes, @Nullable byte[] pubKey); @Deprecated  ECKey(EncryptedData encryptedPrivateKey, byte[] pubKey, KeyCrypter keyCrypter); @Deprecated  ECKey(@Nullable BigInteger privKey, @Nullable byte[] pubKey, boolean compressed); @Deprecated private  ECKey(@Nullable BigInteger privKey, @Nullable byte[] pubKey); }
ECKey implements EncryptableItem, Serializable { public static boolean encryptionIsReversible(ECKey originalKey, ECKey encryptedKey, KeyCrypter keyCrypter, KeyParameter aesKey) { try { ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, aesKey); byte[] originalPrivateKeyBytes = originalKey.getPrivKeyBytes(); byte[] rebornKeyBytes = rebornUnencryptedKey.getPrivKeyBytes(); if (!Arrays.equals(originalPrivateKeyBytes, rebornKeyBytes)) { log.error("The check that encryption could be reversed failed for {}", originalKey); return false; } return true; } catch (KeyCrypterException kce) { log.error(kce.getMessage()); return false; } } ECKey();  ECKey(SecureRandom secureRandom); protected  ECKey(@Nullable BigInteger priv, ECPoint pub); protected  ECKey(@Nullable BigInteger priv, LazyECPoint pub); @Deprecated  ECKey(@Nullable byte[] privKeyBytes, @Nullable byte[] pubKey); @Deprecated  ECKey(EncryptedData encryptedPrivateKey, byte[] pubKey, KeyCrypter keyCrypter); @Deprecated  ECKey(@Nullable BigInteger privKey, @Nullable byte[] pubKey, boolean compressed); @Deprecated private  ECKey(@Nullable BigInteger privKey, @Nullable byte[] pubKey); static ECPoint compressPoint(ECPoint point); static LazyECPoint compressPoint(LazyECPoint point); static ECPoint decompressPoint(ECPoint point); static LazyECPoint decompressPoint(LazyECPoint point); static ECKey fromASN1(byte[] asn1privkey); static ECKey fromPrivate(BigInteger privKey); static ECKey fromPrivate(BigInteger privKey, boolean compressed); static ECKey fromPrivate(byte[] privKeyBytes); static ECKey fromPrivate(byte[] privKeyBytes, boolean compressed); static ECKey fromPrivateAndPrecalculatedPublic(BigInteger priv, ECPoint pub); static ECKey fromPrivateAndPrecalculatedPublic(byte[] priv, byte[] pub); static ECKey fromPublicOnly(ECPoint pub); static ECKey fromPublicOnly(byte[] pub); ECKey decompress(); static ECKey fromEncrypted(EncryptedData encryptedPrivateKey, KeyCrypter crypter, byte[] pubKey); boolean isPubKeyOnly(); boolean hasPrivKey(); boolean isWatching(); byte[] toASN1(); static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed); static ECPoint publicPointFromPrivate(BigInteger privKey); byte[] getPubKeyHash(); byte[] getPubKey(); ECPoint getPubKeyPoint(); BigInteger getPrivKey(); boolean isCompressed(); Address toAddress(NetworkParameters params); ECDSASignature sign(Sha256Hash input); ECDSASignature sign(Sha256Hash input, @Nullable KeyParameter aesKey); static boolean verify(byte[] data, ECDSASignature signature, byte[] pub); static boolean verify(byte[] data, byte[] signature, byte[] pub); boolean verify(byte[] hash, byte[] signature); boolean verify(Sha256Hash sigHash, ECDSASignature signature); void verifyOrThrow(byte[] hash, byte[] signature); void verifyOrThrow(Sha256Hash sigHash, ECDSASignature signature); static boolean isPubKeyCanonical(byte[] pubkey); String signMessage(String message); String signMessage(String message, @Nullable KeyParameter aesKey); static ECKey signedMessageToKey(String message, String signatureBase64); void verifyMessage(String message, String signatureBase64); @Nullable static ECKey recoverFromSignature(int recId, ECDSASignature sig, Sha256Hash message, boolean compressed); byte[] getPrivKeyBytes(); DumpedPrivateKey getPrivateKeyEncoded(NetworkParameters params); @Override long getCreationTimeSeconds(); void setCreationTimeSeconds(long newCreationTimeSeconds); ECKey encrypt(KeyCrypter keyCrypter, KeyParameter aesKey); ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey); ECKey decrypt(KeyParameter aesKey); ECKey maybeDecrypt(@Nullable KeyParameter aesKey); static boolean encryptionIsReversible(ECKey originalKey, ECKey encryptedKey, KeyCrypter keyCrypter, KeyParameter aesKey); @Override boolean isEncrypted(); @Nullable @Override Protos.Wallet.EncryptionType getEncryptionType(); @Override @Nullable byte[] getSecretBytes(); @Nullable @Override EncryptedData getEncryptedData(); @Nullable EncryptedData getEncryptedPrivateKey(); @Nullable KeyCrypter getKeyCrypter(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); String toStringWithPrivate(NetworkParameters params); String getPrivateKeyAsHex(); String getPublicKeyAsHex(); String getPrivateKeyAsWiF(NetworkParameters params); void formatKeyWithAddress(boolean includePrivateKeys, StringBuilder builder, NetworkParameters params); }
ECKey implements EncryptableItem, Serializable { public static boolean encryptionIsReversible(ECKey originalKey, ECKey encryptedKey, KeyCrypter keyCrypter, KeyParameter aesKey) { try { ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, aesKey); byte[] originalPrivateKeyBytes = originalKey.getPrivKeyBytes(); byte[] rebornKeyBytes = rebornUnencryptedKey.getPrivKeyBytes(); if (!Arrays.equals(originalPrivateKeyBytes, rebornKeyBytes)) { log.error("The check that encryption could be reversed failed for {}", originalKey); return false; } return true; } catch (KeyCrypterException kce) { log.error(kce.getMessage()); return false; } } ECKey();  ECKey(SecureRandom secureRandom); protected  ECKey(@Nullable BigInteger priv, ECPoint pub); protected  ECKey(@Nullable BigInteger priv, LazyECPoint pub); @Deprecated  ECKey(@Nullable byte[] privKeyBytes, @Nullable byte[] pubKey); @Deprecated  ECKey(EncryptedData encryptedPrivateKey, byte[] pubKey, KeyCrypter keyCrypter); @Deprecated  ECKey(@Nullable BigInteger privKey, @Nullable byte[] pubKey, boolean compressed); @Deprecated private  ECKey(@Nullable BigInteger privKey, @Nullable byte[] pubKey); static ECPoint compressPoint(ECPoint point); static LazyECPoint compressPoint(LazyECPoint point); static ECPoint decompressPoint(ECPoint point); static LazyECPoint decompressPoint(LazyECPoint point); static ECKey fromASN1(byte[] asn1privkey); static ECKey fromPrivate(BigInteger privKey); static ECKey fromPrivate(BigInteger privKey, boolean compressed); static ECKey fromPrivate(byte[] privKeyBytes); static ECKey fromPrivate(byte[] privKeyBytes, boolean compressed); static ECKey fromPrivateAndPrecalculatedPublic(BigInteger priv, ECPoint pub); static ECKey fromPrivateAndPrecalculatedPublic(byte[] priv, byte[] pub); static ECKey fromPublicOnly(ECPoint pub); static ECKey fromPublicOnly(byte[] pub); ECKey decompress(); static ECKey fromEncrypted(EncryptedData encryptedPrivateKey, KeyCrypter crypter, byte[] pubKey); boolean isPubKeyOnly(); boolean hasPrivKey(); boolean isWatching(); byte[] toASN1(); static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed); static ECPoint publicPointFromPrivate(BigInteger privKey); byte[] getPubKeyHash(); byte[] getPubKey(); ECPoint getPubKeyPoint(); BigInteger getPrivKey(); boolean isCompressed(); Address toAddress(NetworkParameters params); ECDSASignature sign(Sha256Hash input); ECDSASignature sign(Sha256Hash input, @Nullable KeyParameter aesKey); static boolean verify(byte[] data, ECDSASignature signature, byte[] pub); static boolean verify(byte[] data, byte[] signature, byte[] pub); boolean verify(byte[] hash, byte[] signature); boolean verify(Sha256Hash sigHash, ECDSASignature signature); void verifyOrThrow(byte[] hash, byte[] signature); void verifyOrThrow(Sha256Hash sigHash, ECDSASignature signature); static boolean isPubKeyCanonical(byte[] pubkey); String signMessage(String message); String signMessage(String message, @Nullable KeyParameter aesKey); static ECKey signedMessageToKey(String message, String signatureBase64); void verifyMessage(String message, String signatureBase64); @Nullable static ECKey recoverFromSignature(int recId, ECDSASignature sig, Sha256Hash message, boolean compressed); byte[] getPrivKeyBytes(); DumpedPrivateKey getPrivateKeyEncoded(NetworkParameters params); @Override long getCreationTimeSeconds(); void setCreationTimeSeconds(long newCreationTimeSeconds); ECKey encrypt(KeyCrypter keyCrypter, KeyParameter aesKey); ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey); ECKey decrypt(KeyParameter aesKey); ECKey maybeDecrypt(@Nullable KeyParameter aesKey); static boolean encryptionIsReversible(ECKey originalKey, ECKey encryptedKey, KeyCrypter keyCrypter, KeyParameter aesKey); @Override boolean isEncrypted(); @Nullable @Override Protos.Wallet.EncryptionType getEncryptionType(); @Override @Nullable byte[] getSecretBytes(); @Nullable @Override EncryptedData getEncryptedData(); @Nullable EncryptedData getEncryptedPrivateKey(); @Nullable KeyCrypter getKeyCrypter(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); String toStringWithPrivate(NetworkParameters params); String getPrivateKeyAsHex(); String getPublicKeyAsHex(); String getPrivateKeyAsWiF(NetworkParameters params); void formatKeyWithAddress(boolean includePrivateKeys, StringBuilder builder, NetworkParameters params); static final Comparator<ECKey> AGE_COMPARATOR; static final Comparator<ECKey> PUBKEY_COMPARATOR; static final ECDomainParameters CURVE; static final BigInteger HALF_CURVE_ORDER; @VisibleForTesting
    static boolean FAKE_SIGNATURES; }