Skip to content

Commit

Permalink
[P4ADEV-1726] fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedetta-fabbri committed Jan 8, 2025
1 parent b5e46f7 commit 3f700a0
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -31,8 +28,6 @@ private AESUtils() {
private static final int ITERATION_COUNT = 65536;
private static final Charset UTF_8 = StandardCharsets.UTF_8;

public static final String CIPHER_EXTENSION = ".cipher";

public static byte[] getRandomNonce(int length) {
byte[] nonce = new byte[length];
new SecureRandom().nextBytes(nonce);
Expand Down Expand Up @@ -68,35 +63,6 @@ public static byte[] encrypt(String password, String plainMessage) {
.array();
}

public static InputStream encrypt(String password, InputStream plainStream) {
byte[] salt = getRandomNonce(SALT_LENGTH_BYTE);
SecretKey secretKey = getSecretKey(password, salt);

// GCM recommends 12 bytes iv
byte[] iv = getRandomNonce(IV_LENGTH_BYTE);
Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, secretKey, iv);

// prefix IV and Salt to cipher text
byte[] prefix = ByteBuffer.allocate(iv.length + salt.length)
.put(iv)
.put(salt)
.array();

return new SequenceInputStream(
new ByteArrayInputStream(prefix),
new CipherInputStream(new BufferedInputStream(plainStream), cipher));
}

public static File encrypt(String password, File plainFile) {
File cipherFile = new File(plainFile.getAbsolutePath() + CIPHER_EXTENSION);
try (FileInputStream fis = new FileInputStream(plainFile);
InputStream cipherStream = encrypt(password, fis)) {
Files.copy(cipherStream, cipherFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new IllegalStateException("Something went wrong when ciphering input file " + plainFile.getAbsolutePath(), e);
}
return cipherFile;
}

public static String decrypt(String password, byte[] cipherMessage) {
ByteBuffer byteBuffer = ByteBuffer.wrap(cipherMessage);
Expand All @@ -117,29 +83,6 @@ public static String decrypt(String password, byte[] cipherMessage) {
return new String(decryptedMessageByte, UTF_8);
}

public static InputStream decrypt(String password, InputStream cipherStream) {
try {
byte[] iv = cipherStream.readNBytes(IV_LENGTH_BYTE);
byte[] salt = cipherStream.readNBytes(SALT_LENGTH_BYTE);

SecretKey secretKey = getSecretKey(password, salt);
Cipher cipher = initCipher(Cipher.DECRYPT_MODE, secretKey, iv);

return new CipherInputStream(new BufferedInputStream(cipherStream), cipher);
} catch (IOException e) {
throw new IllegalStateException("Cannot read AES prefix data", e);
}
}

public static void decrypt(String password, File cipherFile, File outputPlainFile) {
try (FileInputStream fis = new FileInputStream(cipherFile);
InputStream plainStream = decrypt(password, fis)) {
Files.copy(plainStream, outputPlainFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new IllegalStateException("Something went wrong when deciphering input file " + cipherFile.getAbsolutePath(), e);
}
}

private static byte[] executeCipherOp(Cipher cipher, byte[] encryptedByte) {
try {
return cipher.doFinal(encryptedByte);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

class AESUtilsTest {

@Test
Expand All @@ -27,35 +18,4 @@ void test() {
// Then
Assertions.assertEquals(plain, result);
}

@Test
void testStream() throws IOException {
// Given
String plain = "PLAINTEXT";
String psw = "PSW";

// When
InputStream cipherStream = AESUtils.encrypt(psw, new ByteArrayInputStream(plain.getBytes(StandardCharsets.UTF_8)));
InputStream resultStream = AESUtils.decrypt(psw, cipherStream);

// Then
Assertions.assertEquals(plain, new String(resultStream.readAllBytes(), StandardCharsets.UTF_8));
}

@Test
void testFile() throws IOException {
// Given
String plain = "PLAINTEXT";
Path plainFile = Path.of("build", "tmp", "plainFile.txt");
Files.writeString(plainFile, plain);
String psw = "PSW";
Path decryptedFile = plainFile.getParent().resolve("decryptedFile.txt");

// When
File cipherFile = AESUtils.encrypt(psw, plainFile.toFile());
AESUtils.decrypt(psw, cipherFile, decryptedFile.toFile());

// Then
Assertions.assertEquals(Files.readAllLines(decryptedFile), List.of(plain));
}
}

0 comments on commit 3f700a0

Please sign in to comment.