-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
270 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...rc/main/java/org/jfrog/build/extractor/clientConfiguration/util/encryption/Encryptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.jfrog.build.extractor.clientConfiguration.util.encryption; | ||
|
||
import javax.crypto.BadPaddingException; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.IllegalBlockSizeException; | ||
import javax.crypto.NoSuchPaddingException; | ||
import javax.crypto.spec.GCMParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.security.InvalidAlgorithmParameterException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public class Encryptor { | ||
private static final String ALGORITHM = "AES"; | ||
private static final String TRANSFORMATION = "AES/GCM/NoPadding"; | ||
private static final int GCM_TAG_LENGTH = 128; | ||
|
||
/** | ||
* Decrypts the given data using the provided EncryptionKeyPair. | ||
* | ||
* @param data The encrypted data to be decrypted | ||
* @param keyPair The EncryptionKeyPair containing the secret key and IV for decryption | ||
* @return The decrypted data as a byte array | ||
*/ | ||
public static byte[] decrypt(byte[] data, EncryptionKeyPair keyPair) throws IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { | ||
Cipher cipher = Cipher.getInstance(TRANSFORMATION); | ||
SecretKeySpec secretKeySpec = new SecretKeySpec(keyPair.getSecretKey(), ALGORITHM); | ||
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, keyPair.getIv()); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParameterSpec); | ||
return cipher.doFinal(data); | ||
} | ||
|
||
/** | ||
* Encrypts the given data using the provided EncryptionKeyPair. | ||
* | ||
* @param data The data to be encrypted | ||
* @param keyPair The EncryptionKeyPair containing the secret key and IV for encryption | ||
* @return The encrypted data as a byte array | ||
*/ | ||
public static byte[] encrypt(byte[] data, EncryptionKeyPair keyPair) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { | ||
Cipher cipher = Cipher.getInstance(TRANSFORMATION); | ||
SecretKeySpec secretKeySpec = new SecretKeySpec(keyPair.getSecretKey(), ALGORITHM); | ||
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH, keyPair.getIv()); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gcmParameterSpec); | ||
return cipher.doFinal(data); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...java/org/jfrog/build/extractor/clientConfiguration/util/encryption/PropertyEncryptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package org.jfrog.build.extractor.clientConfiguration.util.encryption; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import javax.crypto.BadPaddingException; | ||
import javax.crypto.IllegalBlockSizeException; | ||
import javax.crypto.NoSuchPaddingException; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.InvalidAlgorithmParameterException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Properties; | ||
|
||
import static org.jfrog.build.extractor.clientConfiguration.util.encryption.Encryptor.decrypt; | ||
import static org.jfrog.build.extractor.clientConfiguration.util.encryption.Encryptor.encrypt; | ||
|
||
public class PropertyEncryptor { | ||
|
||
/** | ||
* Decrypts properties from a file using the provided secret key. | ||
* | ||
* @param filePath The path to the file containing encrypted properties. | ||
* @param keyPair The secret key and iv used for decryption. | ||
* @return A Properties object containing the decrypted properties. | ||
*/ | ||
public static Properties decryptPropertiesFromFile(String filePath, EncryptionKeyPair keyPair) throws IOException, InvalidAlgorithmParameterException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException { | ||
if (StringUtils.isBlank(filePath)) { | ||
return null; | ||
} | ||
if (!new File(filePath).exists()) { | ||
throw new IOException("File " + filePath + " does not exist"); | ||
} | ||
return decryptProperties(FileUtils.readFileToByteArray(new File(filePath)), keyPair); | ||
} | ||
|
||
/** | ||
* Decrypts properties from an encrypted byte array using the provided secret key. | ||
* | ||
* @param encryptedData The encrypted byte array representing properties. | ||
* @param keyPair The secret key and iv used for decryption. | ||
* @return A Properties object containing the decrypted properties. | ||
*/ | ||
private static Properties decryptProperties(byte[] encryptedData, EncryptionKeyPair keyPair) throws IOException, InvalidAlgorithmParameterException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, InvalidKeyException { | ||
byte[] decryptedBytes = decrypt(encryptedData, keyPair); | ||
String decryptedString = new String(decryptedBytes, StandardCharsets.UTF_8); | ||
return stringToProperties(decryptedString); | ||
} | ||
|
||
/** | ||
* Encrypts properties to a file represented as a byte array and returns the secret key used for encryption. | ||
* | ||
* @param os The output stream where the encrypted properties will be written. | ||
* @param properties The Properties object containing the properties to be encrypted. | ||
* @return A byte array representing the secret key used for encryption. | ||
*/ | ||
public static EncryptionKeyPair encryptedPropertiesToStream(OutputStream os, Properties properties) throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { | ||
EncryptionKeyPair keyPair = new EncryptionKeyPair(); | ||
os.write(encrypt(propertiesToString(properties).getBytes(StandardCharsets.UTF_8), keyPair)); | ||
return keyPair; | ||
} | ||
|
||
private static Properties stringToProperties(String propertiesString) throws IOException { | ||
Properties properties = new Properties(); | ||
try (InputStream inputStream = new ByteArrayInputStream(propertiesString.getBytes(StandardCharsets.UTF_8))) { | ||
properties.load(inputStream); | ||
} | ||
return properties; | ||
} | ||
|
||
private static String propertiesToString(Properties properties) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
for (String key : properties.stringPropertyNames()) { | ||
stringBuilder.append(key).append("=").append(properties.getProperty(key)).append("\n"); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |
105 changes: 0 additions & 105 deletions
105
...jfrog/build/extractor/clientConfiguration/util/encryption/SecurePropertiesEncryption.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.