?? brokenjceblockcipher.java
字號:
package org.bouncycastle.jce.provider;import java.security.AlgorithmParameters;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.Key;import java.security.KeyFactory;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.security.SecureRandom;import java.security.spec.AlgorithmParameterSpec;import java.security.spec.InvalidKeySpecException;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.PBEParameterSpec;import javax.crypto.spec.RC2ParameterSpec;import javax.crypto.spec.RC5ParameterSpec;import javax.crypto.spec.SecretKeySpec;import org.bouncycastle.crypto.BlockCipher;import org.bouncycastle.crypto.BufferedBlockCipher;import org.bouncycastle.crypto.CipherParameters;import org.bouncycastle.crypto.DataLengthException;import org.bouncycastle.crypto.InvalidCipherTextException;import org.bouncycastle.crypto.engines.DESEngine;import org.bouncycastle.crypto.engines.DESedeEngine;import org.bouncycastle.crypto.engines.TwofishEngine;import org.bouncycastle.crypto.modes.CBCBlockCipher;import org.bouncycastle.crypto.modes.CFBBlockCipher;import org.bouncycastle.crypto.modes.CTSBlockCipher;import org.bouncycastle.crypto.modes.OFBBlockCipher;import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;import org.bouncycastle.crypto.params.KeyParameter;import org.bouncycastle.crypto.params.ParametersWithIV;import org.bouncycastle.crypto.params.RC2Parameters;import org.bouncycastle.crypto.params.RC5Parameters;public class BrokenJCEBlockCipher implements BrokenPBE{ // // specs we can handle. // private Class[] availableSpecs = { IvParameterSpec.class, PBEParameterSpec.class, RC2ParameterSpec.class, RC5ParameterSpec.class }; private BufferedBlockCipher cipher; private ParametersWithIV ivParam; private int pbeType = PKCS12; private int pbeHash = SHA1; private int pbeKeySize; private int pbeIvSize; private int ivLength = 0; private AlgorithmParameters engineParams = null; protected BrokenJCEBlockCipher( BlockCipher engine) { cipher = new PaddedBufferedBlockCipher(engine); } protected BrokenJCEBlockCipher( BlockCipher engine, int pbeType, int pbeHash, int pbeKeySize, int pbeIvSize) { cipher = new PaddedBufferedBlockCipher(engine); this.pbeType = pbeType; this.pbeHash = pbeHash; this.pbeKeySize = pbeKeySize; this.pbeIvSize = pbeIvSize; } protected int engineGetBlockSize() { return cipher.getBlockSize(); } protected byte[] engineGetIV() { return (ivParam != null) ? ivParam.getIV() : null; } protected int engineGetKeySize( Key key) { return key.getEncoded().length; } protected int engineGetOutputSize( int inputLen) { return cipher.getOutputSize(inputLen); } protected AlgorithmParameters engineGetParameters() { if (engineParams == null) { if (ivParam != null) { String name = cipher.getUnderlyingCipher().getAlgorithmName(); if (name.indexOf('/') >= 0) { name = name.substring(0, name.indexOf('/')); } try { engineParams = AlgorithmParameters.getInstance(name, "BC"); engineParams.init(ivParam.getIV()); } catch (Exception e) { throw new RuntimeException(e.toString()); } } } return engineParams; } protected void engineSetMode( String mode) { String modeName = mode.toUpperCase(); if (modeName.equals("ECB")) { ivLength = 0; cipher = new PaddedBufferedBlockCipher(cipher.getUnderlyingCipher()); } else if (modeName.equals("CBC")) { ivLength = cipher.getUnderlyingCipher().getBlockSize(); cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(cipher.getUnderlyingCipher())); } else if (modeName.startsWith("OFB")) { ivLength = cipher.getUnderlyingCipher().getBlockSize(); if (modeName.length() != 3) { int wordSize = Integer.parseInt(modeName.substring(3)); cipher = new PaddedBufferedBlockCipher( new OFBBlockCipher(cipher.getUnderlyingCipher(), wordSize)); } else { cipher = new PaddedBufferedBlockCipher( new OFBBlockCipher(cipher.getUnderlyingCipher(), 8 * cipher.getBlockSize())); } } else if (modeName.startsWith("CFB")) { ivLength = cipher.getUnderlyingCipher().getBlockSize(); if (modeName.length() != 3) { int wordSize = Integer.parseInt(modeName.substring(3)); cipher = new PaddedBufferedBlockCipher( new CFBBlockCipher(cipher.getUnderlyingCipher(), wordSize)); } else { cipher = new PaddedBufferedBlockCipher( new CFBBlockCipher(cipher.getUnderlyingCipher(), 8 * cipher.getBlockSize())); } } else { throw new IllegalArgumentException("can't support mode " + mode); } } protected void engineSetPadding( String padding) throws NoSuchPaddingException { String paddingName = padding.toUpperCase(); if (paddingName.equals("NOPADDING")) { cipher = new BufferedBlockCipher(cipher.getUnderlyingCipher()); } else if (paddingName.equals("PKCS5PADDING") || paddingName.equals("PKCS7PADDING") || paddingName.equals("ISO10126PADDING")) { cipher = new PaddedBufferedBlockCipher(cipher.getUnderlyingCipher()); } else if (paddingName.equals("WITHCTS")) { cipher = new CTSBlockCipher(cipher.getUnderlyingCipher()); } else { throw new NoSuchPaddingException("Padding " + padding + " unknown."); } } protected void engineInit( int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { CipherParameters param; // // a note on iv's - if ivLength is zero the IV gets ignored (we don't use it). // if (key instanceof JCEPBEKey) { param = BrokenPBE.Util.makePBEParameters((JCEPBEKey)key, params, pbeType, pbeHash, cipher.getUnderlyingCipher().getAlgorithmName(), pbeKeySize, pbeIvSize); if (pbeIvSize != 0) { ivParam = (ParametersWithIV)param; } } else if (params == null) { param = new KeyParameter(key.getEncoded()); } else if (params instanceof IvParameterSpec) { if (ivLength != 0) { param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec)params).getIV()); ivParam = (ParametersWithIV)param; } else { param = new KeyParameter(key.getEncoded()); } } else if (params instanceof RC2ParameterSpec) { RC2ParameterSpec rc2Param = (RC2ParameterSpec)params; param = new RC2Parameters(key.getEncoded(), ((RC2ParameterSpec)params).getEffectiveKeyBits()); if (rc2Param.getIV() != null && ivLength != 0) { param = new ParametersWithIV(param, rc2Param.getIV()); ivParam = (ParametersWithIV)param; } } else if (params instanceof RC5ParameterSpec) { RC5ParameterSpec rc5Param = (RC5ParameterSpec)params; param = new RC5Parameters(key.getEncoded(), ((RC5ParameterSpec)params).getRounds()); if (rc5Param.getWordSize() != 32) { throw new IllegalArgumentException("can only accept RC5 word size 32 (at the moment...)"); } if ((rc5Param.getIV() != null) && (ivLength != 0)) { param = new ParametersWithIV(param, rc5Param.getIV()); ivParam = (ParametersWithIV)param; } } else { throw new InvalidAlgorithmParameterException("unknown parameter type."); } if ((ivLength != 0) && !(param instanceof ParametersWithIV)) { if (random == null) { random = new SecureRandom(); } if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) { byte[] iv = new byte[ivLength]; random.nextBytes(iv); param = new ParametersWithIV(param, iv); ivParam = (ParametersWithIV)param; } else { throw new InvalidAlgorithmParameterException("no IV set when one expected"); } } switch (opmode)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -