From afaebc09c1f9f6bf922a70a708bf18111d737cde Mon Sep 17 00:00:00 2001 From: ase-101 <> Date: Mon, 3 Jan 2022 11:40:52 +0530 Subject: [PATCH] base64 encoding and decoding issue fix --- MockMDS/pom.xml | 2 +- .../mock/sbi/service/SBIServiceResponse.java | 4 ++-- .../io/mosip/mock/sbi/util/StringHelper.java | 10 +++++----- .../org/biometric/provider/CaptureRequest.java | 2 +- .../org/biometric/provider/CryptoUtility.java | 16 ++++++++-------- .../org/biometric/provider/DiscoverRequest.java | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/MockMDS/pom.xml b/MockMDS/pom.xml index d43fd842..4d1c49ac 100644 --- a/MockMDS/pom.xml +++ b/MockMDS/pom.xml @@ -109,7 +109,7 @@ io.mosip.kernel kernel-core - 1.0.6 + 1.2.0-SNAPSHOT diff --git a/MockMDS/src/main/java/io/mosip/mock/sbi/service/SBIServiceResponse.java b/MockMDS/src/main/java/io/mosip/mock/sbi/service/SBIServiceResponse.java index aae8daf9..17dc7466 100644 --- a/MockMDS/src/main/java/io/mosip/mock/sbi/service/SBIServiceResponse.java +++ b/MockMDS/src/main/java/io/mosip/mock/sbi/service/SBIServiceResponse.java @@ -2213,7 +2213,7 @@ private BioMetricsDto getBiometricData (String transactionId, CaptureRequestDto X509Certificate certificate = new JwtUtility().getCertificateToEncryptCaptureBioValue(); PublicKey publicKey = certificate.getPublicKey(); Map cryptoResult = CryptoUtility.encrypt(publicKey, - java.util.Base64.getUrlDecoder().decode(bioValue), transactionId); + io.mosip.mock.sbi.util.StringHelper.base64UrlDecode(bioValue), transactionId); biometricData.setTimestamp(cryptoResult.get("TIMESTAMP")); biometricData.setBioValue(cryptoResult.containsKey("ENC_DATA") ? @@ -2252,7 +2252,7 @@ private BioMetricsDto getBiometricData (String transactionId, CaptureRequestDto previousBioDataHash = decodeHex(previousHash); } //instead of BioData, bioValue (before encrytion in case of Capture response) is used for computing the hash. - byte [] currentDataByteArr = java.util.Base64.getUrlDecoder().decode(bioValue); + byte [] currentDataByteArr = io.mosip.mock.sbi.util.StringHelper.base64UrlDecode(bioValue); // Here Byte Array byte[] currentBioDataHash = generateHash (currentDataByteArr); byte[] finalBioDataHash = new byte[currentBioDataHash.length + previousBioDataHash.length]; diff --git a/MockMDS/src/main/java/io/mosip/mock/sbi/util/StringHelper.java b/MockMDS/src/main/java/io/mosip/mock/sbi/util/StringHelper.java index eb2900aa..87c312ac 100644 --- a/MockMDS/src/main/java/io/mosip/mock/sbi/util/StringHelper.java +++ b/MockMDS/src/main/java/io/mosip/mock/sbi/util/StringHelper.java @@ -1,8 +1,8 @@ package io.mosip.mock.sbi.util; import java.nio.charset.StandardCharsets; -import java.util.Base64; +import io.mosip.kernel.core.util.CryptoUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,19 +11,19 @@ public class StringHelper { public static String base64UrlEncode (byte [] arg) { - return Base64.getUrlEncoder().encodeToString(arg); + return CryptoUtil.encodeToURLSafeBase64(arg); } public static String base64UrlEncode (String arg) { - return Base64.getUrlEncoder().encodeToString(arg.getBytes()); + return CryptoUtil.encodeToURLSafeBase64(arg.getBytes(StandardCharsets.UTF_8)); } public static byte[] base64UrlDecode (String arg) { - return Base64.getUrlDecoder().decode(arg); + return CryptoUtil.decodeURLSafeBase64(arg); } - + public static byte [] toUtf8ByteArray (String arg) { return arg.getBytes (StandardCharsets.UTF_8); diff --git a/MockMDS/src/main/java/org/biometric/provider/CaptureRequest.java b/MockMDS/src/main/java/org/biometric/provider/CaptureRequest.java index 0ad865fb..6bea8204 100644 --- a/MockMDS/src/main/java/org/biometric/provider/CaptureRequest.java +++ b/MockMDS/src/main/java/org/biometric/provider/CaptureRequest.java @@ -201,7 +201,7 @@ protected void doAuthCapture(HttpServletRequest request, HttpServletResponse res X509Certificate certificate = new JwtUtility().getCertificateToEncryptCaptureBioValue(); PublicKey publicKey = certificate.getPublicKey(); Map result = CryptoUtility.encrypt(publicKey, - java.util.Base64.getUrlDecoder().decode(dto.getBioValue()), captureRequestDto.transactionId); + io.mosip.mock.sbi.util.StringHelper.base64UrlDecode(dto.getBioValue()), captureRequestDto.transactionId); NewBioAuthDto data = buildAuthNewBioDto(dto, bio.type, bio.requestedScore, captureRequestDto.transactionId, result); diff --git a/MockMDS/src/main/java/org/biometric/provider/CryptoUtility.java b/MockMDS/src/main/java/org/biometric/provider/CryptoUtility.java index 4a4bbfd2..984e0988 100644 --- a/MockMDS/src/main/java/org/biometric/provider/CryptoUtility.java +++ b/MockMDS/src/main/java/org/biometric/provider/CryptoUtility.java @@ -104,8 +104,8 @@ public static Map encrypt(PublicKey publicKey, byte[] dataBytes final byte[] encryptedData = symmetricEncrypt(secretKey, dataBytes, ivBytes, aadBytes); final byte[] encryptedSymmetricKey = asymmetricEncrypt(publicKey, secretKey.getEncoded()); - result.put("ENC_SESSION_KEY", java.util.Base64.getUrlEncoder().encodeToString(encryptedSymmetricKey)); - result.put("ENC_DATA", java.util.Base64.getUrlEncoder().encodeToString(encryptedData)); + result.put("ENC_SESSION_KEY", io.mosip.mock.sbi.util.StringHelper.base64UrlEncode(encryptedSymmetricKey)); + result.put("ENC_DATA", io.mosip.mock.sbi.util.StringHelper.base64UrlEncode(encryptedData)); result.put("TIMESTAMP", timestamp); } catch(Exception ex) { @@ -123,11 +123,11 @@ public static String decrypt(PrivateKey privateKey, String sessionKey, String da byte[] aadBytes = getLastBytes(xorResult, 16); byte[] ivBytes = getLastBytes(xorResult, 12); - byte[] decodedSessionKey = java.util.Base64.getUrlDecoder().decode(sessionKey); + byte[] decodedSessionKey = io.mosip.mock.sbi.util.StringHelper.base64UrlDecode(sessionKey); final byte[] symmetricKey = asymmetricDecrypt(privateKey, decodedSessionKey); SecretKeySpec secretKeySpec = new SecretKeySpec(symmetricKey, "AES"); - byte[] decodedData = java.util.Base64.getUrlDecoder().decode(data); + byte[] decodedData = io.mosip.mock.sbi.util.StringHelper.base64UrlDecode(data); final byte[] decryptedData = symmetricDecrypt(secretKeySpec, decodedData, ivBytes, aadBytes); return new String(decryptedData); @@ -279,14 +279,14 @@ public static void main(String[] args) throws Exception { final byte[] encryptedData = symmetricEncrypt(secretKey, dataBytes, ivBytes, aadBytes); final byte[] encryptedSymmetricKey = asymmetricEncrypt(pair.getPublic(), secretKey.getEncoded()); - String bioValue = java.util.Base64.getUrlEncoder().encodeToString(encryptedData); - String sessionKey = java.util.Base64.getUrlEncoder().encodeToString(encryptedSymmetricKey); + String bioValue = io.mosip.mock.sbi.util.StringHelper.base64UrlEncode(encryptedData); + String sessionKey = io.mosip.mock.sbi.util.StringHelper.base64UrlEncode(encryptedSymmetricKey); - byte[] decodedSessionKey = java.util.Base64.getUrlDecoder().decode(sessionKey); + byte[] decodedSessionKey = io.mosip.mock.sbi.util.StringHelper.base64UrlDecode(sessionKey); final byte[] symmetricKey = asymmetricDecrypt(pair.getPrivate(), decodedSessionKey); SecretKeySpec secretKeySpec = new SecretKeySpec(symmetricKey, "AES"); - byte[] decodedBioValue = java.util.Base64.getUrlDecoder().decode(bioValue); + byte[] decodedBioValue = io.mosip.mock.sbi.util.StringHelper.base64UrlDecode(bioValue); final byte[] decryptedData = symmetricDecrypt(secretKeySpec, decodedBioValue, ivBytes, aadBytes); System.out.println(new String(decryptedData)); } diff --git a/MockMDS/src/main/java/org/biometric/provider/DiscoverRequest.java b/MockMDS/src/main/java/org/biometric/provider/DiscoverRequest.java index a84922e9..ede68a9c 100644 --- a/MockMDS/src/main/java/org/biometric/provider/DiscoverRequest.java +++ b/MockMDS/src/main/java/org/biometric/provider/DiscoverRequest.java @@ -219,7 +219,7 @@ private String getDigitalModality(Map digitalIdMap) throws JsonP digitalMap.put("model", digitalIdMap.get("model")); digitalMap.put("deviceSubType", digitalIdMap.get("deviceSubType")); digitalMap.put("type", digitalIdMap.get("type")); - return Base64.getUrlEncoder().encodeToString(oB.writeValueAsBytes(digitalMap)); + return io.mosip.mock.sbi.util.StringHelper.base64UrlEncode(oB.writeValueAsBytes(digitalMap)); } private String getTimeStamp() {