use of io.nem.symbol.core.crypto.PublicKey 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