/** * <p>Check that it is possible to decrypt the key with the keyCrypter and that the original key is returned.</p> * * <p>Because it is a critical failure if the private keys cannot be decrypted successfully (resulting of loss of all * bitcoins controlled by the private key) you can use this method to check when you *encrypt* a wallet that * it can definitely be decrypted successfully.</p> * * <p>See {@link Wallet#encrypt(KeyCrypter keyCrypter, KeyParameter aesKey)} for example usage.</p> * * @return true if the encrypted key can be decrypted back to the original key successfully. */ 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; } }
private void checkKeyEncryptionStateMatches(ECKey key) { if (keyCrypter == null && key.isEncrypted()) throw new KeyCrypterException("Key is encrypted but chain is not"); else if (keyCrypter != null && !key.isEncrypted()) throw new KeyCrypterException("Key is not encrypted but chain is"); else if (keyCrypter != null && key.getKeyCrypter() != null && !key.getKeyCrypter().equals(keyCrypter)) throw new KeyCrypterException("Key encrypted under different parameters to chain"); }
private void checkKeyEncryptionStateMatches(ECKey key) { if (keyCrypter == null && key.isEncrypted()) throw new KeyCrypterException("Key is encrypted but chain is not"); else if (keyCrypter != null && !key.isEncrypted()) throw new KeyCrypterException("Key is not encrypted but chain is"); else if (keyCrypter != null && key.getKeyCrypter() != null && !key.getKeyCrypter().equals(keyCrypter)) throw new KeyCrypterException("Key encrypted under different parameters to chain"); }
/** * <p>Check that it is possible to decrypt the key with the keyCrypter and that the original key is returned.</p> * * <p>Because it is a critical failure if the private keys cannot be decrypted successfully (resulting of loss of all * bitcoins controlled by the private key) you can use this method to check when you *encrypt* a wallet that * it can definitely be decrypted successfully.</p> * * <p>See {@link Wallet#encrypt(KeyCrypter keyCrypter, KeyParameter aesKey)} for example usage.</p> * * @return true if the encrypted key can be decrypted back to the original key successfully. */ 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; } }
private void checkKeyEncryptionStateMatches(ECKey key) { if (keyCrypter == null && key.isEncrypted()) throw new KeyCrypterException("Key is encrypted but chain is not"); else if (keyCrypter != null && !key.isEncrypted()) throw new KeyCrypterException("Key is not encrypted but chain is"); else if (keyCrypter != null && key.getKeyCrypter() != null && !key.getKeyCrypter().equals(keyCrypter)) throw new KeyCrypterException("Key encrypted under different parameters to chain"); }
/** * <p>Check that it is possible to decrypt the key with the keyCrypter and that the original key is returned.</p> * * <p>Because it is a critical failure if the private keys cannot be decrypted successfully (resulting of loss of all * bitcoins controlled by the private key) you can use this method to check when you *encrypt* a wallet that * it can definitely be decrypted successfully.</p> * * <p>See {@link Wallet#encrypt(KeyCrypter keyCrypter, KeyParameter aesKey)} for example usage.</p> * * @return true if the encrypted key can be decrypted back to the original key successfully. */ 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; } }
private void checkKeyEncryptionStateMatches(ECKey key) { if (keyCrypter == null && key.isEncrypted()) throw new KeyCrypterException("Key is encrypted but chain is not"); else if (keyCrypter != null && !key.isEncrypted()) throw new KeyCrypterException("Key is not encrypted but chain is"); else if (keyCrypter != null && key.getKeyCrypter() != null && !key.getKeyCrypter().equals(keyCrypter)) throw new KeyCrypterException("Key encrypted under different parameters to chain"); }
/** * <p>Check that it is possible to decrypt the key with the keyCrypter and that the original key is returned.</p> * * <p>Because it is a critical failure if the private keys cannot be decrypted successfully (resulting of loss of all * bitcoins controlled by the private key) you can use this method to check when you *encrypt* a wallet that * it can definitely be decrypted successfully.</p> * * <p>See {@link Wallet#encrypt(KeyCrypter keyCrypter, KeyParameter aesKey)} for example usage.</p> * * @return true if the encrypted key can be decrypted back to the original key successfully. */ 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; } }
/** * Create a decrypted private key with AES key. Note that if the AES key is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyParameter aesKey) throws KeyCrypterException { final KeyCrypter crypter = getKeyCrypter(); if (crypter == null) throw new KeyCrypterException("No key crypter available"); return decrypt(crypter, aesKey); }
private void tryDecrypt(DeterministicKey masterKey, CharSequence password, UnlockResult result) { KeyCrypter crypter = checkNotNull(masterKey.getKeyCrypter()); KeyParameter k = crypter.deriveKey(password); try { result.inputFingerprint = getFingerprint(password.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { /* Should not happen */ } result.keyFingerprint = getFingerprint(k.getKey()); if (crypter instanceof KeyCrypterScrypt) { result.scryptParams = ((KeyCrypterScrypt) crypter).getScryptParameters(); } try { masterKey.decrypt(crypter, k); result.isUnlockSuccess = true; } catch (KeyCrypterException e) { result.isUnlockSuccess = false; result.error = e.getMessage(); } }
/** * Create a decrypted private key with AES key. Note that if the AES key is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyParameter aesKey) throws KeyCrypterException { final KeyCrypter crypter = getKeyCrypter(); if (crypter == null) throw new KeyCrypterException("No key crypter available"); return decrypt(crypter, aesKey); }
private void tryDecrypt(DeterministicKey masterKey, CharSequence password, UnlockResult result) { KeyCrypter crypter = checkNotNull(masterKey.getKeyCrypter()); KeyParameter k = crypter.deriveKey(password); try { result.inputFingerprint = getFingerprint(password.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { /* Should not happen */ } result.keyFingerprint = getFingerprint(k.getKey()); if (crypter instanceof KeyCrypterScrypt) { result.scryptParams = ((KeyCrypterScrypt) crypter).getScryptParameters(); } try { masterKey.decrypt(crypter, k); result.isUnlockSuccess = true; } catch (KeyCrypterException e) { result.isUnlockSuccess = false; result.error = e.getMessage(); } }
/** * Create a decrypted private key with AES key. Note that if the AES key is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyParameter aesKey) throws KeyCrypterException { final KeyCrypter crypter = getKeyCrypter(); if (crypter == null) throw new KeyCrypterException("No key crypter available"); return decrypt(crypter, aesKey); }
System.out.println(key.toAddress(params) + " " + key); } catch (KeyCrypterException kce) { System.err.println("There was an encryption related error when adding the key. The error was '" + kce.getMessage() + "'.");
/** * Create a decrypted private key with AES key. Note that if the AES key is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyParameter aesKey) throws KeyCrypterException { final KeyCrypter crypter = getKeyCrypter(); if (crypter == null) throw new KeyCrypterException("No key crypter available"); return decrypt(crypter, aesKey); }
@Test public void testKeyCrypterWrongPassword() throws KeyCrypterException { KeyCrypterScrypt keyCrypter = new KeyCrypterScrypt(scryptParameters); // create a longer encryption string StringBuilder builder = new StringBuilder(); for (int i = 0; i < 100; i++) { builder.append(i).append(" The quick brown fox"); } EncryptedData data = keyCrypter.encrypt(builder.toString().getBytes(), keyCrypter.deriveKey(PASSWORD2)); assertNotNull(data); try { keyCrypter.decrypt(data, keyCrypter.deriveKey(WRONG_PASSWORD)); // TODO: This test sometimes fails due to relying on padding. fail("Decrypt with wrong password did not throw exception"); } catch (KeyCrypterException ede) { assertTrue(ede.getMessage().contains("Could not decrypt")); } }
@Override public DeterministicKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); BigInteger privKey = findOrDeriveEncryptedPrivateKey(keyCrypter, aesKey); DeterministicKey key = new DeterministicKey(childNumberPath, chainCode, privKey, parent); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); if (parent == null) key.setCreationTimeSeconds(getCreationTimeSeconds()); return key; }
fail("No exception was thrown trying to sign an encrypted key with the wrong password supplied."); } catch (KeyCrypterException kce) { assertEquals("Could not decrypt bytes", kce.getMessage());
@Override public DeterministicKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); BigInteger privKey = findOrDeriveEncryptedPrivateKey(keyCrypter, aesKey); DeterministicKey key = new DeterministicKey(childNumberPath, chainCode, privKey, parent); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); if (parent == null) key.setCreationTimeSeconds(getCreationTimeSeconds()); return key; }
@Override public DeterministicKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); BigInteger privKey = findOrDeriveEncryptedPrivateKey(keyCrypter, aesKey); DeterministicKey key = new DeterministicKey(childNumberPath, chainCode, privKey, parent); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); if (parent == null) key.setCreationTimeSeconds(getCreationTimeSeconds()); return key; }