Search in sources :

Example 11 with CryptoEngine

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());
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) BlockCipher(io.nem.symbol.core.crypto.BlockCipher) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) CryptoException(io.nem.symbol.core.crypto.CryptoException) Test(org.junit.jupiter.api.Test) BlockCipherTest(io.nem.symbol.core.crypto.BlockCipherTest)

Example 12 with CryptoEngine

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

Example 13 with CryptoEngine

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);
}
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

CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)13 KeyPair (io.nem.symbol.core.crypto.KeyPair)13 Test (org.junit.jupiter.api.Test)9 DsaSignerTest (io.nem.symbol.core.crypto.DsaSignerTest)7 Signature (io.nem.symbol.core.crypto.Signature)7 BlockCipher (io.nem.symbol.core.crypto.BlockCipher)6 DsaSigner (io.nem.symbol.core.crypto.DsaSigner)6 BigInteger (java.math.BigInteger)3 BlockCipherTest (io.nem.symbol.core.crypto.BlockCipherTest)2 CryptoException (io.nem.symbol.core.crypto.CryptoException)2 PublicKey (io.nem.symbol.core.crypto.PublicKey)2 PrivateKey (io.nem.symbol.core.crypto.PrivateKey)1