Skip to content

Commit

Permalink
Implement the decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
sujankota committed May 12, 2024
1 parent 6404832 commit f2a9b2c
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 33 deletions.
5 changes: 5 additions & 0 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,10 @@
<artifactId>commons-codec</artifactId>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>
15 changes: 14 additions & 1 deletion sdk/src/main/java/io/opentdf/platform/sdk/AesGcm.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,26 @@ public AesGcm(byte[] key) {
*/
public byte[] encrypt(byte[] plaintext) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
return encrypt(plaintext, 0, plaintext.length);
}

/**
* <p>encrypt.</p>
*
* @param plaintext the plaintext byte array to encrypt
* @param offset where the input start
* @param len input length
* @return the encrypted text
*/
public byte[] encrypt(byte[] plaintext, int offset, int len) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORM);
byte[] nonce = new byte[GCM_NONCE_LENGTH];
SecureRandom.getInstanceStrong().nextBytes(nonce);
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);
cipher.init(Cipher.ENCRYPT_MODE, key, spec);

byte[] cipherText = cipher.doFinal(plaintext);
byte[] cipherText = cipher.doFinal(plaintext, offset, len);
byte[] cipherTextWithNonce = new byte[nonce.length + cipherText.length];
System.arraycopy(nonce, 0, cipherTextWithNonce, 0, nonce.length);
System.arraycopy(cipherText, 0, cipherTextWithNonce, nonce.length, cipherText.length);
Expand Down
4 changes: 1 addition & 3 deletions sdk/src/main/java/io/opentdf/platform/sdk/CryptoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

public class CryptoUtils {
public static byte[] CalculateSHA256Hmac(byte[] key, byte[] data) throws NoSuchAlgorithmException,
UnsupportedEncodingException, InvalidKeyException {
InvalidKeyException {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key, "HmacSHA256");
sha256_HMAC.init(secret_key);

return sha256_HMAC.doFinal(data);

//return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
}
}
8 changes: 5 additions & 3 deletions sdk/src/main/java/io/opentdf/platform/sdk/Manifest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ static public class RootSignature {
static public class IntegrityInformation {
public RootSignature rootSignature;
public String segmentHashAlg;
public long segmentSizeDefault;
public long encryptedSegmentSizeDefault;
public int segmentSizeDefault;
public int encryptedSegmentSizeDefault;
public List<Segment> segments;
}

Expand All @@ -46,7 +46,9 @@ static public class EncryptionInformation {
@SerializedName(value = "type")
public String keyAccessType;
public String policy;
public List<KeyAccess> keyAccess;

@SerializedName(value = "keyAccess")
public List<KeyAccess> keyAccessObj;
public Method method;
public IntegrityInformation integrityInformation;
}
Expand Down
Loading

0 comments on commit f2a9b2c

Please sign in to comment.