-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encrypt & decrypt build info properties file (#766)
- Loading branch information
Showing
10 changed files
with
524 additions
and
41 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
65 changes: 65 additions & 0 deletions
65
...java/org/jfrog/build/extractor/clientConfiguration/util/encryption/EncryptionKeyPair.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,65 @@ | ||
package org.jfrog.build.extractor.clientConfiguration.util.encryption; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.security.SecureRandom; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Represents a pair of secret key and initialization vector (IV) used for encryption and decryption. | ||
*/ | ||
public class EncryptionKeyPair { | ||
private static final int AES_256_KEY_LENGTH = 256; | ||
private static final int IV_LENGTH = 128; | ||
private byte[] secretKey; | ||
private byte[] iv; | ||
|
||
public EncryptionKeyPair() { | ||
this.secretKey = generateRandomKey(AES_256_KEY_LENGTH); | ||
this.iv = generateRandomKey(IV_LENGTH); | ||
} | ||
|
||
public EncryptionKeyPair(String secretKey, String Iv) { | ||
if (StringUtils.isNotBlank(secretKey)) { | ||
this.secretKey = Base64.getDecoder().decode(secretKey); | ||
} | ||
if (StringUtils.isNotBlank(Iv)) { | ||
this.iv = Base64.getDecoder().decode(Iv); | ||
} | ||
} | ||
|
||
/** | ||
* Generates a random key of the specified length in bits. | ||
* | ||
* @param lengthInBits The length of the key in bits. | ||
* @return A byte array representing the generated random key. | ||
*/ | ||
private static byte[] generateRandomKey(int lengthInBits) { | ||
SecureRandom secureRandom = new SecureRandom(); | ||
byte[] key = new byte[lengthInBits / 8]; | ||
secureRandom.nextBytes(key); | ||
return key; | ||
} | ||
|
||
public byte[] getSecretKey() { | ||
return secretKey; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public String getStringSecretKey() { | ||
return Base64.getEncoder().encodeToString(secretKey); | ||
} | ||
|
||
public byte[] getIv() { | ||
return iv; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public String getStringIv() { | ||
return Base64.getEncoder().encodeToString(iv); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return secretKey == null || secretKey.length == 0 || iv == null || iv.length == 0; | ||
} | ||
} |
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); | ||
} | ||
} |
Oops, something went wrong.