package com.monkeyk.os.infrastructure;

import org.jose4j.jwe.ContentEncryptionAlgorithmIdentifiers;
import org.jose4j.jwe.JsonWebEncryption;
import org.jose4j.jwe.KeyManagementAlgorithmIdentifiers;
import org.jose4j.keys.AesKey;
import org.jose4j.lang.ByteUtil;
import org.testng.annotations.Test;

import java.security.Key;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

/**
 * 2016/12/15
 *
 * @author Shengzhao Li
 */
public class Jose4JTest {


    /*
    * AES 加密与解密
    * */

    @Test
    public void aesEncryptDecrypt() throws Exception {

        String keyText = "iue98623diDEs096";
        String data = "I am marico";
        Key key = new AesKey(keyText.getBytes());

        //加密
        JsonWebEncryption jwe = new JsonWebEncryption();
        jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.A128KW);
        jwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);
        jwe.setKey(key);
        jwe.setPayload(data);

        String idToken = jwe.getCompactSerialization();
        assertNotNull(idToken);
        System.out.println(data + " idToken: " + idToken);

        //解密
        JsonWebEncryption jwe2 = new JsonWebEncryption();
        jwe2.setKey(key);
        jwe2.setCompactSerialization(idToken);

        final String payload = jwe2.getPayload();
        assertNotNull(payload);
        assertEquals(payload, data);


    }


}

@@ -227,41 +227,8 @@
 ey;%0A
-import org.jose4j.lang.ByteUtil;%0A
 impo
@@ -259,16 +259,16 @@
 s.Test;%0A
+
 %0Aimport 
@@ -480,16 +480,22 @@
 ES %E5%8A%A0%E5%AF%86%E4%B8%8E%E8%A7%A3%E5%AF%86
+, 128%E4%BD%8D
 %0A    * *
@@ -496,17 +496,16 @@
    * */%0A
-%0A
     @Tes
@@ -539,16 +539,19 @@
 tDecrypt
+128
 () throw
@@ -1428,16 +1428,1009 @@
 data);%0A%0A
+    %7D%0A%0A%0A    /*%0A    * AES %E5%8A%A0%E5%AF%86%E4%B8%8E%E8%A7%A3%E5%AF%86, 256%E4%BD%8D%0A    * */%0A    @Test%0A    public void aesEncryptDecrypt256() throws Exception %7B%0A%0A        String keyText = %22iue98623diDEs096_8u@idls(*JKse09%22;%0A        String data = %22I am marico%22;%0A        Key key = new AesKey(keyText.getBytes());%0A%0A        //%E5%8A%A0%E5%AF%86%0A        JsonWebEncryption jwe = new JsonWebEncryption();%0A        jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.A256KW);%0A        jwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_256_CBC_HMAC_SHA_512);%0A        jwe.setKey(key);%0A        jwe.setPayload(data);%0A%0A        String idToken = jwe.getCompactSerialization();%0A        assertNotNull(idToken);%0A        System.out.println(data + %22 idToken: %22 + idToken);%0A%0A        //%E8%A7%A3%E5%AF%86%0A        JsonWebEncryption jwe2 = new JsonWebEncryption();%0A        jwe2.setKey(key);%0A        jwe2.setCompactSerialization(idToken);%0A%0A        final String payload = jwe2.getPayload();%0A        assertNotNull(payload);%0A        assertEquals(payload, data);%0A
 %0A    %7D%0A%0A

Add Jose4JTest