Java Code Examples for org.apache.tomcat.util.codec.binary.Base64#encodeBase64String()

The following examples show how to use org.apache.tomcat.util.codec.binary.Base64#encodeBase64String() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AesUtil.java    From springboot-learn with MIT License 6 votes vote down vote up
/**
 * AES加密
 *
 * @param passwd  加密的密钥
 * @param content 需要加密的字符串
 * @return 返回Base64转码后的加密数据
 * @throws Exception
 */
public static String encrypt(String passwd, String content) throws Exception {
    // 创建密码器
    Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

    byte[] byteContent = content.getBytes("utf-8");

    // 初始化为加密模式的密码器
    cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(passwd));

    // 加密
    byte[] result = cipher.doFinal(byteContent);

    //通过Base64转码返回
    return Base64.encodeBase64String(result);
}
 
Example 2
Source File: AesUtil.java    From base-admin with MIT License 5 votes vote down vote up
/**
 * 加密
 *
 * @param content    加密的字符串
 * @param encryptKey key值
 */
public static String encrypt(String content, String encryptKey) throws Exception {
    //设置Cipher对象
    Cipher cipher = Cipher.getInstance(ALGORITHMS, PROVIDER);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), KEY_ALGORITHM));

    //调用doFinal
    // 转base64
    return Base64.encodeBase64String(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));

}
 
