From a7a9518b7a432a1e31270df03bc49239befc73d6 Mon Sep 17 00:00:00 2001 From: SR20290919 Date: Tue, 15 Oct 2024 23:34:46 -0700 Subject: [PATCH 1/5] adding changes related to encryption and decryption --- .../AESEncryptionDecryption.java | 113 ++++++++++++------ 1 file changed, 77 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java b/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java index bd78368c..d0a730a0 100644 --- a/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java +++ b/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java @@ -1,73 +1,114 @@ package com.iemr.common.utils.aesencryption; import java.io.UnsupportedEncodingException; + import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; import java.util.Arrays; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; +import javax.crypto.spec.GCMParameterSpec; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import com.iemr.common.encryption.exception.DecryptionException; -import com.iemr.common.encryption.exception.EncryptionException; import com.iemr.common.utils.aesencryption.AESEncryptionDecryption; @Component public class AESEncryptionDecryption { - private static Logger logger = LoggerFactory.getLogger(AESEncryptionDecryption.class); + + private Logger logger = LoggerFactory.getLogger(AESEncryptionDecryption.class); + private static SecretKeySpec secretKey; - private static byte[] key; - static final String SECRET = "amrith$%2022@&*piramal@@swasthya!#"; - public static void setKey(String myKey) { - MessageDigest sha = null; + private byte[] key; + + private final String secret = "amrith$%2022@&*piramal@@swasthya!#"; + + private static final int IV_SIZE = 12; + + private static final int TAG_SIZE = 128; + + private final String UTF_8 = "UTF-8"; + + public void setKey(String myKey) { + try { - key = myKey.getBytes("UTF-8"); - sha = MessageDigest.getInstance("SHA-512"); + + key = myKey.getBytes(UTF_8); + + MessageDigest sha = MessageDigest.getInstance("SHA-512"); + key = sha.digest(key); + key = Arrays.copyOf(key, 16); + secretKey = new SecretKeySpec(key, "AES"); + } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { - logger.error("context", e); + + logger.error("context", e); + } + } + + public String encrypt(String strToEncrypt) throws Exception { + if (secretKey == null) + + setKey(secret); + + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); + + // Generate IV + + byte[] iv = new byte[IV_SIZE]; + + SecureRandom random = new SecureRandom(); + + random.nextBytes(iv); + + cipher.init(Cipher.ENCRYPT_MODE, secretKey, new GCMParameterSpec(TAG_SIZE, iv)); + + byte[] encryptedBytes = cipher.doFinal(strToEncrypt.getBytes(UTF_8)); + + byte[] encryptedIvAndText = new byte[IV_SIZE + encryptedBytes.length]; + + System.arraycopy(iv, 0, encryptedIvAndText, 0, IV_SIZE); + + System.arraycopy(encryptedBytes, 0, encryptedIvAndText, IV_SIZE, encryptedBytes.length); + + return Base64.getEncoder().encodeToString(encryptedIvAndText); - public String encrypt(String strToEncrypt) throws EncryptionException { - String encryptedString=null; - try { - if (secretKey == null) - setKey(SECRET); - Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); - cipher.init(Cipher.ENCRYPT_MODE, secretKey); - encryptedString= Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))); - } catch (Exception e) { - logger.error("Error while encrypting: {}", e.toString()); - throw new EncryptionException("Error while encrypting: "+e.toString(), e); - } - return encryptedString; } + + public String decrypt(String strToDecrypt) throws Exception { + if (secretKey == null) + + setKey(secret); + + byte[] encryptedIvAndText = Base64.getDecoder().decode(strToDecrypt); + + byte[] iv = Arrays.copyOfRange(encryptedIvAndText, 0, IV_SIZE); + + byte[] encryptedBytes = Arrays.copyOfRange(encryptedIvAndText, IV_SIZE, encryptedIvAndText.length); + + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); + + cipher.init(Cipher.DECRYPT_MODE, secretKey, new GCMParameterSpec(TAG_SIZE, iv)); + + byte[] decryptedBytes = cipher.doFinal(encryptedBytes); + + return new String(decryptedBytes, UTF_8); - public String decrypt(String strToDecrypt) throws DecryptionException { - String decryptedString=null; - try { - if (secretKey == null) - setKey(SECRET); - Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); - cipher.init(Cipher.DECRYPT_MODE, secretKey); - decryptedString= new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); - } catch (Exception e) { - logger.error("Error while decrypting: {}",e.toString()); - throw new DecryptionException("Error while decrypting: "+e.toString(), e); - } - return decryptedString; } + } From e1b53189a6a8dc8f1aa347fd50bd4e2c726c26a9 Mon Sep 17 00:00:00 2001 From: SR20290919 Date: Tue, 15 Oct 2024 23:42:56 -0700 Subject: [PATCH 2/5] making final field static --- .../common/utils/aesencryption/AESEncryptionDecryption.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java b/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java index d0a730a0..0fab3247 100644 --- a/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java +++ b/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java @@ -29,13 +29,13 @@ public class AESEncryptionDecryption { private byte[] key; - private final String secret = "amrith$%2022@&*piramal@@swasthya!#"; + private static final String secret = "amrith$%2022@&*piramal@@swasthya!#"; private static final int IV_SIZE = 12; private static final int TAG_SIZE = 128; - private final String UTF_8 = "UTF-8"; + private static final String UTF_8 = "UTF-8"; public void setKey(String myKey) { From eddabe303a59b4d9f181d22139db1319bc374542 Mon Sep 17 00:00:00 2001 From: SR20290919 Date: Tue, 15 Oct 2024 23:48:30 -0700 Subject: [PATCH 3/5] making enclosing method static --- .../utils/aesencryption/AESEncryptionDecryption.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java b/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java index 0fab3247..2c337d30 100644 --- a/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java +++ b/src/main/java/com/iemr/common/utils/aesencryption/AESEncryptionDecryption.java @@ -23,13 +23,13 @@ public class AESEncryptionDecryption { - private Logger logger = LoggerFactory.getLogger(AESEncryptionDecryption.class); + private static Logger logger = LoggerFactory.getLogger(AESEncryptionDecryption.class); private static SecretKeySpec secretKey; - private byte[] key; + private static byte[] key; - private static final String secret = "amrith$%2022@&*piramal@@swasthya!#"; + private static final String SECRET = "amrith$%2022@&*piramal@@swasthya!#"; private static final int IV_SIZE = 12; @@ -37,7 +37,7 @@ public class AESEncryptionDecryption { private static final String UTF_8 = "UTF-8"; - public void setKey(String myKey) { + public static void setKey(String myKey) { try { @@ -63,7 +63,7 @@ public String encrypt(String strToEncrypt) throws Exception { if (secretKey == null) - setKey(secret); + setKey(SECRET); Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); @@ -93,7 +93,7 @@ public String decrypt(String strToDecrypt) throws Exception { if (secretKey == null) - setKey(secret); + setKey(SECRET); byte[] encryptedIvAndText = Base64.getDecoder().decode(strToDecrypt); From 6e626bb73d41436b6fe4939546635728e5b22686 Mon Sep 17 00:00:00 2001 From: SR20290919 Date: Mon, 9 Dec 2024 23:55:33 -0800 Subject: [PATCH 4/5] adding beneficiaryConsent param to createFeedback API --- .../common/data/feedback/FeedbackDetails.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java b/src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java index 40111ebd..b09c5dea 100644 --- a/src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java +++ b/src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java @@ -100,6 +100,10 @@ public class FeedbackDetails { @Transient private String instituteName = ""; + + @Column(name = "BeneficiaryConsent") + @Expose + private Boolean beneficiaryConsent; @Column(name = "DesignationID") @Expose @@ -304,7 +308,7 @@ public FeedbackDetails() { public FeedbackDetails(Long feedbackID, Long institutionID,String instiName, Integer designationID, Integer severityID, Integer feedbackTypeID, Integer feedbackStatusID, String feedback, Long beneficiaryRegID, Integer serviceID, Integer userID, String sMSPhoneNo, Timestamp serviceAvailDate, Boolean deleted, String createdBy, - Timestamp createdDate, String modifiedBy, Timestamp lastModDate, String feedbackAgainst) { + Timestamp createdDate, String modifiedBy, Timestamp lastModDate, String feedbackAgainst, Boolean beneficiaryConsent) { super(); this.feedbackID = feedbackID; this.institutionID = institutionID; @@ -325,7 +329,8 @@ public FeedbackDetails(Long feedbackID, Long institutionID,String instiName, Int this.modifiedBy = modifiedBy; this.lastModDate = lastModDate; this.feedbackAgainst = feedbackAgainst; - } + this.beneficiaryConsent = beneficiaryConsent; + } public FeedbackDetails(Long feedbackID, Integer severityID, Integer feedbackTypeID, Integer feedbackStatusID, String feedback, String createdBy, String feedbackAgainst) { @@ -466,6 +471,16 @@ public Boolean getDeleted() { public void setDeleted(Boolean deleted) { this.deleted = deleted; } + + public Boolean getbeneficiaryConsent() { + return beneficiaryConsent; + } + + public void setbeneficiaryConsent(Boolean beneficiaryConsent) { + this.beneficiaryConsent = beneficiaryConsent; + } + + public String getCreatedBy() { return createdBy; @@ -555,7 +570,7 @@ public static FeedbackDetails initializeFeedbackDetailsWithAllFeilds(Long feedba Integer stateID, States state, Integer districtID, Districts district, Integer blockID, DistrictBlock districtBlock, Integer districtBranchID, DistrictBranchMapping districtBranchMapping, Integer instituteTypeID, InstituteType instituteType, Integer feedbackNatureID, - FeedbackNatureDetail feedbackNatureDetail, String feedbackAgainst) { + FeedbackNatureDetail feedbackNatureDetail, String feedbackAgainst, Boolean beneficiaryConsent) { FeedbackDetails feedbackDetails = new FeedbackDetails(); feedbackDetails.feedbackID = feedbackID; feedbackDetails.mUser = mUser; @@ -624,6 +639,7 @@ public static FeedbackDetails initializeFeedbackDetailsWithAllFeilds(Long feedba feedbackDetails.feedbackNatureID = feedbackNatureID; feedbackDetails.feedbackNatureDetail = feedbackNatureDetail; feedbackDetails.feedbackAgainst = feedbackAgainst; + feedbackDetails.beneficiaryConsent = beneficiaryConsent; return feedbackDetails; } From 2303d6011ddde25e254908bf6519167eca36c0b7 Mon Sep 17 00:00:00 2001 From: Srishti gupta <76839176+srishtigrp78@users.noreply.github.com> Date: Tue, 10 Dec 2024 00:08:54 -0800 Subject: [PATCH 5/5] Update src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .../java/com/iemr/common/data/feedback/FeedbackDetails.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java b/src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java index b09c5dea..5ab9ea68 100644 --- a/src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java +++ b/src/main/java/com/iemr/common/data/feedback/FeedbackDetails.java @@ -471,12 +471,11 @@ public Boolean getDeleted() { public void setDeleted(Boolean deleted) { this.deleted = deleted; } - - public Boolean getbeneficiaryConsent() { + public Boolean getBeneficiaryConsent() { return beneficiaryConsent; } - public void setbeneficiaryConsent(Boolean beneficiaryConsent) { + public void setBeneficiaryConsent(Boolean beneficiaryConsent) { this.beneficiaryConsent = beneficiaryConsent; }