使用Java实现RC4算法
使用RC加密算法,S盒
调用接口:RC4Encrypt.getkeyToPWD(inputStr));
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*作者:渴望飞的鱼
* 加密算法:RC4加密
* 调用: String str = RC4Encrypt.getkeyToPWD(inputStr); inputStr是密码,即可返回str加密以后的密文
* 返回的str则是已经加密以后的密码
*
*/
public class RC4Encrypt {
public RC4Encrypt(){
//构造方法,加载密钥
}
public String HloveyRC4(String aInput,String aKey){
//机密S盒的核心算法
int[] iS = new int[256];
byte[] iK = new byte[256];
for (int i=0;i<256;i++)
iS[i]=i; //256个数字
int j = 1; //整数
for (short i= 0;i<256;i++){
//
iK[i]=(byte)aKey.charAt((i % aKey.length()));
}
j=0;
for (int i=0;i<255;i++){
j=(j+iS[i]+iK[i]) % 256;
int temp = iS[i];
iS[i]=iS[j];
iS[j]=temp;
}
int i=0;
j=0;
char[] iInputChar = aInput.toCharArray();
char[] iOutputChar = new char[iInputChar.length];
for(short x = 0;x
使用Java实现RSA算法
调用接口:RC4Encrypt.getkeyToPWD(inputStr));
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*作者:渴望飞的鱼
* 加密算法:RC4加密
* 调用: String str = RC4Encrypt.getkeyToPWD(inputStr); inputStr是密码,即可返回str加密以后的密文
* 返回的str则是已经加密以后的密码
*
*/
public class RC4Encrypt {
public RC4Encrypt(){
//构造方法,加载密钥
}
public String HloveyRC4(String aInput,String aKey){
//机密S盒的核心算法
int[] iS = new int[256];
byte[] iK = new byte[256];
for (int i=0;i<256;i++)
iS[i]=i; //256个数字
int j = 1; //整数
for (short i= 0;i<256;i++){
//
iK[i]=(byte)aKey.charAt((i % aKey.length()));
}
j=0;
for (int i=0;i<255;i++){
j=(j+iS[i]+iK[i]) % 256;
int temp = iS[i];
iS[i]=iS[j];
iS[j]=temp;
}
int i=0;
j=0;
char[] iInputChar = aInput.toCharArray();
char[] iOutputChar = new char[iInputChar.length];
for(short x = 0;x
使用Java实现RSA算法
使用RSA加密算法,对密文、文件加密
package com.hh.rsa.filter;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.WebUtils;
/**
*
* @author HH
*
*/
public class MyFilter extends OncePerRequestFilter {
/** Default method parameter: {@code _method} */
public static final String DEFAULT_METHOD_PARAM = "_method";
private String methodParam = DEFAULT_METHOD_PARAM;
/**
* Set the parameter name to look for HTTP methods.
* @see #DEFAULT_METHOD_PARAM
*/
public void setMethodParam(String methodParam) {
Assert.hasText(methodParam, "'methodParam' must not be empty");
this.methodParam = methodParam;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
HttpServletRequest requestToUse = request;
requestToUse.setCharacterEncoding("UTF-8");
if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
requestToUse = new HttpMethodRequestWrapper(request, paramValue);
}
}
filterChain.doFilter(requestToUse, response);
}
/**
* Simple {@link HttpServletRequest} wrapper that returns the supplied method for
* {@link HttpServletRequest#getMethod()}.
*/
private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
private final String method;
public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
super(request);
this.method = method.toUpperCase(Locale.ENGLISH);
}
@Override
public String getMethod() {
return this.method;
}
}
}
package com.hh.rsa.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Map;
import com.hh.rsa.util.RSAUtil;
/**
*
* @author 黄卉
*
*/
public class RSATest {
static String publicKey; //声明公钥
static String privateKey; //声明私钥
//通过文件读取路径,对文件文字进行加密
public static void ReadAndAddRSAToFile(String filePath) throws Exception{
//File file=new File("D:\\学习笔记区\\文件测试1.txt");
File file=new File(filePath);
System.out.println("判断文件是否存在,file.exists:"+file.exists());
if(!file.exists()){
System.out.println("文件不已经存在!");
}
else{
System.out.println();
System.out.println("文件已经存在,开始读取加密,请稍后......");
RandomAccessFile raf=new RandomAccessFile(file,"rw");
byte[] byt=new byte[(int)raf.length()];
raf.read(byt);
String ss=new String(byt); //获取文件内容
System.out.println(ss); //打印文件内容
//对文件进行加密
byte[] toPublicKeyText=testRSAByPublicKeyJiaMi(ss);
System.out.println("文件使用公钥加密后的文字: "+new String(toPublicKeyText));
//写到新的文档
File file2=new File(file.getAbsolutePath()+"加密后版本.txt");
RandomAccessFile raf2=new RandomAccessFile(file2,"rw");
raf2.write(new String(toPublicKeyText).getBytes("gbk"));
System.out.println(raf.getFilePointer());
raf2.close();
raf.close();//关闭
//(2)通过私钥进行文字解密
String theString=new String(testRSAByPrivateKeyJieMi(toPublicKeyText));
System.out.println("文件使用私钥解密后的文字"+theString);
}
}
//通过文件读取路径,对文件解密
public static void jieMiFile(String filePath) throws Exception{
//File file=new File("D:\\学习笔记区\\文件测试1.txt");
File file=new File(filePath);
System.out.println("判断文件是否存在,file.exists:"+file.exists());
if(!file.exists()){
System.out.println("文件不已经存在!");
}
else{
System.out.println();
System.out.println("文件已经存在,开始读取,请稍后......");
RandomAccessFile raf=new RandomAccessFile(file,"rw");
byte[] byt=new byte[(int)raf.length()];
raf.read(byt);
String ss=new String(byt); //获取文件内容
System.out.println(ss); //打印文件内容
//对文件进行解密
String theString=new String(testRSAByPrivateKeyJieMi(byt));
System.out.println("文件使用私钥解密后的文字"+theString);
//写到新的文档
File file2=new File(file.getAbsolutePath()+"解密后版本.txt");
RandomAccessFile raf2=new RandomAccessFile(file2,"rw");
raf2.write(new String(theString.trim()).getBytes("gbk"));
System.out.println(raf.getFilePointer());
raf2.close();
raf.close();//关闭
}
}
//调用方法
public static void main(String[] args) throws Exception {
getKey(); //获得公钥和私钥的密钥
// test(source); //对输入的文字进行使用公钥加密,使用私钥解密
//(1)通过公钥进行文字加密
String source = "这是测试一:在这里输入测试文字!输入测试加密文字";
byte[] toPublicKeyText=testRSAByPublicKeyJiaMi(source);
System.out.println("main:加密后的文字: "+new String(toPublicKeyText));
//(2)通过私钥进行文字解密
String theString=new String(testRSAByPrivateKeyJieMi(toPublicKeyText));
System.out.println("main:解密后的文字"+theString);
//(3)进行数字签名 :私钥加密,私钥签名 ——公钥验证签名 ,公钥解密
// testSign(); //数字签名功能
// //(1)通过私钥加密
String source3 = "数字签名测试,先对文字进行私钥加密。并在加密后使用数字签名";
byte[] toPublicKeyText3=testRSAByPrivatyJiaMi(source3);
// //(2)通过私钥签名
String sign=testSignPrivateKeyBySign(toPublicKeyText3);//通过私钥签名后
System.out.println("签名后的sign:"+sign);
// //(3)通过公钥验证 签名是否正确
boolean flag= isRightOrFalseSign(toPublicKeyText3,sign);
if(flag){//验证成功,通过公钥验证签名
// (4)通过私钥解密
String source4= testSignPublicKeyBySignJieMI(toPublicKeyText3);
System.out.println("通过公钥解密后:"+source4);
}
System.out.println("----文件读取区域------");
// ReadAndAddRSAToFile("D:\\学习笔记区\\文件测试3.txt");
}
/**
* @author 黄卉
* @time 2017年6月24日
* @param null
* @return null
* 获取公钥和私钥
*/
public static void getK