📄 jceiescipher.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: JCEIESCipher.java
package org.bouncycastle.jce.provider;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.*;
import javax.crypto.interfaces.DHPrivateKey;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.engines.IESEngine;
import org.bouncycastle.crypto.generators.KDF2BytesGenerator;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.IESParameters;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.IESKey;
import org.bouncycastle.jce.spec.IESParameterSpec;
// Referenced classes of package org.bouncycastle.jce.provider:
// DHUtil, ECUtil, JCEECPublicKey
public class JCEIESCipher
{
public static class ECIES extends JCEIESCipher
{
public ECIES()
{
super(new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest())));
}
}
private IESEngine cipher;
private int state;
private ByteArrayOutputStream buffer;
private AlgorithmParameters engineParam;
private IESParameterSpec engineParams;
private Class availableSpecs[] = {
org/bouncycastle/jce/spec/IESParameterSpec
};
public JCEIESCipher(IESEngine engine)
{
state = -1;
buffer = new ByteArrayOutputStream();
engineParam = null;
engineParams = null;
cipher = engine;
}
protected int engineGetBlockSize()
{
return 0;
}
protected byte[] engineGetIV()
{
return null;
}
protected int engineGetKeySize(Key key)
{
IESKey ieKey = (IESKey)key;
if (ieKey.getPrivate() instanceof DHPrivateKey)
{
DHPrivateKey k = (DHPrivateKey)ieKey.getPrivate();
return k.getX().bitLength();
}
if (ieKey.getPrivate() instanceof ECPrivateKey)
{
ECPrivateKey k = (ECPrivateKey)ieKey.getPrivate();
return k.getD().bitLength();
} else
{
throw new IllegalArgumentException("not an IE key!");
}
}
protected int engineGetOutputSize(int inputLen)
{
if (state == 1 || state == 3)
return buffer.size() + inputLen + 20;
if (state == 2 || state == 4)
return (buffer.size() + inputLen) - 20;
else
throw new IllegalStateException("cipher not initialised");
}
protected AlgorithmParameters engineGetParameters()
{
if (engineParam == null && engineParams != null)
{
String name = "IES";
try
{
engineParam = AlgorithmParameters.getInstance(name, "BC");
engineParam.init(engineParams);
}
catch (Exception e)
{
throw new RuntimeException(e.toString());
}
}
return engineParam;
}
protected void engineSetMode(String mode)
{
throw new IllegalArgumentException((new StringBuilder()).append("can't support mode ").append(mode).toString());
}
protected void engineSetPadding(String padding)
throws NoSuchPaddingException
{
throw new NoSuchPaddingException((new StringBuilder()).append(padding).append(" unavailable with RSA.").toString());
}
public void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
if (!(key instanceof IESKey))
throw new InvalidKeyException("must be passed IE key");
if (params == null && (opmode == 1 || opmode == 3))
{
byte d[] = new byte[16];
byte e[] = new byte[16];
if (random == null)
random = new SecureRandom();
random.nextBytes(d);
random.nextBytes(e);
params = new IESParameterSpec(d, e, 128);
} else
if (!(params instanceof IESParameterSpec))
throw new InvalidAlgorithmParameterException("must be passed IES parameters");
IESKey ieKey = (IESKey)key;
org.bouncycastle.crypto.CipherParameters pubKey;
org.bouncycastle.crypto.CipherParameters privKey;
if (ieKey.getPublic() instanceof JCEECPublicKey)
{
pubKey = ECUtil.generatePublicKeyParameter(ieKey.getPublic());
privKey = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
} else
{
pubKey = DHUtil.generatePublicKeyParameter(ieKey.getPublic());
privKey = DHUtil.generatePrivateKeyParameter(ieKey.getPrivate());
}
engineParams = (IESParameterSpec)params;
IESParameters p = new IESParameters(engineParams.getDerivationV(), engineParams.getEncodingV(), engineParams.getMacKeySize());
state = opmode;
buffer.reset();
switch (opmode)
{
case 1: // '\001'
case 3: // '\003'
cipher.init(true, privKey, pubKey, p);
break;
case 2: // '\002'
case 4: // '\004'
cipher.init(false, privKey, pubKey, p);
break;
default:
System.out.println("eeek!");
break;
}
}
protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
AlgorithmParameterSpec paramSpec = null;
if (params != null)
{
int i = 0;
do
{
if (i == availableSpecs.length)
break;
try
{
paramSpec = params.getParameterSpec(availableSpecs[i]);
break;
}
catch (Exception e)
{
i++;
}
} while (true);
if (paramSpec == null)
throw new InvalidAlgorithmParameterException((new StringBuilder()).append("can't handle parameter ").append(params.toString()).toString());
}
engineParam = params;
engineInit(opmode, key, paramSpec, random);
}
protected void engineInit(int opmode, Key key, SecureRandom random)
throws InvalidKeyException
{
if (opmode == 1 || opmode == 3)
try
{
engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
return;
}
catch (InvalidAlgorithmParameterException e) { }
throw new IllegalArgumentException("can't handle null parameter spec in IES");
}
public byte[] engineUpdate(byte input[], int inputOffset, int inputLen)
{
buffer.write(input, inputOffset, inputLen);
return null;
}
public int engineUpdate(byte input[], int inputOffset, int inputLen, byte output[], int outputOffset)
{
buffer.write(input, inputOffset, inputLen);
return 0;
}
public byte[] engineDoFinal(byte input[], int inputOffset, int inputLen)
throws IllegalBlockSizeException, BadPaddingException
{
if (inputLen != 0)
buffer.write(input, inputOffset, inputLen);
byte buf[];
buf = buffer.toByteArray();
buffer.reset();
return cipher.processBlock(buf, 0, buf.length);
InvalidCipherTextException e;
e;
throw new BadPaddingException(e.getMessage());
}
public int engineDoFinal(byte input[], int inputOffset, int inputLen, byte output[], int outputOffset)
throws IllegalBlockSizeException, BadPaddingException
{
if (inputLen != 0)
buffer.write(input, inputOffset, inputLen);
byte buf[];
buf = buffer.toByteArray();
buffer.reset();
buf = cipher.processBlock(buf, 0, buf.length);
System.arraycopy(buf, 0, output, outputOffset, buf.length);
return buf.length;
InvalidCipherTextException e;
e;
throw new BadPaddingException(e.getMessage());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -