use of io.nem.symbol.core.crypto.CryptoEngine in project nem2-sdk-java by nemtech.
the class Ed25519BlockCipherTest method decryptFailsIfInputIsNull.
@Test
public void decryptFailsIfInputIsNull() {
// Arrange:
final CryptoEngine engine = this.getCryptoEngine();
final KeyPair kp = KeyPair.random(engine);
final BlockCipher blockCipher = this.getBlockCipher(kp, kp);
// Act:
CryptoException exception = Assertions.assertThrows(CryptoException.class, () -> blockCipher.decrypt(null));
// Assert:
Assertions.assertEquals("Cannot decrypt. Input is required.", exception.getMessage());
}
use of io.nem.symbol.core.crypto.CryptoEngine in project nem2-sdk-java by nemtech.
the class PersistentHarvestingDelegationMessage method create.
/**
* Helper constructor that allow users to create an encrypted Persistent Harvesting Delegation
* Message
*
* <p>Note, the strategy to encrypt and decrypt should be shared between the different SDKs. A
* client may send a transaction using a sdk and the recipient may be using a different one.
*
* <p>The strategy is:
*
* <p>"plain text" string - utf8 byte array - encrypted byte array - hex string (the encrypted
* message string)
*
* @param signingPrivateKey Remote harvester signing private key linked to the main account
* @param vrfPrivateKey VRF private key linked to the main account
* @param nodePublicKey Recipient public key
* @return {@link PersistentHarvestingDelegationMessage}
*/
public static PersistentHarvestingDelegationMessage create(PrivateKey signingPrivateKey, PrivateKey vrfPrivateKey, PublicKey nodePublicKey) {
KeyPair ephemeralKeyPair = KeyPair.random();
CryptoEngine engine = CryptoEngines.defaultEngine();
KeyPair recipient = KeyPair.onlyPublic(nodePublicKey, engine);
BlockCipher blockCipher = engine.createBlockCipher(ephemeralKeyPair, recipient);
byte[] encrypted = blockCipher.encrypt(ArrayUtils.concat(signingPrivateKey.getBytes(), vrfPrivateKey.getBytes()));
String encryptedHex = ConvertUtils.toHex(encrypted);
String payload = MessageMarker.PERSISTENT_DELEGATION_UNLOCK + ephemeralKeyPair.getPublicKey().toHex() + encryptedHex;
return new PersistentHarvestingDelegationMessage(payload.toUpperCase());
}
use of io.nem.symbol.core.crypto.CryptoEngine in project nem2-sdk-java by nemtech.
the class PersistentHarvestingDelegationMessage method decryptPayload.
/**
* Utility method that allow users to decrypt a message if it was created using the Java SDK or
* the Typescript SDK.
*
* @param recipientPrivateKey Recipient private key
* @return the 2 private keys
*/
public HarvestingKeys decryptPayload(PrivateKey recipientPrivateKey) {
int markerLength = MessageMarker.PERSISTENT_DELEGATION_UNLOCK.length();
int publicKeyHexSize = PublicKey.SIZE * 2;
PublicKey senderPublicKey = PublicKey.fromHexString(getText().substring(markerLength, markerLength + publicKeyHexSize));
String encryptedPayload = getText().substring(markerLength + publicKeyHexSize);
CryptoEngine engine = CryptoEngines.defaultEngine();
KeyPair sender = KeyPair.onlyPublic(senderPublicKey, engine);
KeyPair recipient = KeyPair.fromPrivate(recipientPrivateKey);
BlockCipher blockCipher = engine.createBlockCipher(sender, recipient);
byte[] decryptPayload = blockCipher.decrypt(ConvertUtils.fromHexToBytes(encryptedPayload));
String doubleKey = ConvertUtils.toHex(decryptPayload);
PrivateKey signingPrivateKey = PrivateKey.fromHexString(doubleKey.substring(0, publicKeyHexSize));
PrivateKey vrfPrivateKey = PrivateKey.fromHexString(doubleKey.substring(publicKeyHexSize));
return new HarvestingKeys(signingPrivateKey, vrfPrivateKey);
}
Aggregations