Example 3
Source File: TestNonLoginAndBasicAuthenticator.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private BasicCredentials(String aMethod,
        String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes =
            userCredentials.getBytes(B2CConverter.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials= method + " " + base64auth;
}
 
Example 4
Source File: TestSSOnonLoginAndBasicAuthenticator.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private BasicCredentials(String aMethod,
        String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes =
            userCredentials.getBytes(B2CConverter.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials= method + " " + base64auth;
}
 
Example 5
Source File: Test001Records.java    From java-crud-api with MIT License 5 votes vote down vote up
@Test
public void test037EditCategoryWithBinaryContentWithPost() throws Exception {
	String string = "€ \000abc\000\n\r\\b\000";
	String binary = Base64.encodeBase64String(string.getBytes("UTF-8"));
	String b64url = Base64.encodeBase64URLSafeString(string.getBytes("UTF-8"));
	mockMvc.perform(
			put("/records/categories/2").contentType("application/x-www-form-urlencoded").content("icon=" + b64url))
			.andExpect(status().isOk()).andExpect(content().string("1"));
	mockMvc.perform(get("/records/categories/2")).andExpect(status().isOk())
			.andExpect(content().string("{\"id\":2,\"name\":\"article\",\"icon\":\"" + binary + "\"}"));
}
 
Example 6
Source File: Test001Records.java    From java-crud-api with MIT License 5 votes vote down vote up
@Test
public void test035EditCategoryWithBinaryContent() throws Exception {
	String string = "€ \000abc\000\n\r\\b\000";
	String binary = Base64.encodeBase64String(string.getBytes("UTF-8"));
	String b64url = Base64.encodeBase64URLSafeString(string.getBytes("UTF-8"));
	mockMvc.perform(
			put("/records/categories/2").contentType("application/json").content("{\"icon\":\"" + b64url + "\"}"))
			.andExpect(status().isOk()).andExpect(content().string("1"));
	mockMvc.perform(get("/records/categories/2")).andExpect(status().isOk())
			.andExpect(content().string("{\"id\":2,\"name\":\"article\",\"icon\":\"" + binary + "\"}"));
}
 
Example 7
Source File: TestRestCsrfPreventionFilter2.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private BasicCredentials(String aMethod, String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes = userCredentials.getBytes(B2CConverter.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials = method + " " + base64auth;
}
 
Example 8
Source File: TestNonLoginAndBasicAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private BasicCredentials(String aMethod,
        String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes =
            userCredentials.getBytes(B2CConverter.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials= method + " " + base64auth;
}
 
Example 9
Source File: TestSSOnonLoginAndBasicAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private BasicCredentials(String aMethod,
        String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes =
            userCredentials.getBytes(B2CConverter.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials= method + " " + base64auth;
}
 
Example 10
Source File: BasicAuthenticator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public String getAuthorization(String requestUri, String WWWAuthenticate,
        Map<String, Object> userProperties) throws AuthenticationException {

    String userName = (String) userProperties.get(Constants.WS_AUTHENTICATION_USER_NAME);
    String password = (String) userProperties.get(Constants.WS_AUTHENTICATION_PASSWORD);

    if (userName == null || password == null) {
        throw new AuthenticationException(
                "Failed to perform Basic authentication due to  missing user/password");
    }

    Map<String, String> wwwAuthenticate = parseWWWAuthenticateHeader(WWWAuthenticate);

    String userPass = userName + ":" + password;
    Charset charset;

    if (wwwAuthenticate.get(charsetparam) != null
            && wwwAuthenticate.get(charsetparam).equalsIgnoreCase("UTF-8")) {
        charset = StandardCharsets.UTF_8;
    } else {
        charset = StandardCharsets.ISO_8859_1;
    }

    String base64 = Base64.encodeBase64String(userPass.getBytes(charset));

    return " Basic " + base64;
}
 
Example 11
Source File: TestRestCsrfPreventionFilter2.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private BasicCredentials(String aMethod, String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes = userCredentials.getBytes(StandardCharsets.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials = method + " " + base64auth;
}
 
Example 12
Source File: TestBasicAuthParser.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private BasicAuthHeader(String method, String username,
        String password, String extraBlob) {
    prefix(method);

    String userCredentials =
            ((password == null) || (password.length() < 1))
            ? username
            : username + ":" + password;
    byte[] credentialsBytes =
            userCredentials.getBytes(StandardCharsets.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    byte[] base64Bytes =
            base64auth.getBytes(StandardCharsets.ISO_8859_1);

    byte[] extraBytes =
            ((extraBlob == null) || (extraBlob.length() < 1))
            ? null :
            extraBlob.getBytes(StandardCharsets.ISO_8859_1);

    try {
        authHeader.append(base64Bytes, 0, base64Bytes.length);
        if (extraBytes != null) {
            authHeader.append(extraBytes, 0, extraBytes.length);
        }
    }
    catch (IOException ioe) {
        throw new IllegalStateException("unable to extend ByteChunk:"
                + ioe.getMessage());
    }
    // emulate tomcat server - offset points to method in header
    authHeader.setOffset(initialOffset);
}
 
Example 13
Source File: TestNonLoginAndBasicAuthenticator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private BasicCredentials(String aMethod,
        String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes =
            userCredentials.getBytes(StandardCharsets.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials= method + " " + base64auth;
}
 
Example 14
Source File: TestSSOnonLoginAndBasicAuthenticator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private BasicCredentials(String aMethod,
        String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes =
            userCredentials.getBytes(StandardCharsets.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials= method + " " + base64auth;
}
 
Example 15
Source File: TestRestCsrfPreventionFilter2.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private BasicCredentials(String aMethod, String aUsername, String aPassword) {
    method = aMethod;
    username = aUsername;
    password = aPassword;
    String userCredentials = username + ":" + password;
    byte[] credentialsBytes = userCredentials.getBytes(B2CConverter.ISO_8859_1);
    String base64auth = Base64.encodeBase64String(credentialsBytes);
    credentials = method + " " + base64auth;
}
 
Example 16
Source File: WsWebSocketContainer.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private static String generateWsKeyValue() {
    byte[] keyBytes = new byte[16];
    random.nextBytes(keyBytes);
    return Base64.encodeBase64String(keyBytes);
}
 
Example 17
Source File: UpgradeUtil.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private static String getWebSocketAccept(String key) {
    byte[] digest = ConcurrentMessageDigest.digestSHA1(
            key.getBytes(StandardCharsets.ISO_8859_1), WS_ACCEPT);
    return Base64.encodeBase64String(digest);
}
 
Example 18
Source File: UpgradeUtil.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private static String getWebSocketAccept(String key) {
    byte[] digest = ConcurrentMessageDigest.digestSHA1(
            key.getBytes(StandardCharsets.ISO_8859_1), WS_ACCEPT);
    return Base64.encodeBase64String(digest);
}
 
Example 19
Source File: UpgradeUtil.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private static String getWebSocketAccept(String key) {
    byte[] digest = ConcurrentMessageDigest.digestSHA1(
            key.getBytes(StandardCharsets.ISO_8859_1), WS_ACCEPT);
    return Base64.encodeBase64String(digest);
}
 
Example 20
Source File: WsWebSocketContainer.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private static String generateWsKeyValue() {
    byte[] keyBytes = new byte[16];
    RANDOM.nextBytes(keyBytes);
    return Base64.encodeBase64String(keyBytes);
}