?? desmac.java
字號:
package au.net.aba.crypto.provider;
/*
* $Id: DESMac.java,v 1.1 1999/02/11 04:32:51 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/DESMac.java,v $
* $Revision: 1.1 $
* $Date: 1999/02/11 04:32:51 $
* $State: Exp $
*/
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;
/**
* This class implements the DES based MAC algorithm as defined in
* <a href="http://www.itl.nist.gov/div897/pubs/fip113.htm">FIPS PUB 113</a>.
* <p>
* This algorithm produces a 4 byte MAC value.
* <p>
* Currently this class will not work correctly unless the input data
* size is an exact multiple of 8. Data that isnt of this length
* should be padded with zeros. Eventually the DES cipher class
* will be modified to perform this function.
*/
public class DESMac extends MacSpi
{
private static final int MAC_LENGTH = 4;
private static final int MAC_BUFFER = 8;
// private static final String MAC_ALG = "DES/CBC/Zeroes";
private static final String MAC_ALG = "DES/CBC/NoPadding";
private static final String MAC_PROV = "ABA";
private Cipher desCipher;
private byte[] inbuf;
private byte[] mac;
/**
* Default constructor, initialises the instance.
*/
public DESMac()
{
inbuf = new byte[1];
mac = new byte[MAC_BUFFER];
}
/**
* Completes the MAC computation and resets the MAC for further
* use, maintaining the secret key that the MAC was initialised with.
*
* @return the MAC result.
*/
protected byte[] engineDoFinal()
{
try
{
byte[] out = desCipher.doFinal();
updateMac(out);
}
catch (BadPaddingException bpe)
{
bpe.printStackTrace();
}
catch (IllegalBlockSizeException ibse)
{
ibse.printStackTrace();
}
byte[] result = new byte[MAC_LENGTH];
System.arraycopy(mac, 0, result, 0, MAC_LENGTH);
return result;
}
/**
* Returns the length of the MAC in bytes.
*
* @return the MAC length in bytes.
*/
protected int engineGetMacLength()
{
return MAC_LENGTH;
}
/**
* Initialises the MAC with the given (secret) key and algorithm
* parameters. The <code>key</code> parameter should be a
* type that is compatible with the DES <code>Cipher</code>.
*
* @param key the (secret) key.
* @param params the algorithm parameters.
*
* @exception InvalidKeyException if the given key is inappropriate for
* initialising this MAC.
* @exception InvalidAlgorithmParameterException if the given algorithm
* parameters are inappropriate for this MAC.
*/
protected void engineInit(Key key, AlgorithmParameterSpec params)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
try
{
desCipher = Cipher.getInstance(MAC_ALG, MAC_PROV);
}
catch (Exception e)
{
throw new ExceptionInInitializerError(e);
}
byte[] iv = new byte[desCipher.getBlockSize()];
IvParameterSpec ivParam = new IvParameterSpec(iv);
desCipher.init(Cipher.ENCRYPT_MODE, key, ivParam);
mac = new byte[MAC_BUFFER];
}
/**
* Resets the MAC for further use, maintaining the secret key that
* the MAC was initialised with.
*/
protected void engineReset()
{
mac = new byte[MAC_BUFFER];
if (desCipher != null)
{
try
{
desCipher.doFinal();
}
catch (Exception e)
{
// ignore it
}
}
}
/**
* Processes the first len bytes in input, starting at offset.
*
* @param input the input buffer.
* @param offset the offset in input where the input starts.
* @param len the number of bytes to process.
*/
protected void engineUpdate(byte[] input, int offset, int len)
{
byte[] out = desCipher.update(input, offset, len);
updateMac(out);
}
/**
* Processes the given byte.
*
* @param input the input byte to be processed.
*/
protected void engineUpdate(byte input)
{
inbuf[0] = input;
byte[] out = desCipher.update(inbuf);
updateMac(out);
}
/**
* This method will add the newbuf bytes on the the mac. The
* MAC result is stored in a circular type buffer, as new data
* arrives it is appended to the buffer and old data is shifted
* out so that the last 4 bytes of the cipher are saved.
*/
private void updateMac(byte[] newbuf)
{
if (newbuf == null || newbuf.length == 0)
{
return;
}
if (newbuf.length == MAC_BUFFER)
{
System.arraycopy(newbuf, 0, mac, 0, MAC_BUFFER);
}
else if (newbuf.length > MAC_BUFFER)
{
System.arraycopy(newbuf, newbuf.length - MAC_BUFFER,
mac, 0, MAC_BUFFER);
}
else
{
System.arraycopy(mac, newbuf.length, mac, 0,
MAC_BUFFER - newbuf.length);
System.arraycopy(newbuf, 0, mac,
MAC_BUFFER - newbuf.length, newbuf.length);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -