• JBoss提供的常用的对称加密算法


    package com.test.resteasy;
    
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URL;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.PublicKey;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    import java.util.Date;
    
    import javax.ws.rs.core.MediaType;
    
    import junit.framework.Assert;
    
    import org.jboss.resteasy.jose.jwe.JWEBuilder;
    import org.jboss.resteasy.jose.jwe.JWEInput;
    import org.jboss.resteasy.jose.jws.JWSBuilder;
    import org.jboss.resteasy.jose.jws.JWSInput;
    import org.jboss.resteasy.jose.jws.crypto.RSAProvider;
    import org.jboss.resteasy.spi.ResteasyProviderFactory;
    import org.junit.Test;
    
    public class User {
    	private String name;
    	private Integer age;
    	private Date birth;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public Integer getAge() {
    		return age;
    	}
    
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    
    	public Date getBirth() {
    		return birth;
    	}
    
    	public void setBirth(Date birth) {
    		this.birth = birth;
    	}
    
    	public static void main(String[] args) throws Exception {
    		File file = new File("");
    		URI uri = file.toURI();
    		try {
    			URL url = uri.toURL();
    		} catch (MalformedURLException e) {
    			e.printStackTrace();
    		}
    
    	}
    
    	private static KeyPair keyPair = null;
    	static {
    		try {
    			keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    		} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	@Test
    	public void testRSAWithContentType() throws Exception {
    		
    
    		String encoded = new JWSBuilder()
    				.contentType(MediaType.TEXT_PLAIN_TYPE)
    				.content("Hello World", MediaType.TEXT_PLAIN_TYPE)
    				.rsa256(keyPair.getPrivate());
    
    		//System.out.println(encoded);
    
    		JWSInput input = new JWSInput(encoded,
    				ResteasyProviderFactory.getInstance());
    		//System.out.println(input.getHeader());
    		String msg = (String) input.readContent(String.class);
    		Assert.assertEquals("Hello World", msg);
    		Assert.assertTrue(RSAProvider.verify(input, keyPair.getPublic()));
    
    	}
    
    	@Test
    	public void testRSA() throws Exception {
    		//KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
    
    		String content = "Live long and prosper.";
    
    		{
    			PublicKey publicKey = keyPair.getPublic();
    			RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
    			String encoded = new JWEBuilder()
    					.contentBytes(content.getBytes())
    					.RSA1_5(rsaPublicKey);
    			//System.out.println("encoded: " + encoded);
    			byte[] raw = new JWEInput(encoded).decrypt(
    					(RSAPrivateKey) keyPair.getPrivate()).getRawContent();
    			String from = new String(raw);
    			Assert.assertEquals(content, from);
    		}
    		{
    			String encoded = new JWEBuilder().contentBytes(content.getBytes())
    					.RSA_OAEP((RSAPublicKey) keyPair.getPublic());
    			//System.out.println("encoded: " + encoded);
    			byte[] raw = new JWEInput(encoded).decrypt(
    					(RSAPrivateKey) keyPair.getPrivate()).getRawContent();
    			String from = new String(raw);
    			Assert.assertEquals(content, from);
    		}
    		{
    			String encoded = new JWEBuilder().contentBytes(content.getBytes())
    					.A128CBC_HS256().RSA1_5((RSAPublicKey) keyPair.getPublic());
    			//System.out.println("encoded: " + encoded);
    			byte[] raw = new JWEInput(encoded).decrypt(
    					(RSAPrivateKey) keyPair.getPrivate()).getRawContent();
    			String from = new String(raw);
    			Assert.assertEquals(content, from);
    		}
    		{
    			String encoded = new JWEBuilder().contentBytes(content.getBytes())
    					.A128CBC_HS256()
    					.RSA_OAEP((RSAPublicKey) keyPair.getPublic());
    			//System.out.println("encoded: " + encoded);
    			byte[] raw = new JWEInput(encoded).decrypt(
    					(RSAPrivateKey) keyPair.getPrivate()).getRawContent();
    			String from = new String(raw);
    			Assert.assertEquals(content, from);
    		}
    	}
    
    	@Test
    	public void testDirect() throws Exception {
    		String content = "Live long and prosper.";
    		String encoded = new JWEBuilder().contentBytes(content.getBytes()).dir("geheim");
    		//System.out.println("encoded: " + encoded);
    		byte[] raw = new JWEInput(encoded).decrypt("geheim").getRawContent();
    		String from = new String(raw);
    		Assert.assertEquals(content, from);
    
    	}
    }
    


  • 相关阅读:
    成功熬了四年还没死?一个IT屌丝创业者的深刻反思
    史氏语录
    WEB安全攻防学习内容
    从程序员的角度谈创业三年
    Windows2008 R2修改3389端口教程
    Win2008R2 zip格式mysql 安装与配置
    制作支持UEFI PC的Server2008 R2系统安装U盘
    郎科U208(主控 PS2251-50 HYNIX H27UCG8T2MYR)量产还原
    自用有线IP切换
    自动配置IP地址.bat
  • 原文地址:https://www.cnblogs.com/pangblog/p/3395447.html
Copyright © 2020-2023  润新知