本文整理汇总了Java中org.spongycastle.crypto.params.KeyParameter类的典型用法代码示例。如果您正苦于以下问题:Java KeyParameter类的具体用法?Java KeyParameter怎么用?Java KeyParameter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
KeyParameter类属于org.spongycastle.crypto.params包,在下文中一共展示了KeyParameter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decrypt
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
/**
* Decrypt bytes previously encrypted with this class.
*
* @param dataToDecrypt The data to decrypt
* @param key The AES key to use for decryption
* @return The decrypted bytes
* @throws KeyCrypterException if bytes could not be decrypted
*/
@Override
public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter key) {
checkNotNull(dataToDecrypt);
checkNotNull(key);
try {
ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(key.getKey()), dataToDecrypt.initialisationVector);
// Decrypt the message.
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
cipher.init(false, keyWithIv);
byte[] cipherBytes = dataToDecrypt.encryptedBytes;
byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
final int length2 = cipher.doFinal(decryptedBytes, length1);
return Arrays.copyOf(decryptedBytes, length1 + length2);
} catch (Exception e) {
throw new KeyCrypterException("Could not decrypt bytes", e);
}
}
示例2: encryptAES
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
private byte[] encryptAES(byte[] privateKeyBytes, byte[] passphrase) {
KeyCrypterAes keyCrypter = new KeyCrypterAes();
KeyParameter keyParameter = new KeyParameter(Sha256Hash.hash(passphrase));
EncryptedData enc = keyCrypter.encrypt(privateKeyBytes, keyParameter);
try {
ByteArrayOutputStream encryptedResult = new ByteArrayOutputStream();
ObjectOutputStream byteStream = new ObjectOutputStream(encryptedResult);
ArrayList<byte[]> bytes = new ArrayList<>();
bytes.add(enc.encryptedBytes);
bytes.add(enc.initialisationVector);
byteStream.writeObject(bytes);
return encryptedResult.toByteArray();
} catch (IOException e) {
//How is this even possible ???
throw new IllegalStateException(e);
}
}
示例3: rotate
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
private static void rotate() throws BlockStoreException {
setup();
peers.start();
// Set a key rotation time and possibly broadcast the resulting maintenance transactions.
long rotationTimeSecs = Utils.currentTimeSeconds();
if (options.has(dateFlag)) {
rotationTimeSecs = options.valueOf(dateFlag).getTime() / 1000;
} else if (options.has(unixtimeFlag)) {
rotationTimeSecs = options.valueOf(unixtimeFlag);
}
log.info("Setting wallet key rotation time to {}", rotationTimeSecs);
wallet.setKeyRotationTime(rotationTimeSecs);
KeyParameter aesKey = null;
if (wallet.isEncrypted()) {
aesKey = passwordToKey(true);
if (aesKey == null)
return;
}
Futures.getUnchecked(wallet.doMaintenance(aesKey, true));
}
示例4: addKey
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
private static void addKey() {
// If we're being given precise details, we have to import the key.
if (options.has("privkey") || options.has("pubkey")) {
importKey();
} else {
if (options.has(lookaheadSize)) {
Integer size = options.valueOf(lookaheadSize);
log.info("Setting keychain lookahead size to {}", size);
wallet.setKeyChainGroupLookaheadSize(size);
}
ECKey key;
try {
key = wallet.freshReceiveKey();
} catch (DeterministicUpgradeRequiredException e) {
try {
KeyParameter aesKey = passwordToKey(false);
wallet.upgradeToDeterministic(aesKey);
} catch (DeterministicUpgradeRequiresPassword e2) {
System.err.println("This wallet must be upgraded to be deterministic, but it's encrypted: please supply the password and try again.");
return;
}
key = wallet.freshReceiveKey();
}
System.out.println(key.toAddress(params) + " " + key);
}
}
示例5: decrypt
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
/**
* Create a decrypted private key with the keyCrypter and AES key supplied.
* Note that if the aesKey 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 keyCrypter
* The keyCrypter that specifies exactly how the decrypted bytes
* are created.
* @param aesKey
* The KeyParameter with the AES encryption key (usually
* constructed with keyCrypter#deriveKey and cached).
*/
public ECKey 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");
checkState(encryptedPrivateKey != null, "This key is not encrypted");
byte[] unencryptedPrivateKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey);
ECKey key = ECKey.fromPrivate(unencryptedPrivateKey);
if (!isCompressed())
key = key.decompress();
if (!Arrays.equals(key.getPubKey(), getPubKey()))
throw new KeyCrypterException("Provided AES key is wrong");
key.setCreationTimeSeconds(creationTimeSeconds);
return key;
}
示例6: initCiphers
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
private void initCiphers(final byte[] key, final byte[] iv) {
// get the keyBytes
this.keyBytes = new byte[key.length];
System.arraycopy(key, 0, this.keyBytes, 0, key.length);
this.keyP = new KeyParameter(this.keyBytes);
// get the IV
this.IV = new byte[blockSize];
System.arraycopy(iv, 0, this.IV, 0, this.IV.length);
// create the ciphers
// AES block cipher in CBC mode with ISO7816d4 padding
this.encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESFastEngine()), new ISO7816d4Padding());
this.decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(
new AESFastEngine()), new ISO7816d4Padding());
// create the IV parameter
final ParametersWithIV parameterIV = new ParametersWithIV(this.keyP, this.IV);
this.encryptCipher.init(true, parameterIV);
this.decryptCipher.init(false, parameterIV);
}
示例7: toDecrypted
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
@Override
public BasicKeyChain toDecrypted(KeyParameter aesKey) {
lock.lock();
try {
checkState(keyCrypter != null, "Wallet is already decrypted");
// Do an up-front check.
if (numKeys() > 0 && !checkAESKey(aesKey))
throw new KeyCrypterException("Password/key was incorrect.");
BasicKeyChain decrypted = new BasicKeyChain();
for (ECKey key : hashToKeys.values()) {
decrypted.importKeyLocked(key.decrypt(aesKey));
}
return decrypted;
} finally {
lock.unlock();
}
}
示例8: shouldSendTimeWindowInClientVersion
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
@Test
public void shouldSendTimeWindowInClientVersion() throws Exception {
final long timeWindow = 4000;
KeyParameter userKey = null;
PaymentChannelClient dut =
new PaymentChannelClient(wallet, ecKey, maxValue, serverHash, userKey, new PaymentChannelClient.DefaultClientChannelProperties() {
@Override
public long timeWindow() {
return timeWindow;
}
@Override
public PaymentChannelClient.VersionSelector versionSelector() {
return clientChannelProperties.versionSelector();
}
}, connection);
connection.sendToServer(capture(clientVersionCapture));
EasyMock.expect(wallet.getExtensions()).andReturn(new HashMap<String, WalletExtension>());
replay(connection, wallet);
dut.connectionOpen();
assertClientVersion(4000);
}
示例9: toEncrypted
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
/**
* Encrypt the wallet using the KeyCrypter and the AES key. A good default KeyCrypter to use is
* {@link org.bitcoinj.crypto.KeyCrypterScrypt}.
*
* @param keyCrypter The KeyCrypter that specifies how to encrypt/ decrypt a key
* @param aesKey AES key to use (normally created using KeyCrypter#deriveKey and cached as it is time consuming
* to create from a password)
* @throws KeyCrypterException Thrown if the wallet encryption fails. If so, the wallet state is unchanged.
*/
@Override
public BasicKeyChain toEncrypted(KeyCrypter keyCrypter, KeyParameter aesKey) {
lock.lock();
try {
checkNotNull(keyCrypter);
checkState(this.keyCrypter == null, "Key chain is already encrypted");
BasicKeyChain encrypted = new BasicKeyChain(keyCrypter);
for (ECKey key : hashToKeys.values()) {
ECKey encryptedKey = key.encrypt(keyCrypter, aesKey);
// Check that the encrypted key can be successfully decrypted.
// This is done as it is a critical failure if the private key cannot be decrypted successfully
// (all bitcoin controlled by that private key is lost forever).
// For a correctly constructed keyCrypter the encryption should always be reversible so it is just
// being as cautious as possible.
if (!ECKey.encryptionIsReversible(key, encryptedKey, keyCrypter, aesKey))
throw new KeyCrypterException("The key " + key.toString() + " cannot be successfully decrypted after encryption so aborting wallet encryption.");
encrypted.importKeyLocked(encryptedKey);
}
return encrypted;
} finally {
lock.unlock();
}
}
示例10: spendUnconfirmedChange
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
private Wallet spendUnconfirmedChange(Wallet wallet, Transaction t2, KeyParameter aesKey) throws Exception {
if (wallet.getTransactionSigners().size() == 1) // don't bother reconfiguring the p2sh wallet
wallet = roundTrip(wallet);
Coin v3 = valueOf(0, 50);
assertEquals(v3, wallet.getBalance());
SendRequest req = SendRequest.to(OTHER_ADDRESS, valueOf(0, 48));
req.aesKey = aesKey;
req.shuffleOutputs = false;
wallet.completeTx(req);
Transaction t3 = req.tx;
assertNotEquals(t2.getOutput(1).getScriptPubKey().getToAddress(PARAMS),
t3.getOutput(1).getScriptPubKey().getToAddress(PARAMS));
assertNotNull(t3);
wallet.commitTx(t3);
assertTrue(wallet.isConsistent());
// t2 and t3 gets confirmed in the same block.
sendMoneyToWallet(AbstractBlockChain.NewBlockType.BEST_CHAIN, t2, t3);
assertTrue(wallet.isConsistent());
return wallet;
}
示例11: encryptionDecryptionBadPassword
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
@Test
public void encryptionDecryptionBadPassword() throws Exception {
Wallet encryptedWallet = new Wallet(PARAMS);
encryptedWallet.encrypt(PASSWORD1);
KeyCrypter keyCrypter = encryptedWallet.getKeyCrypter();
KeyParameter wrongAesKey = keyCrypter.deriveKey(WRONG_PASSWORD);
// Check the wallet is currently encrypted
assertEquals("Wallet is not an encrypted wallet", EncryptionType.ENCRYPTED_SCRYPT_AES, encryptedWallet.getEncryptionType());
assertFalse(encryptedWallet.checkAESKey(wrongAesKey));
// Check that the wrong password does not decrypt the wallet.
try {
encryptedWallet.decrypt(wrongAesKey);
fail("Incorrectly decoded wallet with wrong password");
} catch (KeyCrypterException ede) {
// Expected.
}
}
示例12: encryptionIsReversible
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
/**
* <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>
*
* @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;
}
}
示例13: toString
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
public String toString(boolean includePrivateKeys, @Nullable KeyParameter aesKey, NetworkParameters params) {
final DeterministicKey watchingKey = getWatchingKey();
final StringBuilder builder = new StringBuilder();
if (seed != null) {
if (includePrivateKeys) {
DeterministicSeed decryptedSeed = seed.isEncrypted()
? seed.decrypt(getKeyCrypter(), DEFAULT_PASSPHRASE_FOR_MNEMONIC, aesKey) : seed;
final List<String> words = decryptedSeed.getMnemonicCode();
builder.append("Seed as words: ").append(Utils.SPACE_JOINER.join(words)).append('\n');
builder.append("Seed as hex: ").append(decryptedSeed.toHexString()).append('\n');
} else {
if (seed.isEncrypted())
builder.append("Seed is encrypted\n");
}
builder.append("Seed birthday: ").append(seed.getCreationTimeSeconds()).append(" [")
.append(Utils.dateTimeFormat(seed.getCreationTimeSeconds() * 1000)).append("]\n");
} else {
builder.append("Key birthday: ").append(watchingKey.getCreationTimeSeconds()).append(" [")
.append(Utils.dateTimeFormat(watchingKey.getCreationTimeSeconds() * 1000)).append("]\n");
}
builder.append("Key to watch: ").append(watchingKey.serializePubB58(params)).append('\n');
formatAddresses(includePrivateKeys, aesKey, params, builder);
return builder.toString();
}
示例14: handleGo
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
private void handleGo() {
state = State.DECRYPTING;
updateView();
if (wallet.isEncrypted()) {
new DeriveKeyTask(backgroundHandler, application.scryptIterationsTarget()) {
@Override
protected void onSuccess(final KeyParameter encryptionKey, final boolean wasChanged) {
if (wasChanged)
application.backupWallet();
doRaiseFee(encryptionKey);
}
}.deriveKey(wallet, passwordView.getText().toString().trim());
updateView();
} else {
doRaiseFee(null);
}
}
示例15: FrameCodec
import org.spongycastle.crypto.params.KeyParameter; //导入依赖的package包/类
public FrameCodec(EncryptionHandshake.Secrets secrets) {
this.mac = secrets.mac;
int blockSize = secrets.aes.length * 8;
enc = new SICBlockCipher(new AESFastEngine());
enc.init(true, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[blockSize / 8]));
dec = new SICBlockCipher(new AESFastEngine());
dec.init(false, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[blockSize / 8]));
egressMac = secrets.egressMac;
ingressMac = secrets.ingressMac;
}