本文整理了Java中org.bitcoinj.crypto.KeyCrypter类的一些代码示例,展示了KeyCrypter类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。KeyCrypter类的具体详情如下:包路径:org.bitcoinj.crypto.KeyCrypter类名称:KeyCrypter
KeyCrypter介绍
[英]A KeyCrypter can be used to encrypt and decrypt a message. The sequence of events to encrypt and then decrypt a message are as follows:
(1) Ask the user for a password. deriveKey() is then called to create an KeyParameter. This contains the AES key that will be used for encryption.
(2) Encrypt the message using encrypt(), providing the message bytes and the KeyParameter from (1). This returns an EncryptedData which contains the encryptedPrivateKey bytes and an initialisation vector.
(3) To decrypt an EncryptedData, repeat step (1) to get a KeyParameter, then call decrypt().
There can be different algorithms used for encryption/ decryption so the getUnderstoodEncryptionType is used to determine whether any given KeyCrypter can understand the type of encrypted data you have.[中]密钥加密器可用于加密和解密消息。要加密然后解密消息的事件序列如下所示:(1) 向用户询问密码。然后调用deriveKey()来创建一个KeyParameter。这包含将用于加密的AES密钥。(2) 使用Encrypt()加密消息,提供(1)中的消息字节和密钥参数。这将返回一个EncryptedData,其中包含encryptedPrivateKey字节和初始化向量。(3) 要解密EncryptedData,请重复步骤(1)以获取KeyParameter,然后调用decrypt()。加密/解密可以使用不同的算法,因此getUnderstoodEncryptionType用于确定任何给定的密钥加密程序是否能够理解您拥有的加密数据的类型。
代码示例
代码示例来源:origin: fr.acinq/bitcoinj-core
/** * Returns whether the given password is correct for this key chain. * @throws IllegalStateException if the chain is not encrypted at all. */@Overridepublic boolean checkPassword(CharSequence password) { checkNotNull(password); checkState(keyCrypter != null, "Key chain not encrypted"); return checkAESKey(keyCrypter.deriveKey(password));}
代码示例来源:origin: HashEngineering/dashj
public DeterministicSeed encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkState(encryptedMnemOnicCode== null, "Trying to encrypt seed twice"); checkState(mnemonicCode != null, "Mnemonic missing so cannot encrypt"); EncryptedData encryptedMnemOnic= keyCrypter.encrypt(getMnemonicAsBytes(), aesKey); EncryptedData encryptedSeed = keyCrypter.encrypt(seed, aesKey); return new DeterministicSeed(encryptedMnemonic, encryptedSeed, creationTimeSeconds);}
代码示例来源:origin: fr.acinq/bitcoinj-core
private BigInteger findOrDeriveEncryptedPrivateKey(KeyCrypter keyCrypter, KeyParameter aesKey) { if (encryptedPrivateKey != null) return new BigInteger(1, keyCrypter.decrypt(encryptedPrivateKey, aesKey)); // Otherwise we don't have it, but maybe we can figure it out from our parents. Walk up the tree looking for // the first key that has some encrypted private key data. DeterministicKey cursor = parent; while (cursor != null) { if (cursor.encryptedPrivateKey != null) break; cursor = cursor.parent; } if (cursor == null) throw new KeyCrypterException("Neither this key nor its parents have an encrypted private key"); byte[] parentalPrivateKeyBytes = keyCrypter.decrypt(cursor.encryptedPrivateKey, aesKey); return derivePrivateKeyDownwards(cursor, parentalPrivateKeyBytes);}
代码示例来源:origin: greenaddress/GreenBits
@Testpublic void testEncryptedCreate() throws Exception { ECKey unencryptedKey = new ECKey(); byte[] originalPrivateKeyBytes = checkNotNull(unencryptedKey.getPrivKeyBytes()); log.info("Original private key = " + Utils.HEX.encode(originalPrivateKeyBytes)); EncryptedData encryptedPrivateKey = keyCrypter.encrypt(unencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1)); ECKey encryptedKey = ECKey.fromEncrypted(encryptedPrivateKey, keyCrypter, unencryptedKey.getPubKey()); assertTrue(encryptedKey.isEncrypted()); assertNull(encryptedKey.getSecretBytes()); ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter.deriveKey(PASSWORD1)); assertTrue(!rebornUnencryptedKey.isEncrypted()); assertArrayEquals(originalPrivateKeyBytes, rebornUnencryptedKey.getPrivKeyBytes());}
代码示例来源:origin: cash.bitcoinj/bitcoinj-core
@Nullable@Overridepublic Protos.Wallet.EncryptionType getEncryptionType() { return keyCrypter != null ? keyCrypter.getUnderstoodEncryptionType() : Protos.Wallet.EncryptionType.UNENCRYPTED;}
代码示例来源:origin: greenaddress/GreenBits
@Testpublic 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()); // The key should be encrypted assertTrue("Key not encrypted at start", encryptedKey.isEncrypted()); // Check that the key can be successfully decrypted back to the original. assertTrue("Key encryption is not reversible but it should be", ECKey.encryptionIsReversible(originalUnencryptedKey, encryptedKey, keyCrypter, keyCrypter.deriveKey(PASSWORD1))); // Check that key encryption is not reversible if a password other than the original is used to generate the AES key. assertTrue("Key encryption is reversible with wrong password", !ECKey.encryptionIsReversible(originalUnencryptedKey, encryptedKey, keyCrypter, keyCrypter.deriveKey(WRONG_PASSWORD))); // Change one of the encrypted key bytes (this is to simulate a faulty keyCrypter). // Encryption should not be reversible byte[] goodEncryptedPrivateKeyBytes = encryptedPrivateKey.encryptedBytes; // Break the encrypted private key and check it is broken. 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)));}
代码示例来源:origin: fr.acinq/bitcoinj-core
@Nullable@Overridepublic Protos.Wallet.EncryptionType getEncryptionType() { return keyCrypter != null ? keyCrypter.getUnderstoodEncryptionType() : Protos.Wallet.EncryptionType.UNENCRYPTED;}
代码示例来源:origin: cash.bitcoinj/bitcoinj-core
@Overridepublic BasicKeyChain toDecrypted(CharSequence password) { checkNotNull(keyCrypter, "Wallet is already decrypted"); KeyParameter aesKey = keyCrypter.deriveKey(password); return toDecrypted(aesKey);}
代码示例来源:origin: greenaddress/GreenBits
public DeterministicSeed encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkState(encryptedMnemOnicCode== null, "Trying to encrypt seed twice"); checkState(mnemonicCode != null, "Mnemonic missing so cannot encrypt"); EncryptedData encryptedMnemOnic= keyCrypter.encrypt(getMnemonicAsBytes(), aesKey); EncryptedData encryptedSeed = keyCrypter.encrypt(seed, aesKey); return new DeterministicSeed(encryptedMnemonic, encryptedSeed, creationTimeSeconds);}
代码示例来源:origin: cash.bitcoinj/bitcoinj-core
private BigInteger findOrDeriveEncryptedPrivateKey(KeyCrypter keyCrypter, KeyParameter aesKey) { if (encryptedPrivateKey != null) return new BigInteger(1, keyCrypter.decrypt(encryptedPrivateKey, aesKey)); // Otherwise we don't have it, but maybe we can figure it out from our parents. Walk up the tree looking for // the first key that has some encrypted private key data. DeterministicKey cursor = parent; while (cursor != null) { if (cursor.encryptedPrivateKey != null) break; cursor = cursor.parent; } if (cursor == null) throw new KeyCrypterException("Neither this key nor its parents have an encrypted private key"); byte[] parentalPrivateKeyBytes = keyCrypter.decrypt(cursor.encryptedPrivateKey, aesKey); return derivePrivateKeyDownwards(cursor, parentalPrivateKeyBytes);}
代码示例来源:origin: HashEngineering/dashj
@Nullable@Overridepublic Protos.Wallet.EncryptionType getEncryptionType() { return keyCrypter != null ? keyCrypter.getUnderstoodEncryptionType() : Protos.Wallet.EncryptionType.UNENCRYPTED;}
代码示例来源:origin: cash.bitcoinj/bitcoinj-core
/** * Returns whether the given password is correct for this key chain. * @throws IllegalStateException if the chain is not encrypted at all. */@Overridepublic boolean checkPassword(CharSequence password) { checkNotNull(password); checkState(keyCrypter != null, "Key chain not encrypted"); return checkAESKey(keyCrypter.deriveKey(password));}
代码示例来源:origin: cash.bitcoinj/bitcoinj-core
public DeterministicSeed encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkState(encryptedMnemOnicCode== null, "Trying to encrypt seed twice"); checkState(mnemonicCode != null, "Mnemonic missing so cannot encrypt"); EncryptedData encryptedMnemOnic= keyCrypter.encrypt(getMnemonicAsBytes(), aesKey); EncryptedData encryptedSeed = keyCrypter.encrypt(seed, aesKey); return new DeterministicSeed(encryptedMnemonic, encryptedSeed, creationTimeSeconds);}
代码示例来源:origin: HashEngineering/dashj
private BigInteger findOrDeriveEncryptedPrivateKey(KeyCrypter keyCrypter, KeyParameter aesKey) { if (encryptedPrivateKey != null) return new BigInteger(1, keyCrypter.decrypt(encryptedPrivateKey, aesKey)); // Otherwise we don't have it, but maybe we can figure it out from our parents. Walk up the tree looking for // the first key that has some encrypted private key data. DeterministicKey cursor = parent; while (cursor != null) { if (cursor.encryptedPrivateKey != null) break; cursor = cursor.parent; } if (cursor == null) throw new KeyCrypterException("Neither this key nor its parents have an encrypted private key"); byte[] parentalPrivateKeyBytes = keyCrypter.decrypt(cursor.encryptedPrivateKey, aesKey); return derivePrivateKeyDownwards(cursor, parentalPrivateKeyBytes);}
代码示例来源:origin: greenaddress/GreenBits
@Nullable@Overridepublic Protos.Wallet.EncryptionType getEncryptionType() { return keyCrypter != null ? keyCrypter.getUnderstoodEncryptionType() : Protos.Wallet.EncryptionType.UNENCRYPTED;}
代码示例来源:origin: fr.acinq/bitcoinj-core
public boolean checkPassword(CharSequence password) { checkState(keyCrypter != null, "Not encrypted"); return checkAESKey(keyCrypter.deriveKey(password));}
代码示例来源:origin: fr.acinq/bitcoinj-core
public DeterministicSeed encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) { checkState(encryptedMnemOnicCode== null, "Trying to encrypt seed twice"); checkState(mnemonicCode != null, "Mnemonic missing so cannot encrypt"); EncryptedData encryptedMnemOnic= keyCrypter.encrypt(getMnemonicAsBytes(), aesKey); EncryptedData encryptedSeed = keyCrypter.encrypt(seed, aesKey); return new DeterministicSeed(encryptedMnemonic, encryptedSeed, creationTimeSeconds);}
代码示例来源:origin: greenaddress/GreenBits
private BigInteger findOrDeriveEncryptedPrivateKey(KeyCrypter keyCrypter, KeyParameter aesKey) { if (encryptedPrivateKey != null) return new BigInteger(1, keyCrypter.decrypt(encryptedPrivateKey, aesKey)); // Otherwise we don't have it, but maybe we can figure it out from our parents. Walk up the tree looking for // the first key that has some encrypted private key data. DeterministicKey cursor = parent; while (cursor != null) { if (cursor.encryptedPrivateKey != null) break; cursor = cursor.parent; } if (cursor == null) throw new KeyCrypterException("Neither this key nor its parents have an encrypted private key"); byte[] parentalPrivateKeyBytes = keyCrypter.decrypt(cursor.encryptedPrivateKey, aesKey); return derivePrivateKeyDownwards(cursor, parentalPrivateKeyBytes);}
代码示例来源:origin: cash.bitcoinj/bitcoinj-core
/** * Get the type of encryption used for this wallet. * * (This is a convenience method - the encryption type is actually stored in the keyCrypter). */public EncryptionType getEncryptionType() { keyChainGroupLock.lock(); try { KeyCrypter crypter = keyChainGroup.getKeyCrypter(); if (crypter != null) return crypter.getUnderstoodEncryptionType(); else return EncryptionType.UNENCRYPTED; } finally { keyChainGroupLock.unlock(); }}
代码示例来源:origin: fr.acinq/bitcoinj-core
@Overridepublic BasicKeyChain toDecrypted(CharSequence password) { checkNotNull(keyCrypter, "Wallet is already decrypted"); KeyParameter aesKey = keyCrypter.deriveKey(password); return toDecrypted(aesKey);}