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 ...
随机推荐
- 动态定义数组 .xml
pre{ line-height:1; color:#3c3c3c; background-color:#d2c39b; font-size:16px;}.sysFunc{color:#627cf6; ...
- CSS中animate属性
我记得,在工作中直接使用animation,只要能做出动画就完了,根本没有看每一个细节. 其实,这样做对于我们来说,的确没有错,因为工作中没有时间给你看每一个细节,大致看一篇就没下文了. 当我们想要好 ...
- jquery选择器的使用
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- linux nginx安装(转载)
1.linux 下面安装 1.下载 pcre-8.10.tar.gz nginx-1.1.1.tar.gz 2.安装 pcre 让nginx支持rewrite pcre-8.10.tar.gz 上 ...
- kali2 source
deb http://http.kali.org/kali sana main non-free contrib deb http://security.kali.org/kali-security/ ...
- lego blocks
1.题目描述 https://www.hackerrank.com/challenges/lego-blocks 2.解法分析 这题乍看一下觉得应该可以用动态规划来做,但是却死活想不到最优子结构,在网 ...
- leetcode—jump game
1.题目描述 Given an array of non-negative integers, you are initially positioned at the first index of t ...
- Determining IP information for eth0... failed; no link present. Check cable?
在RedHat下重启网络,service network restart之后,出现问题 Determining IP information for eth0... failed; no link p ...
- Module compiled with Swift 3.0 cannot be imported in Swift 3.0.1
Cartfile:github "SwiftyJSON/SwiftyJSON"got error:Module compiled with Swift 3.0 cannot be ...
- 《Java数据结构与算法》笔记-CH5-链表-3双端链表
/** * 双端链表的实现 */ class LinkA { public long dData; public LinkA next; public LinkA(long d) { dData = ...