Search in sources :

Example 21 with KeyPair

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

the class AccountTest method shouldAcceptKeyPairAsConstructor.

@Test
void shouldAcceptKeyPairAsConstructor() {
    NetworkType networkType = NetworkType.MIJIN_TEST;
    KeyPair random = KeyPair.random(new Ed25519CryptoEngine());
    Account account = new Account(random, networkType);
    assertEquals(random.getPrivateKey().toHex().toUpperCase(), account.getPrivateKey());
    assertEquals(networkType, account.getAddress().getNetworkType());
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) Ed25519CryptoEngine(io.nem.symbol.core.crypto.ed25519.Ed25519CryptoEngine) NetworkType(io.nem.symbol.sdk.model.network.NetworkType) Test(org.junit.jupiter.api.Test)

Example 22 with KeyPair

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

the class EncryptedMessageTest method testTypeScriptCompatibilityFromPayload.

@Test
public void testTypeScriptCompatibilityFromPayload() {
    // This unit test recreates a message encrypted in one of the typescript unit
    // tests.
    // Encryption should be the same between the 2 sdk. If one sdk is encrypting a
    // message,
    // the other sdk should be able to decrypted if it knows the right keys.
    // Although using the same encryption algorithm, outcome may be different if the
    // encoding
    // process is different. Both TS and Java are using utf-8 and hex encodings,
    KeyPair sender = KeyPair.fromPrivate(PrivateKey.fromHexString("2602F4236B199B3DF762B2AAB46FC3B77D8DDB214F0B62538D3827576C46C108"));
    KeyPair recipient = KeyPair.fromPrivate(PrivateKey.fromHexString("B72F2950498111BADF276D6D9D5E345F04E0D5C9B8342DA983C3395B4CF18F08"));
    String typescriptEncryptedKey = "079A490A7F68CC42F7156D12F082AF1ADC193FD8E3DA93CF67FA1D3F880D5DCEF2A9734EFE39646501023D1A9B63A44E57AEDE";
    EncryptedMessage encryptedMessage = new EncryptedMessage(typescriptEncryptedKey);
    String plainMessage = encryptedMessage.decryptPayload(sender.getPublicKey(), recipient.getPrivateKey());
    Assertions.assertEquals("test transaction 漢字", plainMessage);
    Optional<Message> encryptedMessage2 = Message.createFromHexPayload(encryptedMessage.getPayloadHex());
    Assertions.assertEquals(encryptedMessage, encryptedMessage2.get());
    Optional<Message> encryptedMessage3 = Message.createFromPayload(encryptedMessage.getPayloadByteBuffer().array());
    Assertions.assertEquals(encryptedMessage, encryptedMessage3.get());
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) Test(org.junit.jupiter.api.Test)

Example 23 with KeyPair

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

the class EncryptedMessageTest method testDecryptWrong.

@Test
public void testDecryptWrong() {
    String wrongEncrypted = "ABCD";
    KeyPair sender = KeyPair.random();
    KeyPair recipient = KeyPair.random();
    EncryptedMessage encryptedMessage = new EncryptedMessage(wrongEncrypted);
    Assertions.assertEquals(MessageType.ENCRYPTED_MESSAGE, encryptedMessage.getType());
    CryptoException e = Assertions.assertThrows(CryptoException.class, () -> encryptedMessage.decryptPayload(sender.getPublicKey(), recipient.getPrivateKey()));
    Assertions.assertEquals("Cannot decrypt input. Size is 2 when at least 28 is expected.", e.getMessage());
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) CryptoException(io.nem.symbol.core.crypto.CryptoException) Test(org.junit.jupiter.api.Test)

Example 24 with KeyPair

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

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

KeyPair (io.nem.symbol.core.crypto.KeyPair)25 Test (org.junit.jupiter.api.Test)19 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)13 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 PublicKey (io.nem.symbol.core.crypto.PublicKey)5 CryptoException (io.nem.symbol.core.crypto.CryptoException)3 KeyGenerator (io.nem.symbol.core.crypto.KeyGenerator)3 KeyGeneratorTest (io.nem.symbol.core.crypto.KeyGeneratorTest)3 BigInteger (java.math.BigInteger)3 BlockCipherTest (io.nem.symbol.core.crypto.BlockCipherTest)2 PersistentHarvestingDelegationMessage (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage)2 HarvestingKeys (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage.HarvestingKeys)2 TransferTransaction (io.nem.symbol.sdk.model.transaction.TransferTransaction)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 PrivateKey (io.nem.symbol.core.crypto.PrivateKey)1 Ed25519CryptoEngine (io.nem.symbol.core.crypto.ed25519.Ed25519CryptoEngine)1