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); }
}
JBoss提供的常用的对称加密算法的更多相关文章
- 常用加密算法的Java实现总结(二) ——对称加密算法DES、3DES和AES
1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法,技术成熟.在对称加密算法中,数据发信方将明文(原始数据)和加密密钥(mi yue)一起经过特殊加密算法处理后,使其变成复杂的加密密文发 ...
- [转] 对称加密算法DES、3DES
转自:http://www.blogjava.net/amigoxie/archive/2014/07/06/415503.html 1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法, ...
- 对称加密算法DES,3重DES,TDEA,Blowfish,RC5,IDEA,AES。
对称加密算法:DES,3重DES,TDEA,Blowfish,RC5,IDEA,AES. 1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法,技术成熟.在对称加密算法中,数据发信方将明文 ...
- 第八章 对称加密算法--AES
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第7章“初等加密算法--对称加密算法” 8.1.AES 特点: 密钥建立时间短.灵敏性好.内存需求低(不管怎样,反正就是好) ...
- 对称加密算法DES、3DES和AES 原理总结(转载)
1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法,技术成熟.在对称加密算法中,数据发信方将明文(原始数据)和加密密钥(mi yue)一起经过特殊加密算法处理后,使其变成复杂的加密密文发 ...
- openssl 对称加密算法enc命令详解
1.对称加密算法概述 openssl的加密算法库提供了丰富的对称加密算法,我们可以通过openssl提供的对称加密算法指令的方式使用,也可以通过调用openssl提供的API的方式使用. openss ...
- 介绍对称加密算法,最常用的莫过于DES数据加密算法
DES DES-Data Encryption Standard,即数据加密算法.是IBM公司于1975年研究成功并公开发表的.DES算法的入口参数有三个:Key.Data.Mode.其中Key为8个 ...
- java-信息安全(二)-对称加密算法DES,3DES,AES,Blowfish,RC2,RC4
概述 信息安全基本概念: DES(Data Encryption Standard,数据加密标准) 3DES(Triple DES,三重数据加密算法(TDEA,Triple Data Encrypti ...
- 对称加密算法 ~ Des
一.对称加密 (Symmetric Key Encryption) 对称加密是最快速.最简单的一种加密方式,加密(encryption)与解密(decryption)用的是同样的密钥(secret ...
随机推荐
- Media Player 把光盘中的内容拷贝出来的方法
http://jingyan.baidu.com/article/cb5d610529f0c1005c2fe0b4.html 这个链接是通过Media Player 把光盘中的内容拷贝出来的方法h ...
- Page 16 Exercises 1.2.3 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)
Below are four faulty programs. Each includes a test case that results in failure. Answer the follow ...
- SVN的搭建和使用总结
Subversion是优秀的版本控制工具,其具体的的优点和详细介绍就不多做介绍,主要说一下SVN的服务端搭建.客户端安装.使用及出现的问题的解决办法. 首先来下载和搭建SVN服务器. 现在Subver ...
- MySql 5.6 查询日志
记录所有查询的方法 打开My.ini 在 [mysqld]下面写 general_log= ON; log_output= TABLE; 然后保存重启mysql 最后在mysql库下 general_ ...
- LeetCode(7) - Reverse Integer
题目的要求就是要反转一个Integer,例如输入123,则输出321,这一题比较tricky的地方就是它有可能越界,就是说1234567899,反过来是9987654321是一个越界的Integer, ...
- erlang常用命令
1 erlang启动时就运行odbc erl -s odbc 2 ping 节点 net_adm:ping('rabbit@COMPUTERNAME'). 3 运行cmd命令 os:cmd(" ...
- sprite常用操作
,锚点 锚点就是所有旋转,移动,缩放的参考点.cocos2-x中默认的锚点是中心点.锚点用比例来表示范围为0-,(,)点代表左下点,(,)代表右上点.设置的函数为setAnchorPoint(ccp( ...
- Python程序的混淆和加密
混淆 为了增加代码阅读的难度, 源代码的混淆非常必要, 一个在线的Python代码混淆网站. 如果你觉得有用, 可以购买离线版本.同时需要注意的是, 这个混淆其实还是被很多人怀疑的, 因为即使混淆了, ...
- 第一百九十七-第二百天 how can I 坚持
又是四天,how 快. 第一天,晚上要坐车回济南,没下班就躁动了.晚上高铁竟然是知道济南西,中间没有停,到济南九点半,去刘松家又吃了一顿.喝了不少酒.挺爽. 第二天,早上五点多就醒了,睡的婚床,哈哈, ...
- 触控发布《Cocos开发者平台白皮书》
Cocos 2014 开发者大会(秋季)组委会今天正式发布了<Cocos开发者平台白皮书>,GameRes游资网得到Cocos官方授权发布该白皮书电子版. 白皮书主要内容包括对行业的趋势解 ...