Skip to content

Commit 1d0af7b

Browse files
committed
changes according to comments
1 parent 2e0d87a commit 1d0af7b

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

sdk/src/main/java/io/opentdf/platform/sdk/AesGcm.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import javax.crypto.*;
44
import javax.crypto.spec.GCMParameterSpec;
55
import javax.crypto.spec.SecretKeySpec;
6-
import java.nio.charset.StandardCharsets;
76
import java.security.InvalidAlgorithmParameterException;
87
import java.security.InvalidKeyException;
98
import java.security.NoSuchAlgorithmException;
@@ -14,21 +13,33 @@
1413
public class AesGcm {
1514
private static final int GCM_NONCE_LENGTH = 12; // in bytes
1615
private static final int GCM_TAG_LENGTH = 16; // in bytes
16+
private static final String CIPHER_TRANSFORM = "AES/GCM/NoPadding";
1717

1818
private final SecretKey key;
1919

20+
/**
21+
* <p>Constructor for AesGcm.</p>
22+
*
23+
* @param key secret key for encryption and decryption
24+
*/
2025
public AesGcm(byte[] key) {
2126
if (key.length == 0) {
2227
throw new IllegalArgumentException("Invalid key size for gcm encryption");
2328
}
2429
this.key = new SecretKeySpec(key, "AES");
2530
}
2631

32+
/**
33+
* <p>encrypt.</p>
34+
*
35+
* @param plaintext the plaintext to encrypt
36+
* @return the encrypted text
37+
*/
2738
public byte[] encrypt(byte[] plaintext) throws NoSuchPaddingException, NoSuchAlgorithmException,
2839
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
29-
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
40+
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORM);
3041
byte[] nonce = new byte[GCM_NONCE_LENGTH];
31-
new SecureRandom().nextBytes(nonce);
42+
SecureRandom.getInstanceStrong().nextBytes(nonce);
3243
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);
3344
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
3445

@@ -39,9 +50,15 @@ public byte[] encrypt(byte[] plaintext) throws NoSuchPaddingException, NoSuchAlg
3950
return cipherTextWithNonce;
4051
}
4152

53+
/**
54+
* <p>decrypt.</p>
55+
*
56+
* @param cipherTextWithNonce the ciphertext with nonce to decrypt
57+
* @return the decrypted text
58+
*/
4259
public byte[] decrypt(byte[] cipherTextWithNonce) throws NoSuchPaddingException, NoSuchAlgorithmException,
4360
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
44-
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
61+
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORM);
4562
byte[] nonce = Arrays.copyOfRange(cipherTextWithNonce, 0, GCM_NONCE_LENGTH);
4663
byte[] cipherText = Arrays.copyOfRange(cipherTextWithNonce, GCM_NONCE_LENGTH, cipherTextWithNonce.length);
4764
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);

sdk/src/main/java/io/opentdf/platform/sdk/AsymDecryption.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package io.opentdf.platform.sdk;
22

33
import javax.crypto.Cipher;
4-
import java.nio.charset.StandardCharsets;
54
import java.security.*;
65
import java.security.spec.PKCS8EncodedKeySpec;
76
import java.util.Base64;
87

