Search in sources :

Example 1 with BlockCipher

use of io.nem.symbol.core.crypto.BlockCipher in project nem2-sdk-java by nemtech.

the class EncryptedMessage method create.

/**
 * Helper constructor that allow users to easily encrypt a message using the SDK provided {@link
 * CryptoEngine} and {@link BlockCipher}.
 *
 * <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 plainTextMessage Plain message to be encrypted
 * @param senderPrivateKey Sender private key
 * @param recipientPublicKey Recipient public key
 * @return EncryptedMessage
 */
public static EncryptedMessage create(String plainTextMessage, PrivateKey senderPrivateKey, PublicKey recipientPublicKey) {
    CryptoEngine engine = CryptoEngines.defaultEngine();
    KeyPair sender = KeyPair.fromPrivate(senderPrivateKey);
    KeyPair recipient = KeyPair.onlyPublic(recipientPublicKey, engine);
    BlockCipher blockCipher = engine.createBlockCipher(sender, recipient);
    return new EncryptedMessage(ConvertUtils.toHex(blockCipher.encrypt(StringEncoder.getBytes(plainTextMessage))));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) BlockCipher(io.nem.symbol.core.crypto.BlockCipher) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine)

Example 2 with BlockCipher

use of io.nem.symbol.core.crypto.BlockCipher in project nem2-sdk-java by nemtech.

the class EncryptedMessage method decryptPayload.

/**
 * Utility method that allow users to decrypt a message if it was created using the Java SDK or
 * the Typescript SDK.
 *
 * @param senderPublicKey Sender public key.
 * @param recipientPrivateKey Recipient private key
 * @return plain string message.
 */
public String decryptPayload(PublicKey senderPublicKey, PrivateKey recipientPrivateKey) {
    CryptoEngine engine = CryptoEngines.defaultEngine();
    KeyPair sender = KeyPair.onlyPublic(senderPublicKey, engine);
    KeyPair recipient = KeyPair.fromPrivate(recipientPrivateKey);
    BlockCipher blockCipher = engine.createBlockCipher(sender, recipient);
    return StringEncoder.getString(blockCipher.decrypt(ConvertUtils.fromHexToBytes(getText())));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) BlockCipher(io.nem.symbol.core.crypto.BlockCipher) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine)

Example 3 with BlockCipher

use of io.nem.symbol.core.crypto.BlockCipher in project nem2-sdk-java by nemtech.

the class Ed25519BlockCipherTest method decryptFailsInputIsTooSmallInLength.

@Test
public void decryptFailsInputIsTooSmallInLength() {
    // 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(new byte[27]));
    // Assert:
    Assertions.assertEquals("Cannot decrypt input. Size is 27 when at least 28 is expected.", 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 4 with BlockCipher

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

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

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