Search in sources :

Example 6 with PrivateKey

use of io.nem.symbol.core.crypto.PrivateKey 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);
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) PrivateKey(io.nem.symbol.core.crypto.PrivateKey) BlockCipher(io.nem.symbol.core.crypto.BlockCipher) PublicKey(io.nem.symbol.core.crypto.PublicKey) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine)

Aggregations

PrivateKey (io.nem.symbol.core.crypto.PrivateKey)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 PublicKey (io.nem.symbol.core.crypto.PublicKey)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 BlockCipher (io.nem.symbol.core.crypto.BlockCipher)1 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)1 KeyPair (io.nem.symbol.core.crypto.KeyPair)1 Test (org.junit.jupiter.api.Test)1