98
public class AsymDecryption {
109
private PrivateKey privateKey;
10+
private static final String PRIVATE_KEY_HEADER = "-----BEGIN PRIVATE KEY-----";
11+
private static final String PRIVATE_KEY_FOOTER = "-----END PRIVATE KEY-----";
12+
private static final String CIPHER_TRANSFORM = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
1113

14+
/**
15+
* <p>Constructor for AsymDecryption.</p>
16+
*
17+
* @param privateKeyInPem a Private Key in PEM format
18+
*/
1219
public AsymDecryption(String privateKeyInPem) throws Exception {
1320
String privateKeyPEM = privateKeyInPem
14-
.replace("-----BEGIN PRIVATE KEY-----\n", "")
15-
.replace("-----END PRIVATE KEY-----\n", "")
21+
.replace(PRIVATE_KEY_HEADER, "")
22+
.replace(PRIVATE_KEY_FOOTER, "")
1623
.replaceAll("\\s", ""); // remove whitespaces
1724

1825
byte[] decoded = Base64.getDecoder().decode(privateKeyPEM);
@@ -22,12 +29,18 @@ public AsymDecryption(String privateKeyInPem) throws Exception {
2229
this.privateKey = kf.generatePrivate(spec);
2330
}
2431

32+
/**
33+
* <p>decrypt.</p>
34+
*
35+
* @param data the data to decrypt
36+
* @return the decrypted data
37+
*/
2538
public byte[] decrypt(byte[] data) throws Exception {
2639
if (this.privateKey == null) {
2740
throw new Exception("Failed to decrypt, private key is empty");
2841
}
2942

30-
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
43+
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORM);
3144
cipher.init(Cipher.DECRYPT_MODE, this.privateKey);
3245
return cipher.doFinal(data);
3346
}
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package io.opentdf.platform.sdk;
22

3-
43
import javax.crypto.Cipher;
5-
import java.nio.charset.StandardCharsets;
64
import java.security.*;
75
import java.security.spec.X509EncodedKeySpec;
86
import java.util.Base64;
97

108
public class AsymEncryption {
119
private PublicKey publicKey;
12-
10+
private static final String PUBLIC_KEY_HEADER = "-----BEGIN PUBLIC KEY-----";
11+
private static final String PUBLIC_KEY_FOOTER = "-----END PUBLIC KEY-----";
12+
private static final String CIPHER_TRANSFORM = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
13+
14+
/**
15+
* <p>Constructor for AsymEncryption.</p>
16+
*
17+
* @param publicKeyInPem a Public Key in PEM format
18+
*/
1319
public AsymEncryption(String publicKeyInPem) throws Exception {
14-
publicKeyInPem = publicKeyInPem.replace("-----BEGIN PUBLIC KEY-----", "")
15-
.replace("-----END PUBLIC KEY-----", "")
20+
publicKeyInPem = publicKeyInPem
21+
.replace(PUBLIC_KEY_HEADER, "")
22+
.replace(PUBLIC_KEY_FOOTER, "")
1623
.replaceAll("\\s", "");
1724

1825
byte[] decoded = Base64.getDecoder().decode(publicKeyInPem);
@@ -21,22 +28,32 @@ public AsymEncryption(String publicKeyInPem) throws Exception {
2128
this.publicKey = kf.generatePublic(spec);
2229
}
2330

31+
/**
32+
* <p>encrypt.</p>
33+
*
34+
* @param data the data to encrypt
35+
* @return the encrypted data
36+
*/
2437
public byte[] encrypt(byte[] data) throws Exception {
2538
if (this.publicKey == null) {
2639
throw new Exception("Failed to encrypt, public key is empty");
2740
}
2841

29-
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
42+
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORM);
3043
cipher.init(Cipher.ENCRYPT_MODE, this.publicKey);
3144
return cipher.doFinal(data);
3245
}
3346

47+
/**
48+
* <p>publicKeyInPemFormat.</p>
49+
* @return the public key in PEM format
50+
*/
3451
public String publicKeyInPemFormat() throws Exception {
3552
if (this.publicKey == null) {
3653
throw new Exception("Failed to generate PEM formatted public key");
3754
}
3855

3956
String publicKeyPem = Base64.getEncoder().encodeToString(this.publicKey.getEncoded());
40-
return "-----BEGIN PUBLIC KEY-----\n" + publicKeyPem + "\n-----END PUBLIC KEY-----\n";
57+
return PUBLIC_KEY_HEADER + '\n' + publicKeyPem + '\n' + PUBLIC_KEY_FOOTER + '\n';
4158
}
4259
}

0 commit comments

Comments
 (0)