📄 pbewithmd5anddes.java
字号:
package au.net.aba.crypto.provider;
/*
* $Id: PBEWithMD5AndDES.java,v 1.3 1999/01/22 06:00:47 leachbj Exp $
* $Author: leachbj $
*
* Copyright (C) 1996-1998 Australian Business Access Pty Ltd.
* All rights reserved.
*
* Use, modification, copying and distribution of this software is subject the
* terms and conditions of the ABA Public Licence. See the file
* "PUBLIC_LICENCE" for additional information.
*
* If you have not received a copy of the Public Licence, you must destroy all
* copies of this file immediately.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/crypto/provider/PBEWithMD5AndDES.java,v $
* $Revision: 1.3 $
* $Date: 1999/01/22 06:00:47 $
* $State: Exp $
*/
import java.security.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
* This Cipher implements password based encryption (PBE) as specified
* in PKCS#5. This Cipher uses MD5 to convert the password into a
* DES Key and an initialisation vector. DES is then used in
* CBC mode (with PKCS#5 padding) to encrypt or decrypt the data.
*/
public class PBEWithMD5AndDES extends PBE
{
public final static String ident = "$Id: PBEWithMD5AndDES.java,v 1.3 1999/01/22 06:00:47 leachbj Exp $";
public PBEWithMD5AndDES()
{
super("DES/CBC/PKCS5Padding");
}
/**
* Initialises the cipher for encrypt or decryption using the
* provided algorithm parameters.
*
* @param opmode Encrypt/Decrypt
* @param salt The salt to add to the password
* @param iteration The iteration count for key generation
* @param password The actual password to use in the key generation
*/
protected void initCipher(
int opmode,
byte[] salt,
int iteration,
char[] password)
{
// Convert password into bytes
byte[] bytes = new byte[password.length];
for (int i = 0; i < password.length; i++)
{
bytes[i] = (byte)password[i];
}
try
{
MessageDigest digest =
MessageDigest.getInstance("MD5", "ABA");
for (int i = 0; i < iteration; i++)
{
digest.update(bytes, 0, bytes.length);
digest.update(salt, 0, salt.length);
}
byte[] digestBytes = digest.digest();
DESKeySpec keySpec = new DESKeySpec(digestBytes);
SecretKeyFactory skf =
SecretKeyFactory.getInstance("DES");
SecretKey secKey = skf.generateSecret(keySpec);
IvParameterSpec pSpec = new IvParameterSpec(digestBytes,
digestBytes.length - 8, 8);
/*
* initalise cipher with out new key and an
* initialisation vector of the last 8 bytes
* of the digest
*/
cipher.init(opmode, secKey, pSpec);
}
catch(Exception e)
{
throw new ExceptionInInitializerError(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -