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