mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-12-27 02:26:44 +00:00
followup to prev
This commit is contained in:
parent
242c83735a
commit
ba2004c9fb
3 changed files with 12 additions and 16 deletions
|
@ -12,7 +12,7 @@ import java.security.SecureRandom;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
* Performs AES/CBC/PKCS7 encryption and decryption
|
||||||
*/
|
*/
|
||||||
public class AESKeyCrypter implements KeyCrypter {
|
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
|
@Override
|
||||||
public EncryptedData encrypt(byte[] plainBytes, byte[] initializationVector, KeyParameter aesKey) throws KeyCrypterException {
|
public EncryptedData encrypt(byte[] plainBytes, byte[] initializationVector, KeyParameter aesKey) throws KeyCrypterException {
|
||||||
|
|
|
@ -43,15 +43,10 @@ public class ECIESKeyCrypter implements AsymmetricKeyCrypter {
|
||||||
if(decoded.length < 85) {
|
if(decoded.length < 85) {
|
||||||
throw new IllegalArgumentException("Ciphertext is too short at " + decoded.length + " bytes");
|
throw new IllegalArgumentException("Ciphertext is too short at " + decoded.length + " bytes");
|
||||||
}
|
}
|
||||||
byte[] magicFound = Arrays.copyOfRange(decoded, 0, 4); //new byte[4];
|
byte[] magicFound = Arrays.copyOfRange(decoded, 0, 4);
|
||||||
//System.arraycopy(decoded, 0, magicFound, 0, 4);
|
byte[] ephemeralPubKeyBytes = Arrays.copyOfRange(decoded, 4, 37);
|
||||||
byte[] ephemeralPubKeyBytes = Arrays.copyOfRange(decoded, 4, 37); //new byte[33];
|
byte[] ciphertext = Arrays.copyOfRange(decoded, 37, decoded.length - 32);
|
||||||
//System.arraycopy(decoded, 4, ephemeralPubKeyBytes, 0, 33);
|
byte[] mac = Arrays.copyOfRange(decoded, decoded.length - 32, decoded.length);
|
||||||
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);
|
|
||||||
|
|
||||||
if(!Arrays.equals(magic, magicFound)) {
|
if(!Arrays.equals(magic, magicFound)) {
|
||||||
throw new IllegalArgumentException("Invalid ciphertext: invalid magic bytes");
|
throw new IllegalArgumentException("Invalid ciphertext: invalid magic bytes");
|
||||||
|
|
|
@ -12,10 +12,8 @@ import java.security.Security;
|
||||||
public class ScryptKeyCrypterTest {
|
public class ScryptKeyCrypterTest {
|
||||||
@Test
|
@Test
|
||||||
public void testScrypt() {
|
public void testScrypt() {
|
||||||
Security.addProvider(new BouncyCastleProvider());
|
ScryptKeyCrypter scryptKeyCrypter = new ScryptKeyCrypter();
|
||||||
|
KeyParameter keyParameter = scryptKeyCrypter.deriveKey("password");
|
||||||
KeyCrypter keyDeriver = new AESKeyCrypter();
|
|
||||||
KeyParameter keyParameter = keyDeriver.deriveKey("password");
|
|
||||||
|
|
||||||
String message = "testastringmessage";
|
String message = "testastringmessage";
|
||||||
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
|
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
|
||||||
|
@ -24,7 +22,6 @@ public class ScryptKeyCrypterTest {
|
||||||
SecureRandom secureRandom = new SecureRandom();
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
secureRandom.nextBytes(iv);
|
secureRandom.nextBytes(iv);
|
||||||
|
|
||||||
ScryptKeyCrypter scryptKeyCrypter = new ScryptKeyCrypter();
|
|
||||||
EncryptedData scrypted = scryptKeyCrypter.encrypt(messageBytes, iv, keyParameter);
|
EncryptedData scrypted = scryptKeyCrypter.encrypt(messageBytes, iv, keyParameter);
|
||||||
|
|
||||||
AESKeyCrypter aesKeyCrypter = new AESKeyCrypter();
|
AESKeyCrypter aesKeyCrypter = new AESKeyCrypter();
|
||||||
|
@ -36,5 +33,9 @@ public class ScryptKeyCrypterTest {
|
||||||
byte[] aesdecrypted = aesKeyCrypter.decrypt(aescrypted, keyParameter);
|
byte[] aesdecrypted = aesKeyCrypter.decrypt(aescrypted, keyParameter);
|
||||||
|
|
||||||
Assert.assertArrayEquals(sdecrypted, aesdecrypted);
|
Assert.assertArrayEquals(sdecrypted, aesdecrypted);
|
||||||
|
|
||||||
|
String decryptedMessage = new String(sdecrypted, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
Assert.assertEquals(message, decryptedMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue