diff --git a/src/main/java/com/sparrowwallet/drongo/crypto/AESKeyCrypter.java b/src/main/java/com/sparrowwallet/drongo/crypto/AESKeyCrypter.java index c1f93af..b6d38b8 100644 --- a/src/main/java/com/sparrowwallet/drongo/crypto/AESKeyCrypter.java +++ b/src/main/java/com/sparrowwallet/drongo/crypto/AESKeyCrypter.java @@ -12,7 +12,7 @@ import java.security.SecureRandom; import java.util.Arrays; /* - * + * Performs AES/CBC/PKCS7 encryption and decryption */ public class AESKeyCrypter implements KeyCrypter { /** @@ -68,7 +68,7 @@ public class AESKeyCrypter implements KeyCrypter { } /** - * Password based encryption using AES - CBC 256 bits. + * Password based encryption using AES - CBC - PKCS7 */ @Override public EncryptedData encrypt(byte[] plainBytes, byte[] initializationVector, KeyParameter aesKey) throws KeyCrypterException { diff --git a/src/main/java/com/sparrowwallet/drongo/crypto/ECIESKeyCrypter.java b/src/main/java/com/sparrowwallet/drongo/crypto/ECIESKeyCrypter.java index 5cb55bc..64ddf4f 100644 --- a/src/main/java/com/sparrowwallet/drongo/crypto/ECIESKeyCrypter.java +++ b/src/main/java/com/sparrowwallet/drongo/crypto/ECIESKeyCrypter.java @@ -43,15 +43,10 @@ public class ECIESKeyCrypter implements AsymmetricKeyCrypter { if(decoded.length < 85) { throw new IllegalArgumentException("Ciphertext is too short at " + decoded.length + " bytes"); } - byte[] magicFound = Arrays.copyOfRange(decoded, 0, 4); //new byte[4]; - //System.arraycopy(decoded, 0, magicFound, 0, 4); - byte[] ephemeralPubKeyBytes = Arrays.copyOfRange(decoded, 4, 37); //new byte[33]; - //System.arraycopy(decoded, 4, ephemeralPubKeyBytes, 0, 33); - int ciphertextlength = decoded.length - 37 - 32; - byte[] ciphertext = Arrays.copyOfRange(decoded, 37, decoded.length - 32); //new byte[ciphertextlength]; - //System.arraycopy(decoded, 37, ciphertext, 0, ciphertextlength); - byte[] mac = Arrays.copyOfRange(decoded, decoded.length - 32, decoded.length); //new byte[32]; - //System.arraycopy(decoded, decoded.length - 32, mac, 0, 32); + byte[] magicFound = Arrays.copyOfRange(decoded, 0, 4); + byte[] ephemeralPubKeyBytes = Arrays.copyOfRange(decoded, 4, 37); + byte[] ciphertext = Arrays.copyOfRange(decoded, 37, decoded.length - 32); + byte[] mac = Arrays.copyOfRange(decoded, decoded.length - 32, decoded.length); if(!Arrays.equals(magic, magicFound)) { throw new IllegalArgumentException("Invalid ciphertext: invalid magic bytes"); diff --git a/src/test/java/com/sparrowwallet/drongo/crypto/ScryptKeyCrypterTest.java b/src/test/java/com/sparrowwallet/drongo/crypto/ScryptKeyCrypterTest.java index b6aae38..f68b4f6 100644 --- a/src/test/java/com/sparrowwallet/drongo/crypto/ScryptKeyCrypterTest.java +++ b/src/test/java/com/sparrowwallet/drongo/crypto/ScryptKeyCrypterTest.java @@ -12,10 +12,8 @@ import java.security.Security; public class ScryptKeyCrypterTest { @Test public void testScrypt() { - Security.addProvider(new BouncyCastleProvider()); - - KeyCrypter keyDeriver = new AESKeyCrypter(); - KeyParameter keyParameter = keyDeriver.deriveKey("password"); + ScryptKeyCrypter scryptKeyCrypter = new ScryptKeyCrypter(); + KeyParameter keyParameter = scryptKeyCrypter.deriveKey("password"); String message = "testastringmessage"; byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8); @@ -24,7 +22,6 @@ public class ScryptKeyCrypterTest { SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(iv); - ScryptKeyCrypter scryptKeyCrypter = new ScryptKeyCrypter(); EncryptedData scrypted = scryptKeyCrypter.encrypt(messageBytes, iv, keyParameter); AESKeyCrypter aesKeyCrypter = new AESKeyCrypter(); @@ -36,5 +33,9 @@ public class ScryptKeyCrypterTest { byte[] aesdecrypted = aesKeyCrypter.decrypt(aescrypted, keyParameter); Assert.assertArrayEquals(sdecrypted, aesdecrypted); + + String decryptedMessage = new String(sdecrypted, StandardCharsets.UTF_8); + + Assert.assertEquals(message, decryptedMessage); } }