WorldpayCSE { public String encrypt(WPCardData cardData) throws WPCSEException { Set<Integer> errors = new WPCardValidator().validateCardData(cardData); if (errors.isEmpty()) { if (publicKey != null) { return performEncryption(cardData.toString()); } else { throw new WPCSEException("Public key not set"); } } else { throw new WPCSEInvalidCardData(errors); } } WorldpayCSE(); void setPublicKey(String publicKey); WPPublicKey getPublicKey(); void setPublicKey(WPPublicKey publicKey); static boolean isValidPublicKey(String stringKey); String encrypt(WPCardData cardData); static Set<Integer> validate(WPCardData data); static final String RSA_1_5; static final String A_256_GCM; static final String API_VERSION; static final String LIB_VERSION; static final String CHANNEL; }
@Test public void testEncrypt() throws Exception { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.genKeyPair(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); worldpayCSE.setPublicKey(new WPPublicKey(publicKey, "2")); WPCardData cardData = getWPCardData(); String encryptedData = worldpayCSE.encrypt(cardData); JWEObject jweObject = JWEObject.parse(encryptedData); RSADecrypter decrypter = new RSADecrypter(privateKey); jweObject.decrypt(decrypter); JWEHeader header = jweObject.getHeader(); Assert.assertEquals(JWEAlgorithm.RSA1_5.getName(), header.getAlgorithm().getName()); Assert.assertEquals(EncryptionMethod.A256GCM.getName(), header.getEncryptionMethod().getName()); Assert.assertEquals("2", header.getKeyID()); Assert.assertEquals("1.0", header.getCustomParam("com.worldpay.apiVersion")); Assert.assertEquals("1.0.4", header.getCustomParam("com.worldpay.libVersion")); Assert.assertEquals("android", header.getCustomParam("com.worldpay.channel")); String json = jweObject.getPayload().toString(); WPCardData cardDataRet = WPCardData.parseJSON(json); Assert.assertEquals(cardData.getCardHolderName(), cardDataRet.getCardHolderName()); Assert.assertEquals(cardData.getCardNumber(), cardDataRet.getCardNumber()); Assert.assertEquals(cardData.getExpiryMonth(), cardDataRet.getExpiryMonth()); Assert.assertEquals(cardData.getExpiryYear(), cardDataRet.getExpiryYear()); Assert.assertEquals(cardData.getCvc(), cardDataRet.getCvc()); }
@Test(expected = WPCSEInvalidCardData.class) public void testEncryptInvalid() throws Exception { WPCardData cardData = new WPCardData(); cardData.setCardNumber("122121"); cardData.setCvc("1"); cardData.setExpiryMonth(""); cardData.setExpiryYear("11"); cardData.setCardHolderName(""); worldpayCSE.encrypt(cardData); }
@Test(expected = WPCSEException.class) public void testEncryptKeyNotSet() throws Exception { WPCardData cardData = getWPCardData(); worldpayCSE.encrypt(cardData); }