-
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.
Add Initialization vector(IV) used in encryption with a given key
- Loading branch information
Showing
8 changed files
with
156 additions
and
44 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
63 changes: 63 additions & 0 deletions
63
...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,63 @@ | ||
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 = 126; | ||
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; | ||
} | ||
|
||
public String getStringSecretKey() { | ||
return Base64.getEncoder().encodeToString(secretKey); | ||
} | ||
|
||
public byte[] getIv() { | ||
return Iv; | ||
} | ||
|
||
public String getStringIv() { | ||
return Base64.getEncoder().encodeToString(Iv); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return secretKey == null || secretKey.length == 0 || Iv == null || Iv.length == 0; | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
...c/test/java/org/jfrog/build/extractor/util/encryption/SecurePropertiesEncryptionTest.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,37 @@ | ||
package org.jfrog.build.extractor.util.encryption; | ||
|
||
import org.jfrog.build.extractor.clientConfiguration.util.encryption.EncryptionKeyPair; | ||
import org.jfrog.build.extractor.clientConfiguration.util.encryption.SecurePropertiesEncryption; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNotNull; | ||
|
||
public class SecurePropertiesEncryptionTest { | ||
@Test | ||
public void testEncryptDecryptProperties() throws IOException { | ||
// Create properties to be encrypted | ||
Properties originalProperties = new Properties(); | ||
originalProperties.setProperty("key1", "value1"); | ||
originalProperties.setProperty("key2", "value2"); | ||
|
||
// Encrypt properties and get encryption key pair | ||
ByteArrayOutputStream encryptedOutputStream = new ByteArrayOutputStream(); | ||
EncryptionKeyPair keyPair = SecurePropertiesEncryption.encryptedPropertiesToFile(encryptedOutputStream, originalProperties); | ||
assertNotNull(keyPair); | ||
|
||
// Decrypt properties using the generated key pair | ||
byte[] encryptedData = encryptedOutputStream.toByteArray(); | ||
Properties decryptedProperties = SecurePropertiesEncryption.decryptProperties(encryptedData, keyPair); | ||
assertNotNull(decryptedProperties); | ||
|
||
// Compare original and decrypted properties | ||
assertEquals(originalProperties.size(), decryptedProperties.size()); | ||
assertEquals(originalProperties.getProperty("key1"), decryptedProperties.getProperty("key1")); | ||
assertEquals(originalProperties.getProperty("key2"), decryptedProperties.getProperty("key2")); | ||
} | ||
} |