Search in sources :

Example 6 with CryptoEngine

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

Example 7 with CryptoEngine

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));
}
Also used : KeyPair(io.nem.symbol.core.crypto.KeyPair) CryptoEngine(io.nem.symbol.core.crypto.CryptoEngine) DsaSigner(io.nem.symbol.core.crypto.DsaSigner) Signature(io.nem.symbol.core.crypto.Signature) BigInteger(java.math.BigInteger) DsaSignerTest(io.nem.symbol.core.crypto.DsaSignerTest) Test(org.junit.jupiter.api.Test)

Example 8 with CryptoEngine

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

Example 9 with CryptoEngine

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

Example 10 with CryptoEngine

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

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