本文整理汇总了Java中com.google.bitcoin.crypto.EncryptedPrivateKey类的典型用法代码示例。如果您正苦于以下问题:Java EncryptedPrivateKey类的具体用法?Java EncryptedPrivateKey怎么用?Java EncryptedPrivateKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EncryptedPrivateKey类属于com.google.bitcoin.crypto包,在下文中一共展示了EncryptedPrivateKey类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testEncryptionIsReversible
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
@Test
public void testEncryptionIsReversible() throws Exception {
ECKey originalUnencryptedKey = new ECKey();
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(originalUnencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1));
ECKey encryptedKey = new ECKey(encryptedPrivateKey, originalUnencryptedKey.getPubKey(), keyCrypter);
// 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.getEncryptedBytes();
// Break the encrypted private key and check it is broken.
byte[] badEncryptedPrivateKeyBytes = new byte[goodEncryptedPrivateKeyBytes.length];
encryptedPrivateKey.setEncryptedPrivateBytes(badEncryptedPrivateKeyBytes);
ECKey badEncryptedKey = new ECKey(encryptedPrivateKey, originalUnencryptedKey.getPubKey(), keyCrypter);
assertTrue("Key encryption is reversible with faulty encrypted bytes", !ECKey.encryptionIsReversible(originalUnencryptedKey, badEncryptedKey, keyCrypter, keyCrypter.deriveKey(PASSWORD1)));
}
示例2: testEncryptedCreate
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
@Test
public void testEncryptedCreate() throws Exception {
ECKey unencryptedKey = new ECKey();
byte[] originalPrivateKeyBytes = checkNotNull(unencryptedKey.getPrivKeyBytes());
log.info("Original private key = " + Utils.bytesToHexString(originalPrivateKeyBytes));
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(unencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1));
ECKey encryptedKey = new ECKey(encryptedPrivateKey, unencryptedKey.getPubKey(), keyCrypter);
// The key should initially be encrypted
assertTrue("Key not encrypted at start", encryptedKey.isEncrypted());
// The unencrypted private key bytes of the encrypted keychain should all be blank.
byte[] privateKeyBytes = encryptedKey.getPrivKeyBytes();
if (privateKeyBytes != null) {
for (int i = 0; i < privateKeyBytes.length; i++) {
assertEquals("Byte " + i + " of the private key was not zero but should be", 0, privateKeyBytes[i]);
}
}
// Decrypt the key.
ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
assertTrue(!rebornUnencryptedKey.isEncrypted());
assertArrayEquals(originalPrivateKeyBytes, rebornUnencryptedKey.getPrivKeyBytes());
}
示例3: getEncryptedPrivateKey
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
/**
* @return The encryptedPrivateKey (containing the encrypted private key bytes and initialisation vector) for this ECKey,
* or null if the ECKey is not encrypted.
*/
@Nullable
public EncryptedPrivateKey getEncryptedPrivateKey() {
if (encryptedPrivateKey == null) {
return null;
} else {
return encryptedPrivateKey.clone();
}
}
示例4: testEncryptedCreate
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
@Test
public void testEncryptedCreate() throws Exception {
ECKey unencryptedKey = new ECKey();
// Copy the private key bytes for checking later.
byte[] originalPrivateKeyBytes = new byte[32];
System.arraycopy(unencryptedKey.getPrivKeyBytes(), 0, originalPrivateKeyBytes, 0, 32);
log.info("Original private key = " + Utils.bytesToHexString(originalPrivateKeyBytes));
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(unencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1));
ECKey encryptedKey = new ECKey(encryptedPrivateKey, unencryptedKey.getPubKey(), keyCrypter);
// The key should initially be encrypted
assertTrue("Key not encrypted at start", encryptedKey.isEncrypted());
// The unencrypted private key bytes of the encrypted keychain should all be blank.
byte[] privateKeyBytes = encryptedKey.getPrivKeyBytes();
if (privateKeyBytes != null) {
for (int i = 0; i < privateKeyBytes.length; i++) {
assertEquals("Byte " + i + " of the private key was not zero but should be", 0, privateKeyBytes[i]);
}
}
// Decrypt the key.
ECKey rebornUnencryptedKey = encryptedKey.decrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
// The key should be unencrypted
assertTrue("Key is not unencrypted but it should be", !rebornUnencryptedKey.isEncrypted());
// The reborn unencrypted private key bytes should match the original private key.
privateKeyBytes = rebornUnencryptedKey.getPrivKeyBytes();
log.info("Reborn decrypted private key = " + Utils.bytesToHexString(privateKeyBytes));
for (int i = 0; i < privateKeyBytes.length; i++) {
assertEquals("Byte " + i + " of the private key did not match the original", originalPrivateKeyBytes[i], privateKeyBytes[i]);
}
}
示例5: encrypt
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
/**
* Create an encrypted private key with the keyCrypter and the AES key supplied.
* This method returns a new encrypted key and leaves the original unchanged.
* To be secure you need to clear the original, unencrypted private key bytes.
*
* @param keyCrypter The keyCrypter that specifies exactly how the encrypted bytes are created.
* @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached as it is slow to create).
* @return encryptedKey
*/
public ECKey encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException {
Preconditions.checkNotNull(keyCrypter);
final byte[] privKeyBytes = getPrivKeyBytes();
checkState(privKeyBytes != null, "Private key is not available");
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey);
ECKey result = new ECKey(encryptedPrivateKey, getPubKey(), keyCrypter);
result.setCreationTimeSeconds(creationTimeSeconds);
return result;
}
示例6: getEncryptedPrivateKey
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
/**
* @return The encryptedPrivateKey (containing the encrypted private key bytes and initialisation vector) for this ECKey,
* or null if the ECKey is not encrypted.
*/
public EncryptedPrivateKey getEncryptedPrivateKey() {
if (encryptedPrivateKey == null) {
return null;
} else {
return encryptedPrivateKey.clone();
}
}
示例7: testEncryptionIsReversible
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
@Test
public void testEncryptionIsReversible() throws Exception {
ECKey originalUnencryptedKey = new ECKey();
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(originalUnencryptedKey.getPrivKeyBytes(), keyCrypter.deriveKey(PASSWORD1));
ECKey encryptedKey = new ECKey(encryptedPrivateKey, originalUnencryptedKey.getPubKey(), keyCrypter);
// 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.getEncryptedBytes();
// Break the encrypted private key and check it is broken.
byte[] badEncryptedPrivateKeyBytes = goodEncryptedPrivateKeyBytes;
// XOR the 16th byte with 0x0A (this is fairly arbitary) to break it.
badEncryptedPrivateKeyBytes[16] = (byte) (badEncryptedPrivateKeyBytes[12] ^ new Byte("12").byteValue());
encryptedPrivateKey.setEncryptedPrivateBytes(badEncryptedPrivateKeyBytes);
ECKey badEncryptedKey = new ECKey(encryptedPrivateKey, originalUnencryptedKey.getPubKey(), keyCrypter);
assertTrue("Key encryption is reversible with faulty encrypted bytes", !ECKey.encryptionIsReversible(originalUnencryptedKey, badEncryptedKey, keyCrypter, keyCrypter.deriveKey(PASSWORD1)));
}
示例8: ECKey
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
/**
* Create a new ECKey with an encrypted private key, a public key and a KeyCrypter.
*
* @param encryptedPrivateKey The private key, encrypted,
* @param pubKey The keys public key
* @param keyCrypter The KeyCrypter that will be used, with an AES key, to encrypt and decrypt the private key
*/
public ECKey(@Nullable EncryptedPrivateKey encryptedPrivateKey, @Nullable byte[] pubKey, KeyCrypter keyCrypter) {
this((byte[])null, pubKey);
this.keyCrypter = Preconditions.checkNotNull(keyCrypter);
this.encryptedPrivateKey = encryptedPrivateKey;
}
示例9: encrypt
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
/**
* Create an encrypted private key with the keyCrypter and the AES key supplied.
* This method returns a new encrypted key and leaves the original unchanged.
* To be secure you need to clear the original, unencrypted private key bytes.
*
* @param keyCrypter The keyCrypter that specifies exactly how the encrypted bytes are created.
* @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached as it is slow to create).
* @return encryptedKey
*/
public ECKey encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException {
Preconditions.checkNotNull(keyCrypter);
final byte[] privKeyBytes = getPrivKeyBytes();
checkState(privKeyBytes != null, "Private key is not available");
EncryptedPrivateKey encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey);
return new ECKey(encryptedPrivateKey, getPubKey(), keyCrypter);
}
示例10: ECKey
import com.google.bitcoin.crypto.EncryptedPrivateKey; //导入依赖的package包/类
/**
* Create a new ECKey with an encrypted private key, a public key and a KeyCrypter.
*
* @param encryptedPrivateKey The private key, encrypted,
* @param pubKey The keys public key
* @param keyCrypter The KeyCrypter that will be used, with an AES key, to encrypt and decrypt the private key
*/
public ECKey(@Nullable EncryptedPrivateKey encryptedPrivateKey, @Nullable byte[] pubKey, KeyCrypter keyCrypter) {
this((byte[])null, pubKey);
this.keyCrypter = Preconditions.checkNotNull(keyCrypter);
this.encryptedPrivateKey = encryptedPrivateKey;
}