www.google.com
search md5
search md5
解决方案 »
- 期待大牛帮助解决Java录音问题
- java调用存储过程问题
- java thread notify恢复挂起线程的问题
- 如何将应用程序转换成安装文件
- jtable的问题
- javax.mail.AuthenticationFailedException:EOF on socket
- java.lang.ArrayIndexOutOfBoundsException: 0,帮我调试看下,谢谢
- 学习开发JAVA程序都需要哪些工具啊?请教大侠!----在线等
- 在ModalDialog窗口中的URL地址,怎么让它在父窗口中打开?请多多指教!!!
- 请教 如何设置JOptionPane弹出对话框的字体
- 请问怎么用JAVA的IO操作实现文件的删除?
- 求高手修改一段JAVA代码,解决窗体关不掉的问题
public String generatemd5string(String csinput)
{
byte[] b, b2 ;
StringBuffer buf ;
String csreturn = null ; try
{
b = csinput.getBytes("iso-8859-1") ;
MessageDigest md = MessageDigest.getInstance("MD5") ;
md.update(b) ;
b2 = md.digest() ; buf = new StringBuffer(b2.length * 2) ;
for (int nLoopindex = 0 ; nLoopindex < b2.length ; nLoopindex++)
{
if ( ( (int) b2[nLoopindex] & 0xff) < 0x10)
{
buf.append("0") ;
}
buf.append(Long.toString( (int) b2[nLoopindex] & 0xff, 16)) ;
}
csreturn = new String(buf) ;
}
catch (Exception e)
{
e.printStackTrace() ;
csreturn = null ;
} return csreturn ;
}
* 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));
}
}
* <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();
}
}
}