当前位置: 技术问答>java相关
请教口令加密相关问题
来源: 互联网 发布时间:2015-02-14
本文导语: 我正在写一个jsp,需要对用户输入的口令采用DES算法加密。 请问有这方面经验的大侠,jcs中的javax.crypto,javax.crypto.spec,javax.crypto.interfaces应该怎么用?最好能给出比较具体的例子,多谢多谢! | ...
我正在写一个jsp,需要对用户输入的口令采用DES算法加密。
请问有这方面经验的大侠,jcs中的javax.crypto,javax.crypto.spec,javax.crypto.interfaces应该怎么用?最好能给出比较具体的例子,多谢多谢!
请问有这方面经验的大侠,jcs中的javax.crypto,javax.crypto.spec,javax.crypto.interfaces应该怎么用?最好能给出比较具体的例子,多谢多谢!
|
public static byte[] PBEncryption(String rStr,String rPassword)
{
try
{
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;
// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
// Iteration count
int count = 20;
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);
// Prompt user for encryption password.
// Collect user password as char array (using the
// "readPasswd" method from above), and convert
// it into a SecretKey object, using a PBE key
// factory.
pbeKeySpec = new PBEKeySpec(rPassword.toCharArray());
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// Our cleartext
byte[] cleartext = rStr.getBytes();
// Encrypt the cleartext
byte[] ciphertext = pbeCipher.doFinal(cleartext);
return ciphertext;
}catch(Exception e)
{
e.printStackTrace();
return new byte[0];
}
}
{
try
{
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;
// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
// Iteration count
int count = 20;
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);
// Prompt user for encryption password.
// Collect user password as char array (using the
// "readPasswd" method from above), and convert
// it into a SecretKey object, using a PBE key
// factory.
pbeKeySpec = new PBEKeySpec(rPassword.toCharArray());
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// Our cleartext
byte[] cleartext = rStr.getBytes();
// Encrypt the cleartext
byte[] ciphertext = pbeCipher.doFinal(cleartext);
return ciphertext;
}catch(Exception e)
{
e.printStackTrace();
return new byte[0];
}
}
|
rStr是用户口令,password 是你的加密密码。
rByte是加密后的用户口令,password是你的加密密码。
rByte是加密后的用户口令,password是你的加密密码。