use of io.nem.symbol.core.crypto.CryptoEngine 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())));
}
use of io.nem.symbol.core.crypto.CryptoEngine in project nem2-sdk-java by nemtech.
the class Ed25519DsaSignerTest method isCanonicalReturnsFalseForNonCanonicalSignature.
@Test
public void isCanonicalReturnsFalseForNonCanonicalSignature() {
// Arrange:
final CryptoEngine engine = this.getCryptoEngine();
final KeyPair kp = KeyPair.random(engine);
final DsaSigner dsaSigner = this.getDsaSigner(kp);
final byte[] input = RandomUtils.generateRandomBytes();
// Act:
final Signature signature = dsaSigner.sign(input);
final BigInteger nonCanonicalS = engine.getCurve().getGroupOrder().add(signature.getS());
final Signature nonCanonicalSignature = new Signature(signature.getR(), nonCanonicalS);
// Assert:
Assertions.assertFalse(dsaSigner.isCanonicalSignature(nonCanonicalSignature));
}
use of io.nem.symbol.core.crypto.CryptoEngine in project nem2-sdk-java by nemtech.
the class Ed25519DsaSignerTest method signReturnsVerifiableSignature.
@Test
public void signReturnsVerifiableSignature() {
// Arrange:
final CryptoEngine engine = this.getCryptoEngine();
final KeyPair keyPair = KeyPair.random(engine);
for (int i = 0; i < 20; i++) {
final DsaSigner dsaSigner = this.getDsaSigner(keyPair);
final byte[] input = RandomUtils.generateRandomBytes();
// Act:
final Signature signature1 = dsaSigner.sign(input);
// Assert:
Assertions.assertTrue(dsaSigner.verify(input, signature1));
}
}
use of io.nem.symbol.core.crypto.CryptoEngine in project nem2-sdk-java by nemtech.
the class Ed25519DsaSignerTest method signThrowsIfGeneratedSignatureIsNotCanonical.
@Test
public void signThrowsIfGeneratedSignatureIsNotCanonical() {
// Arrange:
final CryptoEngine engine = this.getCryptoEngine();
final KeyPair keyPair = KeyPair.random(engine);
final Ed25519DsaSigner dsaSigner = new Ed25519DsaSigner(keyPair) {
@Override
public boolean isCanonicalSignature(Signature signature) {
return false;
}
};
final byte[] input = RandomUtils.generateRandomBytes();
// Act:
Assertions.assertEquals("Generated signature is not canonical", Assertions.assertThrows(CryptoException.class, () -> dsaSigner.sign(input)).getMessage());
}
use of io.nem.symbol.core.crypto.CryptoEngine 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());
}
Aggregations