Best Cerberus-source code snippet using org.cerberus.service.encryption.EncryptionService.decrypt
Source:EncryptionServiceTest.java  
...41import org.mockito.Mockito;42import org.mockito.MockitoAnnotations;43public class EncryptionServiceTest {44  @Mock private Region currentRegion;45  @Mock private CryptoMaterialsManager decryptCryptoMaterialsManager;46  @Mock private CryptoMaterialsManager encryptCryptoMaterialsManager;47  @Mock private AwsCrypto awsCrypto;48  @Before49  public void setup() {50    MockitoAnnotations.initMocks(this);51  }52  @Test53  public void testArnFormatIsWrong() {54    String exceptionMessage = "";55    try {56      new EncryptionService(57          awsCrypto,58          "cmk",59          decryptCryptoMaterialsManager,60          encryptCryptoMaterialsManager,61          Region.getRegion(Regions.US_WEST_2));62    } catch (IllegalArgumentException illegalArgumentException) {63      exceptionMessage = illegalArgumentException.getMessage();64    }65    Assert.assertEquals(66        "At least 2 CMK ARNs are required for high availability, size:1", exceptionMessage);67  }68  @Test69  public void testEncryptWithString() {70    String expectedEncryptedValue = "encryptedValue";71    EncryptionService encryptionService = getEncryptionService();72    CryptoResult<String, ?> cryptoResult = getCryptoResult(expectedEncryptedValue);73    Mockito.when(74            awsCrypto.encryptString(75                Mockito.eq(encryptCryptoMaterialsManager),76                Mockito.eq("plainText"),77                Mockito.anyMap()))78        .thenReturn(cryptoResult);79    String encryptedValue = encryptionService.encrypt("plainText", "sdbPath");80    Assert.assertEquals(expectedEncryptedValue, encryptedValue);81  }82  @Test83  public void testEncryptWithBytes() {84    String expectedEncryptedValue = "encryptedValue";85    EncryptionService encryptionService = getEncryptionService();86    CryptoResult<byte[], ?> cryptoResult = getCryptoResultBytes(expectedEncryptedValue);87    Mockito.when(88            awsCrypto.encryptData(89                Mockito.eq(encryptCryptoMaterialsManager),90                Mockito.eq("plainText".getBytes(StandardCharsets.UTF_8)),91                Mockito.anyMap()))92        .thenReturn(cryptoResult);93    byte[] encryptedBytes =94        encryptionService.encrypt("plainText".getBytes(StandardCharsets.UTF_8), "sdbPath");95    Assert.assertEquals(expectedEncryptedValue, new String(encryptedBytes, StandardCharsets.UTF_8));96  }97  @Test98  public void testDecryptWhenEncryptionContentDidNotHaveExpectedPath() {99    EncryptionService encryptionService = Mockito.spy(getEncryptionService());100    ParsedCiphertext parsedCiphertext = getParsedCipherText();101    Mockito.doReturn(parsedCiphertext)102        .when(encryptionService)103        .getParsedCipherText("encryptedPayload");104    Map<String, String> contextMap = new HashMap<>();105    Mockito.when(parsedCiphertext.getEncryptionContextMap()).thenReturn(contextMap);106    String exceptionMessage = "";107    try {108      encryptionService.decrypt("encryptedPayload", "sdbPath");109    } catch (IllegalArgumentException illegalArgumentException) {110      exceptionMessage = illegalArgumentException.getMessage();111    }112    assertEquals(113        "EncryptionContext did not have expected path, possible tampering: sdbPath",114        exceptionMessage);115  }116  @Test117  public void testDecryptWhenEncryptionContent() {118    EncryptionService encryptionService = Mockito.spy(getEncryptionService());119    ParsedCiphertext parsedCiphertext = getParsedCipherText();120    Mockito.doReturn(parsedCiphertext)121        .when(encryptionService)122        .getParsedCipherText("encryptedPayload");123    Map<String, String> contextMap = new HashMap<>();124    contextMap.put(SDB_PATH_PROPERTY_NAME, "sdbPath");125    Mockito.when(parsedCiphertext.getEncryptionContextMap()).thenReturn(contextMap);126    CryptoResult cryptoResult = getCryptoResultBytes("decryptedData");127    Mockito.when(128            awsCrypto.decryptData(129                Mockito.any(CryptoMaterialsManager.class), Mockito.any(ParsedCiphertext.class)))130        .thenReturn(cryptoResult);131    String decryptedData = encryptionService.decrypt("encryptedPayload", "sdbPath");132    Assert.assertEquals("decryptedData", decryptedData);133  }134  @Test135  public void testDecryptBytesWhenEncryptionContentDidNotHaveExpectedPath() {136    EncryptionService encryptionService = Mockito.spy(getEncryptionService());137    ParsedCiphertext parsedCiphertext = getParsedCipherText();138    Mockito.doReturn(parsedCiphertext)139        .when(encryptionService)140        .getParsedCipherText("encryptedPayload".getBytes(StandardCharsets.UTF_8));141    Map<String, String> contextMap = new HashMap<>();142    Mockito.when(parsedCiphertext.getEncryptionContextMap()).thenReturn(contextMap);143    String exceptionMessage = "";144    try {145      encryptionService.decrypt("encryptedPayload".getBytes(StandardCharsets.UTF_8), "sdbPath");146    } catch (IllegalArgumentException illegalArgumentException) {147      exceptionMessage = illegalArgumentException.getMessage();148    }149    assertEquals(150        "EncryptionContext did not have expected path, possible tampering: sdbPath",151        exceptionMessage);152  }153  @Test154  public void testDecryptBytesWhenEncryptionContent() {155    EncryptionService encryptionService = Mockito.spy(getEncryptionService());156    ParsedCiphertext parsedCiphertext = getParsedCipherText();157    Mockito.doReturn(parsedCiphertext)158        .when(encryptionService)159        .getParsedCipherText("encryptedPayload".getBytes(StandardCharsets.UTF_8));160    Map<String, String> contextMap = new HashMap<>();161    contextMap.put(SDB_PATH_PROPERTY_NAME, "sdbPath");162    Mockito.when(parsedCiphertext.getEncryptionContextMap()).thenReturn(contextMap);163    CryptoResult cryptoResult = getCryptoResultBytes("decryptedData");164    Mockito.when(165            awsCrypto.decryptData(166                Mockito.any(CryptoMaterialsManager.class), Mockito.any(ParsedCiphertext.class)))167        .thenReturn(cryptoResult);168    byte[] decryptedData =169        encryptionService.decrypt("encryptedPayload".getBytes(StandardCharsets.UTF_8), "sdbPath");170    Assert.assertEquals("decryptedData", new String(decryptedData, StandardCharsets.UTF_8));171  }172  @Test173  public void testReEncryptWhenEncryptionContentDidNotHaveExpectedPath() {174    EncryptionService encryptionService = Mockito.spy(getEncryptionService());175    ParsedCiphertext parsedCiphertext = getParsedCipherText();176    Mockito.doReturn(parsedCiphertext)177        .when(encryptionService)178        .getParsedCipherText("encryptedPayload");179    Map<String, String> contextMap = new HashMap<>();180    Mockito.when(parsedCiphertext.getEncryptionContextMap()).thenReturn(contextMap);181    String exceptionMessage = "";182    try {183      encryptionService.reencrypt("encryptedPayload", "sdbPath");184    } catch (IllegalArgumentException illegalArgumentException) {185      exceptionMessage = illegalArgumentException.getMessage();186    }187    assertEquals(188        "EncryptionContext did not have expected path, possible tampering: sdbPath",189        exceptionMessage);190  }191  @Test192  public void testReEncryptWithBytes() {193    EncryptionService encryptionService = Mockito.spy(getEncryptionService());194    ParsedCiphertext parsedCiphertext = getParsedCipherText();195    Mockito.doReturn(parsedCiphertext)196        .when(encryptionService)197        .getParsedCipherText("encryptedPayload".getBytes(StandardCharsets.UTF_8));198    Map<String, String> contextMap = new HashMap<>();199    contextMap.put(SDB_PATH_PROPERTY_NAME, "sdbPath");200    Mockito.when(parsedCiphertext.getEncryptionContextMap()).thenReturn(contextMap);201    CryptoResult cryptoResult = getCryptoResultBytes("decryptedData");202    Mockito.when(203            awsCrypto.decryptData(204                Mockito.any(CryptoMaterialsManager.class), Mockito.any(ParsedCiphertext.class)))205        .thenReturn(cryptoResult);206    CryptoResult<byte[], ?> encryptedCryptoResult = getCryptoResultBytes("encryptedData");207    Mockito.when(208            awsCrypto.encryptData(209                Mockito.eq(encryptCryptoMaterialsManager),210                Mockito.eq("decryptedData".getBytes(StandardCharsets.UTF_8)),211                Mockito.anyMap()))212        .thenReturn(encryptedCryptoResult);213    byte[] reEncryptedData =214        encryptionService.reencrypt("encryptedPayload".getBytes(StandardCharsets.UTF_8), "sdbPath");215    Assert.assertEquals("encryptedData", new String(reEncryptedData, StandardCharsets.UTF_8));216  }217  @Test218  public void testReEncryptWithString() {219    EncryptionService encryptionService = Mockito.spy(getEncryptionService());220    ParsedCiphertext parsedCiphertext = getParsedCipherText();221    Mockito.doReturn(parsedCiphertext)222        .when(encryptionService)223        .getParsedCipherText("encryptedPayload");224    Map<String, String> contextMap = new HashMap<>();225    contextMap.put(SDB_PATH_PROPERTY_NAME, "sdbPath");226    Mockito.when(parsedCiphertext.getEncryptionContextMap()).thenReturn(contextMap);227    CryptoResult cryptoResult = getCryptoResultBytes("decryptedData");228    Mockito.when(229            awsCrypto.decryptData(230                Mockito.any(CryptoMaterialsManager.class), Mockito.any(ParsedCiphertext.class)))231        .thenReturn(cryptoResult);232    CryptoResult<String, ?> encryptedCryptoResult = getCryptoResult("encryptedData");233    Mockito.when(234            awsCrypto.encryptString(235                Mockito.eq(encryptCryptoMaterialsManager),236                Mockito.eq("decryptedData"),237                Mockito.anyMap()))238        .thenReturn(encryptedCryptoResult);239    String reEncryptedData = encryptionService.reencrypt("encryptedPayload", "sdbPath");240    Assert.assertEquals("encryptedData", reEncryptedData);241  }242  @Test243  public void testReEncryptBytesWhenEncryptionContentDidNotHaveExpectedPath() {244    EncryptionService encryptionService = Mockito.spy(getEncryptionService());245    ParsedCiphertext parsedCiphertext = getParsedCipherText();246    Mockito.doReturn(parsedCiphertext)247        .when(encryptionService)248        .getParsedCipherText("encryptedPayload".getBytes(StandardCharsets.UTF_8));249    Map<String, String> contextMap = new HashMap<>();250    Mockito.when(parsedCiphertext.getEncryptionContextMap()).thenReturn(contextMap);251    String exceptionMessage = "";252    try {253      encryptionService.reencrypt("encryptedPayload".getBytes(StandardCharsets.UTF_8), "sdbPath");254    } catch (IllegalArgumentException illegalArgumentException) {255      exceptionMessage = illegalArgumentException.getMessage();256    }257    assertEquals(258        "EncryptionContext did not have expected path, possible tampering: sdbPath",259        exceptionMessage);260  }261  private ParsedCiphertext getParsedCipherText() {262    ParsedCiphertext parsedCiphertext = Mockito.mock(ParsedCiphertext.class);263    byte b = 0;264    Mockito.when(parsedCiphertext.getVersion()).thenReturn(b);265    Mockito.when(parsedCiphertext.getType())266        .thenReturn(CiphertextType.CUSTOMER_AUTHENTICATED_ENCRYPTED_DATA);267    Mockito.when(parsedCiphertext.getCryptoAlgoId())268        .thenReturn(CryptoAlgorithm.ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384);269    Mockito.when(parsedCiphertext.getMessageId())270        .thenReturn("messageId".getBytes(StandardCharsets.UTF_8));271    Mockito.when(parsedCiphertext.getEncryptedKeyBlobCount()).thenReturn(1);272    Mockito.when(parsedCiphertext.getContentType()).thenReturn(ContentType.SINGLEBLOCK);273    return parsedCiphertext;274  }275  private EncryptionService getEncryptionService() {276    EncryptionService encryptionService =277        new EncryptionService(278            awsCrypto,279            "cmk,Arns,cmk",280            decryptCryptoMaterialsManager,281            encryptCryptoMaterialsManager,282            Region.getRegion(Regions.US_WEST_2));283    return encryptionService;284  }285  private CryptoResult<String, ?> getCryptoResult(String value) {286    CryptoResult<String, ?> cryptoResult = Mockito.mock(CryptoResult.class);287    Mockito.when(cryptoResult.getResult()).thenReturn(value);288    return cryptoResult;289  }290  private CryptoResult<byte[], ?> getCryptoResultBytes(String value) {291    CryptoResult<byte[], ?> cryptoResult = Mockito.mock(CryptoResult.class);292    Mockito.when(cryptoResult.getResult()).thenReturn(value.getBytes(StandardCharsets.UTF_8));293    return cryptoResult;294  }...Source:AESGCMCipher.java  
...34import java.security.NoSuchAlgorithmException;35import java.util.Arrays;36public class AESGCMCipher implements SymmetricCipher {37    private Cipher encrypt;38    private Cipher decrypt;39    private SecretKey key;40    private int TLLength;41    private int TLByteLength;42    private CerberusEncrypt encryptionService;43    public AESGCMCipher(SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException {44        this(key, 128, 16);45    }46    public AESGCMCipher(SecretKey key, int TLLength, int TLByteLength) throws NoSuchPaddingException, NoSuchAlgorithmException {47        encrypt = Cipher.getInstance("AES/GCM/NoPadding");48        decrypt = Cipher.getInstance("AES/GCM/NoPadding");49        this.TLLength = TLLength;50        this.TLByteLength = TLByteLength;51        this.key = key;52    }53    @Override54    public CerberusCipher reset() {55        return this;56    }57    @Override58    public SecretKey getKeySet() {59        return key;60    }61    @Override62    public byte[] encrypt(byte[] value, int offset, int length) {63        try {64            byte[] gcmBytes = getEncryptionService().randomIV(TLByteLength);65            encrypt.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(TLLength, gcmBytes));66            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1 + TLByteLength + length);67            outputStream.write((byte) TLByteLength);68            outputStream.write(gcmBytes);69            CipherOutputStream cipherStream = new CipherOutputStream(outputStream, encrypt);70            cipherStream.write(value, offset, length);71            cipherStream.flush();72            cipherStream.close();73            Arrays.fill(gcmBytes, (byte) 0); // wipe gcm from memory74            return outputStream.toByteArray();75        } catch (InvalidKeyException | InvalidAlgorithmParameterException | IOException e) {76            CerberusRegistry.getInstance().warning("Failed to encrypt AES/GCM data!");77            CerberusRegistry.getInstance().getService(CerberusEvent.class)78                    .executeFullEIF(new ExceptionEvent(CerberusEncrypt.class, e));79        }80        return null;81    }82    @Override83    public byte[] encrypt(byte[] value) {84        return encrypt(value, 0, value.length);85    }86    @Override87    public byte[] decrypt(byte[] value, int offset, int length) {88        try {89            ByteArrayInputStream inputStream = new ByteArrayInputStream(value, offset, length);90            int gcmLength = Byte.toUnsignedInt((byte) inputStream.read());91            byte[] gcmBytes = new byte[gcmLength];92            int read = inputStream.read(gcmBytes);93            if (read < gcmLength)94                throw new IllegalStateException("Invalid gcm head size AES stream!");95            GCMParameterSpec gcm = new GCMParameterSpec(TLLength, gcmBytes);96            decrypt.init(Cipher.DECRYPT_MODE, key, gcm);97            CipherInputStream cipherStream = new CipherInputStream(inputStream, decrypt);98            byte[] data = new byte[length - 1 - gcmLength];99            read = cipherStream.read(data);100            inputStream.close();101            if (read < 0)102                throw new IllegalStateException("Invalid data set length!");103            byte[] finalBytes = new byte[read];104            System.arraycopy(data, 0, finalBytes, 0, read);105            return finalBytes;106        } catch (InvalidAlgorithmParameterException | InvalidKeyException | IOException e) {107            e.printStackTrace();108        }109        return null;110    }111    @Override112    public byte[] decrypt(byte[] value) {113        return decrypt(value, 0, value.length);114    }115    private CerberusEncrypt getEncryptionService() {116        if (encryptionService == null)117            encryptionService = CerberusRegistry.getInstance().getService(CerberusEncrypt.class);118        return encryptionService;119    }120}...Source:EncryptionService.java  
...54        return null;55    }56    57    58    public static String decrypt(String encrypted) {59    try {60        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));61        SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("UTF-8"), "AES");62 63        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");64        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);65        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));66 67        return new String(original);68    } catch (UnsupportedEncodingException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException ex) {69        LOG.warn(ex.toString());70    }71 72    return null;...decrypt
Using AI Code Generation
1package org.cerberus.service.encryption;2import org.springframework.beans.factory.annotation.Autowired;3import org.springframework.stereotype.Service;4public class EncryptionService {5    private IEncryption encryptionImplementation;6    public String decrypt(String encryptedText) {7        return encryptionImplementation.decrypt(encryptedText);8    }9}10package org.cerberus.service.encryption;11import org.springframework.beans.factory.annotation.Autowired;12import org.springframework.stereotype.Service;13public class EncryptionService {14    private IEncryption encryptionImplementation;15    public String encrypt(String text) {16        return encryptionImplementation.encrypt(text);17    }18}19package org.cerberus.service.encryption;20import org.springframework.beans.factory.annotation.Autowired;21import org.springframework.stereotype.Service;22public class EncryptionService {23    private IEncryption encryptionImplementation;24    public String encrypt(String text) {25        return encryptionImplementation.encrypt(text);26    }27    public String decrypt(String encryptedText) {28        return encryptionImplementation.decrypt(encryptedText);29    }30}31package org.cerberus.service.encryption;32import org.springframework.beans.factory.annotation.Autowired;33import org.springframework.stereotype.Service;34public class EncryptionService {35    private IEncryption encryptionImplementation;36    public String encrypt(String text) {37        return encryptionImplementation.encrypt(text);38    }39    public String decrypt(String encryptedText) {40        return encryptionImplementation.decrypt(encryptedText);41    }42}43package org.cerberus.service.encryption;44import org.springframework.beans.factory.annotation.Autowired;45import org.springframework.stereotype.Service;46public class EncryptionService {47    private IEncryption encryptionImplementation;48    public String encrypt(String text) {49        return encryptionImplementation.encrypt(text);50    }51    public String decrypt(String encryptedText) {52        return encryptionImplementation.decrypt(encryptedText);53    }54}decrypt
Using AI Code Generation
1import org.cerberus.service.encryption.EncryptionService;2import org.cerberus.util.StringUtil;3public class 3 {4    public static void main(String[] args) {5        String encryptedPassword = "encryptedPassword";6        String decryptedPassword = EncryptionService.decrypt(encryptedPassword);7        System.out.println(decryptedPassword);8    }9}10import org.cerberus.service.encryption.EncryptionService;11import org.cerberus.util.StringUtil;12public class 4 {13    public static void main(String[] args) {14        String password = "password";15        String encryptedPassword = EncryptionService.encrypt(password);16        System.out.println(encryptedPassword);17    }18}19import org.cerberus.service.encryption.EncryptionService;20import org.cerberus.util.StringUtil;21public class 5 {22    public static void main(String[] args) {23        String password = "password";24        String encryptedPassword = EncryptionService.encrypt(password);25        System.out.println(encryptedPassword);26    }27}28import org.cerberus.service.encryption.EncryptionService;29import org.cerberus.util.StringUtil;30public class 6 {31    public static void main(String[] args) {32        String key = EncryptionService.getKey();33        System.out.println(key);34    }35}36import org.cerberus.service.encryption.EncryptionService;37import org.cerberus.util.StringUtil;38public class 7 {39    public static void main(String[] args) {40        String key = EncryptionService.getKey();41        System.out.println(key);42    }43}44import org.cerberus.service.encryption.EncryptionService;45import org.cerberus.util.StringUtil;46public class 8 {decrypt
Using AI Code Generation
1package com.cerberus.service.encryption;2import org.cerberus.service.encryption.EncryptionService;3public class test {4    public static void main(String[] args) {5        String password = "password";6        String encryptedPassword = EncryptionService.encrypt(password);7        System.out.println("Encrypted password: " + encryptedPassword);8        String decryptedPassword = EncryptionService.decrypt(encryptdecrypt
Using AI Code Generation
1import org.cerberus.service.encryption.EncryptionService;2import org.springframework.security.crypto.codec.Base64;3public class 3 {4    public static void main(String[] args) {5        String encrypted = "XvG2Q7QI+H6aJ7wD1i6ZcQ==";6        byte[] decoded = Base64.decode(encrypted.getBytes());7        String decrypted = new String(EncryptionService.decrypt(decoded));8        System.out.println(decrypted);9    }10}Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
