followup to prev

This commit is contained in:
Craig Raw 2020-05-12 12:24:22 +02:00
parent 242c83735a
commit ba2004c9fb
3 changed files with 12 additions and 16 deletions

View file

@ -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 {

View file

@ -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");

View file

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