Skip to content

Commit

Permalink
Merge pull request #143 from ase-101/develop
Browse files Browse the repository at this point in the history
base64 encoding and decoding issue fix
  • Loading branch information
mandeepdhiman123 authored Jan 3, 2022
2 parents 47d7f4a + afaebc0 commit 23fc039
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion MockMDS/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
<groupId>io.mosip.kernel</groupId>
<artifactId>kernel-core</artifactId>
<!-- <version>1.2.0-SNAPSHOT</version> -->
<version>1.0.6</version>
<version>1.2.0-SNAPSHOT</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.bitbucket.b_c/jose4j -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,7 @@ private BioMetricsDto getBiometricData (String transactionId, CaptureRequestDto
X509Certificate certificate = new JwtUtility().getCertificateToEncryptCaptureBioValue();
PublicKey publicKey = certificate.getPublicKey();
Map<String, String> 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") ?
Expand Down Expand Up @@ -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];
Expand Down
10 changes: 5 additions & 5 deletions MockMDS/src/main/java/io/mosip/mock/sbi/util/StringHelper.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected void doAuthCapture(HttpServletRequest request, HttpServletResponse res
X509Certificate certificate = new JwtUtility().getCertificateToEncryptCaptureBioValue();
PublicKey publicKey = certificate.getPublicKey();
Map<String, String> 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);
Expand Down
16 changes: 8 additions & 8 deletions MockMDS/src/main/java/org/biometric/provider/CryptoUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public static Map<String, String> 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) {
Expand All @@ -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);

Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private String getDigitalModality(Map<String, String> 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() {
Expand Down

0 comments on commit 23fc039

Please sign in to comment.