📄 pbewithsha1and128bitrc4.java
字号:
package au.net.aba.crypto.provider;
/*
* $Id: PBEWithSHA1And128BitRC4.java,v 1.2 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/PBEWithSHA1And128BitRC4.java,v $
* $Revision: 1.2 $
* $Date: 1999/01/22 06:00:47 $
* $State: Exp $
*/
import java.security.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import au.net.aba.crypto.spec.RC4KeySpec;
/**
* This Cipher implements password based encryption (PBE) as specified
* in PKCS#12. This Cipher uses SHA to convert the password into a
* RC4 Key. RC4 is then used to encrypt or decrypt the data.
*/
public class PBEWithSHA1And128BitRC4 extends PBE
{
public final static String ident = "$Id: PBEWithSHA1And128BitRC4.java,v 1.2 1999/01/22 06:00:47 leachbj Exp $";
public PBEWithSHA1And128BitRC4()
{
super("RC4");
}
/**
* 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)
{
try
{
/*
* Convert password into bytes - PKCS#12
* High byte, Low Byte
*/
byte[] bytes = new byte[(password.length+1) * 2];
for (int i = 0; i < password.length; i++)
{
int aint = (int)password[i];
bytes[i++] = (byte)(aint >>> 8);
bytes[i] = (byte)aint;
}
// Last two bytes must be zero.
bytes[password.length] = 0;
bytes[password.length+1] = 0;
MessageDigest digest =
MessageDigest.getInstance("SHA", "ABA");
for (int i = 0; i < iteration; i++)
{
digest.update(bytes, 0, bytes.length);
digest.update(salt, 0, salt.length);
}
byte[] digestBytes = digest.digest();
RC4KeySpec keySpec = new RC4KeySpec(digestBytes);
SecretKeyFactory skf =
SecretKeyFactory.getInstance("RC4");
SecretKey secKey = skf.generateSecret(keySpec);
cipher.init(opmode, secKey);
}
catch (Exception e)
{
throw new ExceptionInInitializerError(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -