Search in sources :

Example 6 with BlockCipher

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

BlockCipher (io.nem.symbol.core.crypto.BlockCipher)6 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)6 KeyPair (io.nem.symbol.core.crypto.KeyPair)6 BlockCipherTest (io.nem.symbol.core.crypto.BlockCipherTest)2 CryptoException (io.nem.symbol.core.crypto.CryptoException)2 Test (org.junit.jupiter.api.Test)2 PrivateKey (io.nem.symbol.core.crypto.PrivateKey)1 PublicKey (io.nem.symbol.core.crypto.PublicKey)1