求助用java写的加密算法 请各位高手给个用java写的用户密码的加密算法的例子! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://community.csdn.net/Expert/topic/3433/3433100.xml?temp=.8216364看看吧 天乙社区用的md5加密算法,不错的import java.lang.reflect.*;/** * <p>Title: 天乙社区V5.0</p> * <p>Description: BBS-CS天乙社区V5.0</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: laoer.com</p> * @author 龚天乙 * @version 5.0 *//************************************************* md5 类实现了RSA Data Security, Inc.在提交给IETF 的RFC1321中的MD5 message-digest 算法。 *************************************************/public class MD5 { /* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个 Instance间共享*/ static final int S11 = 7; static final int S12 = 12; static final int S13 = 17; static final int S14 = 22; static final int S21 = 5; static final int S22 = 9; static final int S23 = 14; static final int S24 = 20; static final int S31 = 4; static final int S32 = 11; static final int S33 = 16; static final int S34 = 23; static final int S41 = 6; static final int S42 = 10; static final int S43 = 15; static final int S44 = 21; static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 被定义到MD5_CTX结构中 */ private long[] state = new long[4]; // state (ABCD) private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb first) private byte[] buffer = new byte[64]; // input buffer /* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示. */ public String digestHexStr; /* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值. */ private byte[] digest = new byte[16]; /* getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的. */ public String getMD5ofStr(String inbuf) { md5Init(); md5Update(inbuf.getBytes(), inbuf.length()); md5Final(); digestHexStr = ""; for (int i = 0; i < 16; i++) { digestHexStr += byteHEX(digest[i]); } return digestHexStr; } // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数 public MD5() { md5Init(); return; } /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */ private void md5Init() { count[0] = 0L; count[1] = 0L; ///* Load magic initialization constants. state[0] = 0x67452301L; state[1] = 0xefcdab89L; state[2] = 0x98badcfeL; state[3] = 0x10325476L; return; } /* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。 */ private long F(long x, long y, long z) { return (x & y) | ( (~x) & z); } private long G(long x, long y, long z) { return (x & z) | (y & (~z)); } private long H(long x, long y, long z) { return x ^ y ^ z; } private long I(long x, long y, long z) { return y ^ (x | (~z)); } 其实我个人认为。加密算法有很多中类。他们都有自己的长处,但是对于一般的java爱好者来说,有点难以接受,那些算法恨事高升末测,其实最简单的就是。比如你输入的字符串“asdfgh"我先在将他的每个字符的ASCII码加上一个数值,让他从组成另外一个字符串,把他保护起来,别人也不知道他的愿码是什么, 标准论坛使用的都是md5散列后存入数据库的。像vbb这样的论坛竟然使用了2次md5。一般使用就md5可以了。简单加密看你的用途能不能接受啦 /*** CryptionData.java* @version 1.0* @author 2005/4/20 Yao (WICT)*/import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.*;import sun.misc.*;/*** DES encryption algorithm, providing the encryption and decryption* algorithm for byte array and string* @author : Yao (WICT)* @version 1.0*/public class CryptionData{// The length of Encryptionstring should be 8 bytes and not be// a weak keyprivate String EncryptionString;// The initialization vector should be 8 bytesprivate final byte[] EncryptionIV = "abcdefgh".getBytes();private final static String DES = "DES/CBC/PKCS5Padding";/*** Saving key for encryption and decryption* @param EncryptionString String*/public CryptionData(String EncryptionString) {this.EncryptionString = EncryptionString;}/*** Encrypt a byte array* @param SourceData byte[]* @throws Exception* @return byte[]*/public byte[] EncryptionByteData(byte[] SourceData) throws Exception {byte[] retByte = null;// Create SecretKey objectbyte[] EncryptionByte = EncryptionString.getBytes();DESKeySpec dks = new DESKeySpec(EncryptionByte);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey securekey = keyFactory.generateSecret(dks);// Create IvParameterSpec object with initialization vectorIvParameterSpec spec=new IvParameterSpec(EncryptionIV);// Create Cipter objectCipher cipher = Cipher.getInstance(DES);// Initialize Cipher objectcipher.init(Cipher.ENCRYPT_MODE, securekey, spec);// Encrypting dataretByte = cipher.doFinal(SourceData);return retByte;}/*** Decrypt a byte array* @param SourceData byte[]* @throws Exception* @return byte[]*/public byte[] DecryptionByteData(byte[] SourceData) throws Exception {byte[] retByte = null;// Create SecretKey objectbyte[] EncryptionByte = EncryptionString.getBytes();DESKeySpec dks = new DESKeySpec(EncryptionByte);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey securekey = keyFactory.generateSecret(dks);// Create IvParameterSpec object with initialization vectorIvParameterSpec spec=new IvParameterSpec(EncryptionIV);// Create Cipter objectCipher cipher = Cipher.getInstance(DES);// Initialize Cipher objectcipher.init(Cipher.DECRYPT_MODE, securekey, spec);// Decrypting dataretByte = cipher.doFinal(SourceData);return retByte;} /*** Encrypt a string* @param SourceData String* @throws Exception* @return String*/public String EncryptionStringData(String SourceData) throws Exception{String retStr = null;byte[] retByte = null;// Transform SourceData to byte arraybyte[] sorData = SourceData.getBytes();// Encrypte dataretByte = EncryptionByteData(sorData);// Encode encryption data BASE64Encoder be = new BASE64Encoder();retStr = be.encode(retByte);return retStr;}/*** Decrypt a string* @param SourceData String* @throws Exception* @return String*/public String DecryptionStringData(String SourceData) throws Exception {String retStr = null;byte[] retByte = null;// Decode encryption dataBASE64Decoder bd = new BASE64Decoder();byte[] sorData = bd.decodeBuffer(SourceData);// Decrypting dataretByte = DecryptionByteData(sorData);retStr = new String(retByte);return retStr;}} 请查看JCE或Bouncycastle类包说明文档www.bouncycastle.orgor java home 请教 thread 异常 向 Map<String, String>中增加加元素 请前辈进来指点下,谢谢~~ DB事务必须具备的四个特性 哪位知道如何实现邮件到达通知呀? 怎么利用java的帮助文档学习java? 你们的工资多少呀? 将要学习java请各位进来帮帮忙!!谢谢 isLetter()的含义?? Object和Integer 急!! 我改了一个第三方jar 包里的一个程序, 重新打的jar 包程序不能正常引用,不知是什么问题 如何在窗口中倒入图像啊?
看看吧
* <p>Title: 天乙社区V5.0</p>
* <p>Description: BBS-CS天乙社区V5.0</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: laoer.com</p>
* @author 龚天乙
* @version 5.0
*//*************************************************
md5 类实现了RSA Data Security, Inc.在提交给IETF
的RFC1321中的MD5 message-digest 算法。
*************************************************/public class MD5 {
/* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的,
这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个
Instance间共享*/
static final int S11 = 7;
static final int S12 = 12;
static final int S13 = 17;
static final int S14 = 22; static final int S21 = 5;
static final int S22 = 9;
static final int S23 = 14;
static final int S24 = 20; static final int S31 = 4;
static final int S32 = 11;
static final int S33 = 16;
static final int S34 = 23; static final int S41 = 6;
static final int S42 = 10;
static final int S43 = 15;
static final int S44 = 21; static final byte[] PADDING = {
-128, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中
被定义到MD5_CTX结构中
*/
private long[] state = new long[4]; // state (ABCD)
private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb first)
private byte[] buffer = new byte[64]; // input buffer /* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的
16进制ASCII表示.
*/
public String digestHexStr; /* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.
*/
private byte[] digest = new byte[16]; /*
getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串
返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.
*/
public String getMD5ofStr(String inbuf) {
md5Init();
md5Update(inbuf.getBytes(), inbuf.length());
md5Final();
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr; } // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
public MD5() {
md5Init(); return;
} /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */
private void md5Init() {
count[0] = 0L;
count[1] = 0L;
///* Load magic initialization constants. state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L; return;
} /* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是
简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们
实现成了private方法,名字保持了原来C中的。 */ private long F(long x, long y, long z) {
return (x & y) | ( (~x) & z); } private long G(long x, long y, long z) {
return (x & z) | (y & (~z)); } private long H(long x, long y, long z) {
return x ^ y ^ z;
} private long I(long x, long y, long z) {
return y ^ (x | (~z));
}
比如你输入的字符串“asdfgh"
我先在将他的每个字符的ASCII码加上一个数值,让他从组成另外一个字符串,把他保护起来,别人也不知道他的愿码是什么,
* CryptionData.java
* @version 1.0
* @author 2005/4/20 Yao (WICT)
*/import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.*;
import sun.misc.*;/**
* DES encryption algorithm, providing the encryption and decryption
* algorithm for byte array and string
* @author : Yao (WICT)
* @version 1.0
*/
public class CryptionData
{
// The length of Encryptionstring should be 8 bytes and not be
// a weak key
private String EncryptionString;// The initialization vector should be 8 bytes
private final byte[] EncryptionIV = "abcdefgh".getBytes();
private final static String DES = "DES/CBC/PKCS5Padding";/**
* Saving key for encryption and decryption
* @param EncryptionString String
*/
public CryptionData(String EncryptionString) {
this.EncryptionString = EncryptionString;
}/**
* Encrypt a byte array
* @param SourceData byte[]
* @throws Exception
* @return byte[]
*/
public byte[] EncryptionByteData(byte[] SourceData) throws Exception {
byte[] retByte = null;// Create SecretKey objectbyte[] EncryptionByte = EncryptionString.getBytes();
DESKeySpec dks = new DESKeySpec(EncryptionByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);// Create IvParameterSpec object with initialization vector
IvParameterSpec spec=new IvParameterSpec(EncryptionIV);// Create Cipter object
Cipher cipher = Cipher.getInstance(DES);// Initialize Cipher object
cipher.init(Cipher.ENCRYPT_MODE, securekey, spec);// Encrypting data
retByte = cipher.doFinal(SourceData);
return retByte;
}/**
* Decrypt a byte array
* @param SourceData byte[]
* @throws Exception
* @return byte[]
*/
public byte[] DecryptionByteData(byte[] SourceData) throws Exception {
byte[] retByte = null;// Create SecretKey object
byte[] EncryptionByte = EncryptionString.getBytes();
DESKeySpec dks = new DESKeySpec(EncryptionByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);// Create IvParameterSpec object with initialization vector
IvParameterSpec spec=new IvParameterSpec(EncryptionIV);// Create Cipter object
Cipher cipher = Cipher.getInstance(DES);// Initialize Cipher object
cipher.init(Cipher.DECRYPT_MODE, securekey, spec);// Decrypting data
retByte = cipher.doFinal(SourceData);return retByte;
}
* Encrypt a string
* @param SourceData String
* @throws Exception
* @return String
*/
public String EncryptionStringData(String SourceData) throws Exception
{
String retStr = null;
byte[] retByte = null;// Transform SourceData to byte array
byte[] sorData = SourceData.getBytes();// Encrypte data
retByte = EncryptionByteData(sorData);// Encode encryption data
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte);return retStr;
}/**
* Decrypt a string
* @param SourceData String
* @throws Exception
* @return String
*/
public String DecryptionStringData(String SourceData) throws Exception {
String retStr = null;
byte[] retByte = null;// Decode encryption data
BASE64Decoder bd = new BASE64Decoder();
byte[] sorData = bd.decodeBuffer(SourceData);// Decrypting data
retByte = DecryptionByteData(sorData);
retStr = new String(retByte);return retStr;
}
}
www.bouncycastle.org
or
java home