Prechádzať zdrojové kódy

add exception when try to decrypt with wrong key

Daniel Bohry 8 mesiacov pred
rodič
commit
ae3515d56a

+ 6 - 0
src/main/java/com/danielbohry/stocks/api/GlobalExceptionHandler.java

@@ -1,6 +1,7 @@
 package com.danielbohry.stocks.api;
 
 
+import com.danielbohry.stocks.exception.DecryptionKeyException;
 import com.danielbohry.stocks.exception.Error;
 import com.danielbohry.stocks.exception.UnauthorizedException;
 import feign.FeignException;
@@ -28,4 +29,9 @@ public class GlobalExceptionHandler {
         return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new Error(e.getMessage()));
     }
 
+    @ExceptionHandler(DecryptionKeyException.class)
+    public ResponseEntity<Error> handleDecryptionKeyException(DecryptionKeyException e, WebRequest request) {
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new Error(e.getMessage()));
+    }
+
 }

+ 16 - 0
src/main/java/com/danielbohry/stocks/exception/DecryptionKeyException.java

@@ -0,0 +1,16 @@
+package com.danielbohry.stocks.exception;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(value = HttpStatus.BAD_REQUEST)
+public class DecryptionKeyException extends RuntimeException {
+
+    public DecryptionKeyException(String message) {
+        super(message);
+    }
+
+    public DecryptionKeyException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}

+ 16 - 18
src/main/java/com/danielbohry/stocks/service/EncryptService.java

@@ -1,9 +1,10 @@
 package com.danielbohry.stocks.service;
 
-import com.danielbohry.stocks.repository.portfolio.PortfolioEntity;
+import com.danielbohry.stocks.exception.DecryptionKeyException;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
@@ -40,27 +41,24 @@ public class EncryptService {
     }
 
     public String decrypt(String encryptedData) throws Exception {
-        SecretKeySpec key = getSecretKey();
+        try {
+            SecretKeySpec key = getSecretKey();
 
-        byte[] combined = Base64.getDecoder().decode(encryptedData);
-        byte[] iv = new byte[IV_LENGTH];
-        byte[] encrypted = new byte[combined.length - IV_LENGTH];
+            byte[] combined = Base64.getDecoder().decode(encryptedData);
+            byte[] iv = new byte[IV_LENGTH];
+            byte[] encrypted = new byte[combined.length - IV_LENGTH];
 
-        System.arraycopy(combined, 0, iv, 0, IV_LENGTH);
-        System.arraycopy(combined, IV_LENGTH, encrypted, 0, encrypted.length);
+            System.arraycopy(combined, 0, iv, 0, IV_LENGTH);
+            System.arraycopy(combined, IV_LENGTH, encrypted, 0, encrypted.length);
 
-        Cipher cipher = Cipher.getInstance(ALGORITHM);
-        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
-
-        byte[] decrypted = cipher.doFinal(encrypted);
-        return new String(decrypted, StandardCharsets.UTF_8);
-    }
+            Cipher cipher = Cipher.getInstance(ALGORITHM);
+            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
 
-    public boolean isEncrypted(PortfolioEntity entity) {
-        return entity != null &&
-            entity.getEncryptedStocks() != null &&
-            !entity.getEncryptedStocks().isEmpty() &&
-            (entity.getStocks() == null || entity.getStocks().isEmpty());
+            byte[] decrypted = cipher.doFinal(encrypted);
+            return new String(decrypted, StandardCharsets.UTF_8);
+        } catch (BadPaddingException e) {
+            throw new DecryptionKeyException("Cannot decrypt data", e);
+        }
     }
 
     private SecretKeySpec getSecretKey() {