Search in sources :

Example 1 with AESEncrypter

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

the class StringUtils method getEncryptedString.

/**
 * Get encrypted string
 *
 * @param type            key type
 * @param decryptedString decrypted string
 * @return encrypted string
 */
public static String getEncryptedString(@KeysManager.KeyType int type, String decryptedString) {
    JWEAlgorithm alg = JWEAlgorithm.A256GCMKW;
    EncryptionMethod enc = EncryptionMethod.A256GCM;
    ECCKeyPair keyPair = KeysManager.getInstance().getPairForType(type);
    if (null == keyPair) {
        throw new IllegalStateException("No key pair available for type (type = " + type + ")");
    }
    JWEHeader.Builder jweHeaderBuilder = new JWEHeader.Builder(alg, enc).contentType("application/json").keyID(keyPair.getKeyId());
    JWEHeader header = jweHeaderBuilder.build();
    Payload payload = new Payload(decryptedString);
    JWEObject jweObject = new JWEObject(header, payload);
    try {
        JWEEncrypter encrypter = new AESEncrypter(KeysManager.getInstance().getSecretKey(type));
        jweObject.encrypt(encrypter);
    } catch (JOSEException e) {
        FPLog.e(e);
    }
    return jweObject.serialize();
}
Also used : AESEncrypter(com.nimbusds.jose.crypto.AESEncrypter) JWEHeader(com.nimbusds.jose.JWEHeader) JWEObject(com.nimbusds.jose.JWEObject) JWEAlgorithm(com.nimbusds.jose.JWEAlgorithm) Payload(com.nimbusds.jose.Payload) JWEEncrypter(com.nimbusds.jose.JWEEncrypter) EncryptionMethod(com.nimbusds.jose.EncryptionMethod) ECCKeyPair(com.fitpay.android.api.models.security.ECCKeyPair) JOSEException(com.nimbusds.jose.JOSEException)

Aggregations

ECCKeyPair (com.fitpay.android.api.models.security.ECCKeyPair)1 EncryptionMethod (com.nimbusds.jose.EncryptionMethod)1 JOSEException (com.nimbusds.jose.JOSEException)1 JWEAlgorithm (com.nimbusds.jose.JWEAlgorithm)1 JWEEncrypter (com.nimbusds.jose.JWEEncrypter)1 JWEHeader (com.nimbusds.jose.JWEHeader)1 JWEObject (com.nimbusds.jose.JWEObject)1 Payload (com.nimbusds.jose.Payload)1 AESEncrypter (com.nimbusds.jose.crypto.AESEncrypter)1