包含rc4java实现的词条

博主:adminadmin 2023-01-08 09:03:10 719

今天给各位分享rc4java实现的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求一个实现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;i256;i++){

   S[i]= i;

   T[i]=K[i%K.length];

  }

  //用T使S置换

  {

    int j=0;

    for(int i=0;i256;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(c6){

   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;i256;i++){

   S[i]= i;

   T[i]=K[i%K.length];

  }

  //用T使S置换

  {

    int j=0;

    for(int i=0;i256;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(cP.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%"

   

    titleMy 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"

 

 h1RC4加密算法测试系统/h1

 hr

/div

footer.jsp

%@ page language="java" import="java.util.*" pageEncoding="gb2312"%

div align="center"

    hr

    table

       trtdPowered by Tasu/td/tr

       trtdCopyright@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

 }

}(中网互赢 手机客户端)

求大神用java实现RC4的加密,解密功能,高分悬赏.

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import javax.xml.bind.DatatypeConverter;

public class Test {

public static void main(String[] args) throws Exception{

Cipher cipher=Cipher.getInstance("RC4");

String pwd="123456";

String ptext="Hello World 你好";

SecretKeySpec key=new SecretKeySpec(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("解密文: "+new String(ddata,"UTF-8"));

}

}

密码: 123456

明文: Hello World 你好

密文: 489D120B4B1342F30D5B46961D83E12B4875

解密文: Hello World 你好

RC4已经不太安全,只能用于一般加密,不能用于金融等紧要场合。

求编程实现同步序列密码(流密码)的加解密系统,Java或C都行。谢谢啦~

序列密码

编辑

流密码即序列密码。

序列密码也称为流密码(Stream Cipher),它是对称密码算法的一种。序列密码具有实现简单、便于硬件实施、加解密处理速度快、没有或只有有限的错误传播等特点,因此在实际应用中,特别是专用或机密机构中保持着优势,典型的应用领域包括无线通信、外交通信。 1949年Shannon证明了只有一次一密的密码体制是绝对安全的,这给序列密码技术的研究以强大的支持,序列密码方案的发展是模仿一次一密系统的尝试,或者说“一次一密”的密码方案是序列密码的雏形。如果序列密码所使用的是真正随机方式的、与消息流长度相同的密钥流,则此时的序列密码就是一次一密的密码体制。若能以一种方式产生一随机序列(密钥流),这一序列由密钥所确定,则利用这样的序列就可以进行加密,即将密钥、明文表示成连续的符号或二进制,对应地进行加密,加解密时一次处理明文中的一个或几个比特。

序列密码与分组密码的对比

分组密码以一定大小作为每次处理的基本单元,而序列密码则是以一个元素(一个字母或一个比特)作为基本的处理单元。

序列密码是一个随时间变化的加密变换,具有转换速度快、低错误传播的优点,硬件实现电路更简单;其缺点是:低扩散(意味着混乱不够)、插入及修改的不敏感性。

分组密码使用的是一个不随时间变化的固定变换,具有扩散性好、插入敏感等优点;其缺点是:加解密处理速度慢、存在错误传播。

序列密码涉及到大量的理论知识,提出了众多的设计原理,也得到了广泛的分析,但许多研究成果并没有完全公开,这也许是因为序列密码目前主要应用于军事和外交等机密部门的缘故。目前,公开的序列密码算法主要有RC4、SEAL等。

JAVA简单加密解密,写入文件再读取解密就不行了

这个因为加密的时候使用char[]数组,输出到文件的时候用GBK编码,而一些字符GBK无法编码,因此到文件中用"?"替代了。

你可以比较一下encryptedStr与lines,虽然控制台看着是一样,但不是同一个字符串,只不过乱码字符都用"?"表示了

关于rc4java实现和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。