Search in sources :

Example 1 with AESDecrypter

use of com.nimbusds.jose.crypto.AESDecrypter in project fitpay-android-sdk by fitpay.

the class StringUtils method getDecryptedString.

/**
 * Get decrypted string
 *
 * @param type            key type
 * @param encryptedString encrypted string
 * @return decrypted string
 */
public static String getDecryptedString(@KeysManager.KeyType int type, String encryptedString) {
    KeysManager keysManager = KeysManager.getInstance();
    JWEObject jweObject;
    try {
        jweObject = JWEObject.parse(encryptedString);
        JWEHeader jweHeader = jweObject.getHeader();
        if (jweHeader.getKeyID() == null || jweHeader.getKeyID().equals(keysManager.getKeyId(type))) {
            jweObject.decrypt(new AESDecrypter(keysManager.getSecretKey(type)));
            if ("JWT".equals(jweObject.getHeader().getContentType())) {
                SignedJWT signedJwt = jweObject.getPayload().toSignedJWT();
                ECCKeyPair keyPair = keysManager.getPairForType(type);
                ECPublicKey key = null;
                if ("https://fit-pay.com".equals(signedJwt.getJWTClaimsSet().getIssuer())) {
                    key = (ECPublicKey) keysManager.getPublicKey("EC", Hex.hexStringToBytes(keyPair.getServerPublicKey()));
                } else {
                    key = (ECPublicKey) keysManager.getPublicKey("EC", Hex.hexStringToBytes(keyPair.getPublicKey()));
                }
                JWSVerifier verifier = new ECDSAVerifier(key);
                if (!signedJwt.verify(verifier)) {
                    throw new IllegalArgumentException("jwt did not pass signature validation");
                }
                return signedJwt.getJWTClaimsSet().getStringClaim("data");
            } else {
                return jweObject.getPayload().toString();
            }
        }
    } catch (Exception e) {
        FPLog.e(e);
    }
    return null;
}
Also used : ECDSAVerifier(com.nimbusds.jose.crypto.ECDSAVerifier) JWEHeader(com.nimbusds.jose.JWEHeader) ECPublicKey(java.security.interfaces.ECPublicKey) JWEObject(com.nimbusds.jose.JWEObject) JWSVerifier(com.nimbusds.jose.JWSVerifier) AESDecrypter(com.nimbusds.jose.crypto.AESDecrypter) SignedJWT(com.nimbusds.jwt.SignedJWT) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair) JOSEException(com.nimbusds.jose.JOSEException)

Aggregations

ECCKeyPair (com.fitpay.android.api.models.security.ECCKeyPair)1 JOSEException (com.nimbusds.jose.JOSEException)1 JWEHeader (com.nimbusds.jose.JWEHeader)1 JWEObject (com.nimbusds.jose.JWEObject)1 JWSVerifier (com.nimbusds.jose.JWSVerifier)1 AESDecrypter (com.nimbusds.jose.crypto.AESDecrypter)1 ECDSAVerifier (com.nimbusds.jose.crypto.ECDSAVerifier)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1 ECPublicKey (java.security.interfaces.ECPublicKey)1