javarc4
Ⅰ 求编程实现同步序列密码(流密码)的加解密系统,java或C都行。谢谢啦~
序列密码
编辑
流密码即序列密码。
序列密码也称为流密码(Stream Cipher),它是对称密码算法的一种。序列密码具有实现简单、便于硬件实施、加解密处理速度快、没有或只有有限的错误传播等特点,因此在实际应用中,特别是专用或机密机构中保持着优势,典型的应用领域包括无线通信、外交通信。 1949年Shannon证明了只有一次一密的密码体制是绝对安全的,这给序列密码技术的研究以强大的支持,序列密码方案的发展是模仿一次一密系统的尝试,或者说“一次一密”的密码方案是序列密码的雏形。如果序列密码所使用的是真正随机方式的、与消息流长度相同的密钥流,则此时的序列密码就是一次一密的密码体制。若能以一种方式产生一随机序列(密钥流),这一序列由密钥所确定,则利用这样的序列就可以进行加密,即将密钥、明文表示成连续的符号或二进制,对应地进行加密,加解密时一次处理明文中的一个或几个比特。
序列密码与分组密码的对比
分组密码以一定大小作为每次处理的基本单元,而序列密码则是以一个元素(一个字母或一个比特)作为基本的处理单元。
序列密码是一个随时间变化的加密变换,具有转换速度快、低错误传播的优点,硬件实现电路更简单;其缺点是:低扩散(意味着混乱不够)、插入及修改的不敏感性。
分组密码使用的是一个不随时间变化的固定变换,具有扩散性好、插入敏感等优点;其缺点是:加解密处理速度慢、存在错误传播。
序列密码涉及到大量的理论知识,提出了众多的设计原理,也得到了广泛的分析,但许多研究成果并没有完全公开,这也许是因为序列密码目前主要应用于军事和外交等机密部门的缘故。目前,公开的序列密码算法主要有RC4、SEAL等。
Ⅱ java poi 要导哪些包
poi-3.0-rc4-20070503.jarpoi-2.0-final-20040126.jarcommons-io-1.4.jarcommons-fileupload-1.2.1.jar
Ⅲ 从nsq队列中取数据时会报java.lang.IllegalStateException: Queue full
在不对的时机操作 比如一入文件被芦搭一个程陪碧拿序读取并锁定 这时你用java向慧蠢其中写入内容 就会出现所谓的 illegalstateexception
Ⅳ java如何读取一个加密后的.xls文件
近日来,研究了一下Excel Biff8(xls 97-2007)与OpenXML(ECMA-376)的加密文档的读取(这还是为了我们世界先进Grid而做的 ^__^)。有些成果,写在这里,希望能给要做类似功能的XD们一些参考。
如有不详,请联系:[email protected] / [email protected]
前提:
1. 加密文档:指Wookbook级的加密,就是在Save Excel文档时在General Settings中设置open password之后的文档;
2. 打开:需要用户传入密码。并非破解。但请勿将本文方法添加暴力模块使用 :-) ;
3. 本文涉及较多为,密钥计算,关于解密细节请参考微软相关文档;
使用的加密算法: RC4, SHA1, MD5, AES-128(其中RC4并不包含在所有版本的.NET Framework中,AES算法可以在.NET Framework 3.5中找到)
本文示例依赖 .NET Framework 3.5
A. Biff8 的加密文档读取
1. 通过文档中FILEPASS的record取得,文档的加密信息(关于Biff文档的格式问题,请参阅Biff的微软文档)
其中Biff8可以使用两种方法加密:Biff8标准加密算法和Biff8扩充加密算法。本文主要讨论最常用的Biff标准加密算法
2. 通过FILEPASS的结构,获得如下信息:
salt(加密随机数,16 bytes)
password verifier (密码效验器,16 bytes)
password verifier hash(密码效验器Hash,16 bytes)
3. 通过以上信息,生成解密key。并通过密码效验器,验证密码:
i. 将密码转化成unicode数组,并进行MD5 Hash;
ii. 将hash结果与salt串联,然后将byte数组,反复串联16次(336 bytes) ,然后再进行MD5 Hash;
iii. 将上步hash结果的前五位,串联上4 bytes的block值(在密码验证阶段为0,在以后解密阶段为block的index) ,然后进行MD5 Hash;
iv. 将上步hash结果的前16位,作为key
v. 使用RC4对称加密算法,将password verifier和password verifier hash分别解密,然后对password verifier的解密结果进行MD5 hash,其值应和password verifier hash的解密结果一致,即为密码正确。
vi. 之后进行逐个record的解密。excel biff8加密原则基本为,record的标示不加密,长度不加密,个别record不加密(见文档);另外,在record解密时,还需要通过block的值重新计算解密key,block的大小为1024.
4. 详细请参照示例代码;
B. OpenXML(ECMA-376) 加密文档的读取
1. 通常来说,xlsx文件相当于一个zip文件,可以用zip程序,直接打开。而在加密后,为了安全性考虑,微软使用了 structured storage(一种OLE文档存储方式)存储(可以用7-zip或者OLE document viewer打开,windows也有相应API来操作此类结构)。在上述文档中,有一个叫做“EncryptedPackage”加密的package,就是一个zip包通过AES算法进行加密之后的结果。我们将使用和A一样的方式来检查密码,但生成key的方法不同;OpenXML的加密类型也有多种,我们这里就讨论常用的用AES-128进行加密的流程;
2. 通过文档的“EncryptedInfo”部分,需要过的一下信息(关于此部分的结构,请参考[MS-OFFCRYPTO].pdf)
salt(加密随机数,16 bytes)
password verifier (密码效验器,16 bytes)
password verifier hash(密码效验器Hash,32 bytes)
3. 通过以上信息,生成解密key。并通过密码效验器,验证密码:
i. 首先,定义一个H函数,其有两个输入,内部使用SHA1算法将两个输入串联之后的结果hash返回;
ii. 先将salt与password(password的unicode数组)进行H计算,h = H(salt, password) ;
iii.然后设iterator为0x00000000,将其转为4byte的数组,然后进行H计算,h1 = H(iterator, h);
iv.将上面的iterator递增一,然后再与h1进行H计算,h2 = H(iterator,h1),然后将这个递增和计算过程重复50000次,最后计算过的iterator为49999即可;
v. 现在有计算结果h50000,将h50000再与0x00000000(4 byte数组)进行H计算,Hfinal = H(h50000, 0x00000000);
vi. 生成一个64byte的数组,将每位都初始化成0x36,然后将这个数组与Hfinal异或;(关于这个地方,微软文档中写的有错误,按照原文的方法生成的key不正确,要不是文档的作者回信告诉我要使用这个法子,就算我想破头也想不出来啊 T__T)
vii.将异或结果,进行SHA1 hash,结果的前16byte就是解密的key;
viii.初始化AES算法,key长度为128,模式为ECB模式,Padding为none; 然后将password verifier 和password verifier hash分别解密;
ix. password verifier 解密后的SHA1 hash结果应该与password verifier hash解密后的前20byte相同;
4. 关于"EncryptedPackage" 的解密则更为简单,只许将“EncryptedPackage”读入,去除前8byte的size信息后,进行AES解密,即为未加密的标准openxml文档。
参考:
[MS-OFFCRYPTO].pdf
[MS-XLS].pdf
ECMA-376 standards
Reply by "winnow", 2008-09-10, 1:17
-----------------------------------------------------
总结一下, 关于这两种基于密码的加密方法, 基本上都是基于RFC2898 建议, 思想是这样:
输入是用户的密码:password, 输出是提供给加密函数的密钥:key.
考虑安全, 需要使同样的password生成的key不一样, 这样用相同的password加密后的结果就无法比较. 需要一个随机数salt.
另外, 为了使暴力破解的代价增大, 考虑使用一个循环多次的过程, 需要循环次数:iteration_count.
概念上, 生成方法为: 将password和salt进行某种运算, 配合一个Hash函数, 以某种方式循环iteration_count次, 在最后的结果里取一部分作为key输出.
具体参照RFC2898中的建议方法PBKDF1和PBKDF2.
这样, 用户输入的密码与一个随机数组合, 经过一定代价的运算, 就生成了可以供加密函数使用的密钥. 使用这个密钥和一个加密函数, 就可以进行加密了.
在应用中, 为了快速判断密码是否错误. 生成一个随机数verifier, 用一个Hash函数计算verifier的hash值:verifier_hash, 分别加密verifier和verifier_hash并保存.
解密的时候, 先分别解密出verifier和verifier_hash, 计算verifier的hash值, 与verifier_hash比较, 如果一致, 即说明密码正确.
Ⅳ 求一个实现RC4加密算法的第三方包,JAVA的
曾经实验室的带动,加上最近在上网络安全与管理的专业选修课,对加密算法有了浓厚的兴趣。老师留了一次作业,用自己的学号为密钥,加密一句话,使用RC4加密算法。
图书馆查找资料,发现RC4算法还是比较容易理解的。于是动手实现出来。不多说废话,还是贴代码吧。我写的注释还算清楚。
先贴一个含main函数的核心算法类,有测试,可以看看最后输出了什么^.^
import java.io.UnsupportedEncodingException;
public class Arithmetic {
public static void swap(int x,int y){
int temp;
temp=x;
x=y;
y=temp;
}
public static void main(String[] args) throws UnsupportedEncodingException {
// 密钥(我的学号)
byte K[]={0,6,1,6,1,0,0,4};
int S[]=new int[256];//状态矢量S
int T[]=new int[256];//临时矢量T
// 初始化状态矢量S,同时生成临时矢量T
for(int i=0;i<256;i++){
S[i]= i;
T[i]=K[i%K.length];
}
//用T使S置换
{
int j=0;
for(int i=0;i<256;i++){
j=(j+(int)S[i]+(int)T[i])%256;
swap(S[i],S[j]);
}
}
int i = 0,j=0;
boolean tt=true;
int c=0;
int t;
byte k;//密钥流的当前字节
byte C[]="套范续".getBytes();
System.out.println(C[3]);
byte P[]=new byte[C.length];
while(c<6){
i=(i+1)%256;
j=(j+S[i])%256;
swap(S[i],S[j]);
t=((S[i]+S[j])%256);
k=(byte) S[t];
// C[c]=(byte) (k^P[c]);
// System.out.print(C[c]+" ");
P[c]=(byte) (k^C[c]);
System.out.print(P[c]+" ");
c++;
}
System.out.println(new String(P,"GBK"));
// byte rr[]={65};
// System.out.println(new String(rr));
}
}
再来贴一下以界面展示的代码,比较长。用的就是普通的jsp+servlet。
核心类:
public class RC4 {
// 密钥(我的学号)
byte K[]={0,6,1,6,1,0,0,4};
void swap(int x,int y){
int temp;
temp=x;
x=y;
y=temp;
}
public String encrypt(String plaintext){
String ciphertext=new String();
int S[]=new int[256];//状态矢量S
int T[]=new int[256];//临时矢量T
// 初始化状态矢量S,同时生成临时矢量T
for(int i=0;i<256;i++){
S[i]= i;
T[i]=K[i%K.length];
}
//用T使S置换
{
int j=0;
for(int i=0;i<256;i++){
j=(j+(int)S[i]+(int)T[i])%256;
swap(S[i],S[j]);
}
}
int i = 0,j=0;
int c=0;
int t;
byte k;//密钥流的当前字节
byte P[]=plaintext.getBytes();
byte C[]=new byte[P.length];
while(c<P.length){
i=(i+1)%256;
j=(j+S[i])%256;
swap(S[i],S[j]);
t=((S[i]+S[j])%256);
k=(byte) S[t];
C[c]=(byte) (k^P[c]);
System.out.print(C[c]+" ");
c++;
}
System.out.println(new String(C));
ciphertext=new String(C);
return ciphertext;
}
}
页面:
home.jsp
<%@ page language="java" import="java.util.*,core.ChangeCharset" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'home.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript">
function doencrypt(){
document.form1.domethod.value="encrypt";
document.form1.submit();
}
function dodecipher(){
document.form1.domethod.value="decipher";
document.form1.submit();
}
</script>
</head>
<body>
<%@ include file="header.jsp" %>
<div id="show" >
<form name="form1" method="post" action="Manage">
<input type="hidden" name="domethod" />
请输入一句话:<input type="text" name="plainText" />
<a href="javascript:void(0)" onclick="javascript:doencrypt();return false;">加密</a>
</br>
</br>
<%String cipher=(String)request.getAttribute("cipher");
if(cipher==null){
cipher="";
}
String plain1=(String)request.getAttribute("plain1");
if(plain1==null){
plain1="";
}
%>
所得密文:<input type="text" name="cipherText" value="<%=cipher %>" />
<a href="javascript:void(0)" onclick="javascript:dodecipher();return false;">解密</a>
</br>
</br>
所得明文:<input type="text" name="getPlain" value="<%=plain1 %>" />
</form>
</div>
<%@ include file="footer.jsp" %>
</body>
</html>
header.jsp(这个没什么意思,但还是给出来吧,给初学html的朋友一些借鉴)
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<div id="header" align="center">
<h1>RC4加密算法测试系统</h1>
<hr>
</div>
footer.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<div align="center">
<hr>
<table>
<tr><td>Powered by Tasu</td></tr>
<tr><td>Copyright@Tasusparty Studio 2009-2010 All rights reserved</td></tr>
</table>
</div>
styles.css(给出来省事一些,诸位看起来方便)
body{
margin:0px;
padding:0px;
background: #E6EAE9;
font-family: "Lucida Sans Unicode", "宋体", "新宋体", Arial, Verdana, serif;
color:#4f6b72;
font-size:12px;
line-height:150%;
}
#show{
margin:0px auto;
padding:0px;
width:200px;
height:400px
}
#header{
margin:30px auto;
}
处理的Servlet:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import core.ChangeCharset;
import core.RC4;
public class Manage extends HttpServlet {
public Manage() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK"); //设置输入编码格式
response.setContentType("text/html;charset=GBK"); //设置输出编码格式
String domethod=request.getParameter("domethod");
if(domethod.equals("encrypt")){
String plain=request.getParameter("plainText");
RC4 rc4=new RC4();
String cipher=rc4.encrypt(plain);
System.out.println(cipher);
request.setAttribute("cipher", cipher);
request.getRequestDispatcher("home.jsp").forward(request, response);
}
if(domethod.equals("decipher")){
String cipher=request.getParameter("cipherText");
RC4 rc4=new RC4();
String plain=rc4.encrypt(cipher);
request.setAttribute("plain1", plain);
request.getRequestDispatcher("home.jsp").forward(request, response);
}
}
public void init() throws ServletException {
// Put your code here
}
}(中网互赢 手机客户端)
Ⅵ 怎么启用 app transport security安全功能
最近看见苹果下发的通知是关于所有iosapp都要使用渗谈禅安全的https链接与服务器进行通信的,并且是2017年1月1日开始执行(虽然后期另行通知是时间有所推迟),那也是早晚的事了。
苹果要求的安全https链接不是在http上加一个s这么简单,那满足ATS我们需要做些什么呢
①必须是苹果信任的CA证书机构颁发的证书,不能是自签证书。
②后台传输必须满足:TLS1.2(这个很重要)
③证书必须使用SHA256或者更好的哈希算法进行签名。
下面上干货,如果你的项目是Java + Tomcat/Jboss 开发的那请看下面,你要找到相应配置证书的地方在xml配置如下,其中ciphers是加密方式、sslEnabledProtocols是使用TLS协议而关闭SSL协议(因为ssl协议出现漏洞所以关闭它)。
[html] view plain
<connector port="443"
maxhttpheadersize="8192"
address="127.0.0.1"
enablelookups="false"
disableuploadtimeout="true"
acceptCount="100"
scheme="https"
secure="true"
clientAuth="false"
SSLEnabled="侍祥true"
sslEnabledProtocols="TLSv1,TLSv1.1,TLSv1.2"
ciphers="TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
TLS_ECDHE_RSA_WITH_RC4_128_SHA,
TLS_RSA_WITH_AES_128_CBC_SHA256,
TLS_RSA_WITH_AES_128_CBC_SHA,
TLS_RSA_WITH_AES_256_CBC_SHA256,
TLS_RSA_WITH_AES_256_CBC_SHA,
SSL_RSA_WITH_RC4_128_SHA"
keystoreFile="mydomain.key"
keystorePass="password"
truststoreFile="mytruststore.truststore"
truststorePass="password"/>
如果你的项目是.NET 开发的项目那你请看下面,.NET修改起来很简单只需安装一个程序,在 windows上 有一个很好用的软件来解决此问题 叫 IISCrypto40.exe ,
下载完后,在windows server 服务器上运行此软件,然后按照上图来勾选每一项,最后 apply后需要重启服务器,重启之后就大功告成,丛尘你的https网站安全级别也被提高。
Ⅶ java.lang.NullPointerException
写一个用户名和留言再读出来?检查一下
File file=new File("D:\\mule-1.3-rc4\\新建文件夹\\1.log");
这句成功了没?捕捉一下IOException 看看
另外
if("submit".equals(action))//这句有点棚孙问题,你想判断喊轿提交表单的话用个form好链渗链了,不需要这么来判断
{
File file=new File("D:\\mule-1.3-rc4\\新建文件夹\\1.log");
String name=request.getParameter("name");
String words=request.getParameter("words");
try{
DataOutputStream dataout=new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(file)
)
);
dataout.writeUTF(name);
dataout.writeUTF(words);
dataout.close();
}catch(IOException e){
}
}
//加个按钮来submit表单
<Form>
<imput type="submit" id="submit" value="提交">
<body><div align="center">
<%
String action=request.getParameter("action");
request.setCharacterEncoding("gb2312");
if(action!=null){ //不为空的话往下做
File file=new File("D:\\mule-1.3-rc4\\新建文件夹\\1.log");
String name=request.getParameter("name");
String words=request.getParameter("words");
try{
DataOutputStream dataout=new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(file)
)
);
dataout.writeUTF(name);
dataout.writeUTF(words);
dataout.close();
}catch(IOException e){
}
}
%>
</form>
Ⅷ JAVA简单加密解密,写入文件再读取解密就不行了
这个因为加密的时候使用char[]数组,输出到文件的时蔽衫候用GBK编码,而一些字符GBK无法编码,因此到文件中用"?"替代了。
你可以比较一下encryptedStr与lines,虽然控制台看着是一样,但宏闷腔不是同一个字符串,只不过乱码字符都用"?"表罩启示了
Ⅸ java des des3 rc4加密后字符串会增大多少
package com.*;
public class RC4 {
public static String decry_RC4(byte[] data, String key) {
if (data == null || key == null) {
return null;
}
return asString(RC4Base(data, key));
}
public static String decry_RC4(String data, String key) {
if (data == null || key == null) {
return null;
}
return new String(RC4Base(HexString2Bytes(data), key));
}
public static byte[] encry_RC4_byte(String data, String key) {
if (data == null || key == null) {
return null;
}
byte b_data[] = data.getBytes();
return RC4Base(b_data, key);
}
public static String encry_RC4_string(String data, String key) {
if (data == null || key == null) {
return null;
}
return toHexString(asString(encry_RC4_byte(data, key)));
}
private static String asString(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length);
for (int i = 0; i < buf.length; i++) {
strbuf.append((char) buf[i]);
}
return strbuf.toString();
}
private static byte[] initKey(String aKey) {
byte[] b_key = aKey.getBytes();
byte state[] = new byte[256];
for (int i = 0; i < 256; i++) {
state[i] = (byte) i;
}
int index1 = 0;
int index2 = 0;
if (b_key == null || b_key.length == 0) {
return null;
}
for (int i = 0; i < 256; i++) {
index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
byte tmp = state[i];
state[i] = state[index2];
state[index2] = tmp;
index1 = (index1 + 1) % b_key.length;
}
return state;
}
private static String toHexString(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch & 0xFF);
if (s4.length() == 1) {
s4 = '0' + s4;
}
str = str + s4;
}
return str;// 0x表示十六进制
}
private static byte[] HexString2Bytes(String src) {
int size = src.length();
byte[] ret = new byte[size / 2];
byte[] tmp = src.getBytes();
for (int i = 0; i < size / 2; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
private static byte uniteBytes(byte src0, byte src1) {
char _b0 = (char) Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
_b0 = (char) (_b0 << 4);
char _b1 = (char) Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
private static byte[] RC4Base(byte[] input, String mKkey) {
int x = 0;
int y = 0;
byte key[] = initKey(mKkey);
int xorIndex;
byte[] result = new byte[input.length];
for (int i = 0; i < input.length; i++) {
x = (x + 1) & 0xff;
y = ((key[x] & 0xff) + y) & 0xff;
byte tmp = key[x];
key[x] = key[y];
key[y] = tmp;
xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
result[i] = (byte) (input[i] ^ key[xorIndex]);
}
return result;
}
public static void main(String[] args) {
String inputStr = "做个好男人";
String str = encry_RC4_string(inputStr, "123456");
System.out.println(str);
System.out.println(decry_RC4(str, "123456"));
}
}
Ⅹ 求大神用java实现RC4的加密,解密功能,高分悬赏.
importjavax.crypto.Cipher;
importjavax.crypto.spec.SecretKeySpec;
importjavax.xml.bind.DatatypeConverter;
publicclassTest{
publicstaticvoidmain(String[]args)throwsException{
Ciphercipher=Cipher.getInstance("RC4");
Stringpwd="123456";
Stringptext="HelloWorld你好";
SecretKeySpeckey=newSecretKeySpec(pwd.getBytes("UTF-8"),"RC4");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[]cdata=cipher.update(ptext.getBytes("UTF-8"));
//解密
cipher.init(Cipher.DECRYPT_MODE,key);
byte[]ddata=cipher.update(cdata);
System.out.println("密码:"+pwd);
System.out.println("明文:"晌隐+ptext);
System.out.println("密文:"+DatatypeConverter.printHexBinary(cdata));
System.out.println("解密文:"+newString(ddata,"UTF-8"));
}
}
密码:123456
明文:HelloWorld你镇戚好
密文:
解密文御谨陵:HelloWorld你好
RC4已经不太安全,只能用于一般加密,不能用于金融等紧要场合。