/**
* FileName: Encryptstr.java
* Description: 字符串加密:通过指定相同长度的key字符串,将源字符串和Key字符串的Ascii码进行异或,并进行简单操作,获得加密后的数字字符串
* Copyright: Copyright (c) 2003
* @Author: Totodo
* @Email: [email protected]
* @Version: 1.0
*/
public class Encryptstr {
static String sWrong;
static String sSkey="207806514039";public Encryptstr() {
sWrong="";
}public static String encryptkey(String sKey,int iLength) {
//获取指定长度的key
String sKeystr=sKey;
int kLength;
kLength=sKey.length();
if (iLength<=kLength)
sKeystr=sKey.substring(0,iLength); //截取和加密字符串长度相等的key
else {
//重复key使长度相等
while (kLength<iLength) {
if (kLength+sKey.length()>=iLength) {
sKeystr=sKeystr+sKey.substring(0,iLength-kLength);
}
else {
sKeystr=sKeystr+sKey;
}
kLength=sKeystr.length();
}
}
return sKeystr;
}public String encrypt(String sKey,String sSource) {
//加密字符串
String sTarget="";
String sKeystr;//和加密字符串长度相等的key
int sLength;//加密字符串长度
char cS,cK,cT;
int iS,iK,iT;
int i;
if (sSource=="") {
sWrong="字符串为空!";
return sTarget;
}
if (sKey=="") {
sWrong="请指定密钥!";
return sTarget;
}
sLength=sSource.length();
sKeystr=encryptkey(sKey,sLength);
for (i=0;i<sLength;i++) {
cS=sSource.charAt(i);
cK=sKeystr.charAt(i);
iS=(int)cS;
iK=(int)cK;
iT=iS^iK;
// System.out.print(iS);
// System.out.print(" ");
// System.out.print(iK);
// System.out.print(" ");
// System.out.print(iT);
// System.out.print(" ");
// System.out.println(iT+29);
iT=iT+29;
iT=1000+iT/10%10*100+iT/100*10+iT%10;//十位、百位、个位
sTarget=sTarget+Integer.toString(iT).substring(1);
}
return sTarget;
}public String encrypt(String sSource) {
return encrypt(sSkey,sSource);
}public static String decode(String sKey,String sTarget) {
//解密
String sSource="";
String sKeystr;//和解密字符串长度相等的key
int sLength; //解密字符串长度
char cS,cK;
String cT;
int iS,iK,iT;
int i; if (sTarget=="") {
sWrong="字符串为空!";
return sSource;
}
if (sKey=="") {
sWrong="请指定密钥!";
return sSource;
}
sLength=sTarget.length()/3;
sKeystr=encryptkey(sKey,sLength);
for (i=0;i<sLength;i++) {
cT=sTarget.substring(i*3,(i+1)*3);
cK=sKeystr.charAt(i);
iT=Integer.parseInt(cT);
iT=iT/10%10*100+iT/100*10+iT%10;
iK=(int)cK;
iS=(iT-29)^iK;
// System.out.print(iT);
// System.out.print(" ");
// System.out.print(iT-29);
// System.out.print(" ");
// System.out.print(iK);
// System.out.print(" ");
// System.out.println(iS);
cS=(char)iS;
sSource=sSource+cS;
} return sSource;
}public static String decode(String sTarget) {
return decode(sSkey,sTarget);
}public static void main(String[] args) throws Exception{
String str;
Encryptstr en_str = new Encryptstr();
str=en_str.encrypt("abcde");
System.out.println("abcde加密后的字符传:"+str);
System.out.println("adcde解迷后的字符船:"+decode("207806514039",str));
}
}
* FileName: Encryptstr.java
* Description: 字符串加密:通过指定相同长度的key字符串,将源字符串和Key字符串的Ascii码进行异或,并进行简单操作,获得加密后的数字字符串
* Copyright: Copyright (c) 2003
* @Author: Totodo
* @Email: [email protected]
* @Version: 1.0
*/
public class Encryptstr {
static String sWrong;
static String sSkey="207806514039";public Encryptstr() {
sWrong="";
}public static String encryptkey(String sKey,int iLength) {
//获取指定长度的key
String sKeystr=sKey;
int kLength;
kLength=sKey.length();
if (iLength<=kLength)
sKeystr=sKey.substring(0,iLength); //截取和加密字符串长度相等的key
else {
//重复key使长度相等
while (kLength<iLength) {
if (kLength+sKey.length()>=iLength) {
sKeystr=sKeystr+sKey.substring(0,iLength-kLength);
}
else {
sKeystr=sKeystr+sKey;
}
kLength=sKeystr.length();
}
}
return sKeystr;
}public String encrypt(String sKey,String sSource) {
//加密字符串
String sTarget="";
String sKeystr;//和加密字符串长度相等的key
int sLength;//加密字符串长度
char cS,cK,cT;
int iS,iK,iT;
int i;
if (sSource=="") {
sWrong="字符串为空!";
return sTarget;
}
if (sKey=="") {
sWrong="请指定密钥!";
return sTarget;
}
sLength=sSource.length();
sKeystr=encryptkey(sKey,sLength);
for (i=0;i<sLength;i++) {
cS=sSource.charAt(i);
cK=sKeystr.charAt(i);
iS=(int)cS;
iK=(int)cK;
iT=iS^iK;
// System.out.print(iS);
// System.out.print(" ");
// System.out.print(iK);
// System.out.print(" ");
// System.out.print(iT);
// System.out.print(" ");
// System.out.println(iT+29);
iT=iT+29;
iT=1000+iT/10%10*100+iT/100*10+iT%10;//十位、百位、个位
sTarget=sTarget+Integer.toString(iT).substring(1);
}
return sTarget;
}public String encrypt(String sSource) {
return encrypt(sSkey,sSource);
}public static String decode(String sKey,String sTarget) {
//解密
String sSource="";
String sKeystr;//和解密字符串长度相等的key
int sLength; //解密字符串长度
char cS,cK;
String cT;
int iS,iK,iT;
int i; if (sTarget=="") {
sWrong="字符串为空!";
return sSource;
}
if (sKey=="") {
sWrong="请指定密钥!";
return sSource;
}
sLength=sTarget.length()/3;
sKeystr=encryptkey(sKey,sLength);
for (i=0;i<sLength;i++) {
cT=sTarget.substring(i*3,(i+1)*3);
cK=sKeystr.charAt(i);
iT=Integer.parseInt(cT);
iT=iT/10%10*100+iT/100*10+iT%10;
iK=(int)cK;
iS=(iT-29)^iK;
// System.out.print(iT);
// System.out.print(" ");
// System.out.print(iT-29);
// System.out.print(" ");
// System.out.print(iK);
// System.out.print(" ");
// System.out.println(iS);
cS=(char)iS;
sSource=sSource+cS;
} return sSource;
}public static String decode(String sTarget) {
return decode(sSkey,sTarget);
}public static void main(String[] args) throws Exception{
String str;
Encryptstr en_str = new Encryptstr();
str=en_str.encrypt("abcde");
System.out.println("abcde加密后的字符传:"+str);
System.out.println("adcde解迷后的字符船:"+decode("207806514039",str));
}
}
解决方案 »
- struts2 登陆成功后url里显示.action而不是.jsp
- 请教一个关于初始化的当前时间和某个固定时间比较的问题。
- ibatis的N+1问题
- 用InitialContext建立数据源,运行出现javax.naming.NoInitialContextException异常
- 大家快来帮帮我啊~~~ WEB应用域登陆的问题
- 求J2EE高手!
- tomcat 连接池问题
- 有那位高手能说一说应用服务器集群的原理和实现的方法?
- 大数据处理,有办法没?
- JAVA解析JSON串
- struts中用tiles实现网页动态显示不同模板页面的讨论~~
- 最后70分,关于eclipse import log4j包的问题?????????????
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.io.*;import javax.crypto.*;
import java.security.*;public class DESCryptoTest {
public static void main(String[] args) {
// Security.addProvider(new com.sun.crypto.provider.SunJCE());
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyGenerator kg = null;
try {
//指定算法,这里为DES;如果想用Blowfish算法,则用 getInstance("Blowfish")
//BouncyCastle基本上支持所有通用标准算法
kg = KeyGenerator.getInstance("DES", "BC"); //指定密钥长度,长度越高,加密强度越大
kg.init(56); //产生密钥
Key key = kg.generateKey();
System.out.println("Key format: " + key.getFormat());
System.out.println("Key algorithm: " + key.getAlgorithm()); //加密要用Cipher来实现
Cipher cipher = Cipher.getInstance("DES");
System.out.println("Cipher provider: " + cipher.getProvider());
System.out.println("Cipher algorithm: " + cipher.getAlgorithm()); byte[] data = "Hello World!".getBytes();
System.out.println("Original data : [" + data.length + "]" +
new String(data)); //设置加密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(data);
System.out.println("Encrypted data: [" + result.length + "]" +
new String(result)); //设置解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] original = cipher.doFinal(result);
System.out.println("Decrypted data: [" + original.length + "]" +
new String(original)); String filename = "加密前.txt";
//读入并加密文件
try {
//输入流
cipher.init(Cipher.ENCRYPT_MODE, key);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
//输出流
CipherOutputStream out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream("加密后.txt")), cipher);
int i;
do {
i = in.read();
if (i != -1)
out.write(i);
}
while (i != -1); in.close();
out.close();
System.out.println("加密文件完成!");
}
catch (Exception ey5) {
System.out.println("Error when encrypt the file");
System.exit(0);
} try {
cipher.init(Cipher.DECRYPT_MODE, key);
//输出流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("解密后.txt"));
//输入流
CipherInputStream in = new CipherInputStream(new BufferedInputStream(
new FileInputStream("加密后.txt")), cipher); int i;
do {
i = in.read();
if (i != -1)
out.write(i);
}
while (i != -1); in.close();
out.close();
System.out.println("解密文件完成!");
}
catch (Exception ey5) {
System.out.println("Error when encrypt the file");
System.exit(0);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}