JBoss提供的常用的对称加密算法
2013-10-29 22:21
330 查看
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); } }
相关文章推荐
- [置顶] (奇迹冬瓜)坦克大战[MFC框架]
- [World Wind学习]22.相机高度和瓦片等级计算
- C语言
- apple风格的html文档生成
- ffmpeg 学习 之几个基本数据结构之间联系
- (递归)汉诺塔
- iOS开发-小知识(二)
- HDU3468:A Simple Problem with Integers(线段树区间更新+lazy)
- IOS7 新特性(针对同样讨厌更新后IOS7的开发者)
- [置顶] bzoj 1036 树的统计Count 点权值模板
- Python Cookbook学习记录 ch3_3/4_2013/10/29
- Sublime Text 配置使用总结(不断更新....)
- SPOJ 8222. Substrings(后缀自动机模板)
- All in All 找子串 水题
- 三国演义 ----感悟
- fcntl的区域锁定
- HDU1698:Just a Hook(线段树区间更新)
- spinner自定义,效果如腾讯QQ账号选择时候的下拉列表
- 写给大家的编程书(2)
- python 高级编程 一