use of com.github.zhenwei.provider.jcajce.provider.util.BadBlockException in project LinLong-Java by zhenwei1108.
the class IESCipher method engineDoFinal.
// Finalisation methods
public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
if (inputLen != 0) {
buffer.write(input, inputOffset, inputLen);
}
byte[] in = buffer.toByteArray();
buffer.reset();
// Convert parameters for use in IESEngine
CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(), engineSpec.getEncodingV(), engineSpec.getMacKeySize(), engineSpec.getCipherKeySize());
if (engineSpec.getNonce() != null) {
params = new ParametersWithIV(params, engineSpec.getNonce());
}
DHParameters dhParams = ((DHKeyParameters) key).getParameters();
byte[] V;
if (otherKeyParameter != null) {
try {
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
engine.init(true, otherKeyParameter, key, params);
} else {
engine.init(false, key, otherKeyParameter, params);
}
return engine.processBlock(in, 0, in.length);
} catch (Exception e) {
throw new BadBlockException("unable to process block", e);
}
}
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
// Generate the ephemeral key pair
DHKeyPairGenerator gen = new DHKeyPairGenerator();
gen.init(new DHKeyGenerationParameters(random, dhParams));
EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {
public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
byte[] Vloc = new byte[(((DHKeyParameters) keyParameter).getParameters().getP().bitLength() + 7) / 8];
byte[] Vtmp = BigIntegers.asUnsignedByteArray(((DHPublicKeyParameters) keyParameter).getY());
if (Vtmp.length > Vloc.length) {
throw new IllegalArgumentException("Senders's public key longer than expected.");
} else {
System.arraycopy(Vtmp, 0, Vloc, Vloc.length - Vtmp.length, Vtmp.length);
}
return Vloc;
}
});
// Encrypt the buffer
try {
engine.init(key, params, kGen);
return engine.processBlock(in, 0, in.length);
} catch (Exception e) {
throw new BadBlockException("unable to process block", e);
}
} else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) {
// Decrypt the buffer
try {
engine.init(key, params, new DHIESPublicKeyParser(((DHKeyParameters) key).getParameters()));
return engine.processBlock(in, 0, in.length);
} catch (InvalidCipherTextException e) {
throw new BadBlockException("unable to process block", e);
}
} else {
throw new IllegalStateException("IESCipher not initialised");
}
}
use of com.github.zhenwei.provider.jcajce.provider.util.BadBlockException in project LinLong-Java by zhenwei1108.
the class IESCipher method engineDoFinal.
// Finalisation methods
public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
if (inputLen != 0) {
buffer.write(input, inputOffset, inputLen);
}
final byte[] in = buffer.toByteArray();
buffer.reset();
// Convert parameters for use in IESEngine
CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(), engineSpec.getEncodingV(), engineSpec.getMacKeySize(), engineSpec.getCipherKeySize());
if (engineSpec.getNonce() != null) {
params = new ParametersWithIV(params, engineSpec.getNonce());
}
final ECDomainParameters ecParams = ((ECKeyParameters) key).getParameters();
final byte[] V;
if (otherKeyParameter != null) {
try {
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
engine.init(true, otherKeyParameter, key, params);
} else {
engine.init(false, key, otherKeyParameter, params);
}
return engine.processBlock(in, 0, in.length);
} catch (Exception e) {
throw new BadBlockException("unable to process block", e);
}
}
if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
// Generate the ephemeral key pair
ECKeyPairGenerator gen = new ECKeyPairGenerator();
gen.init(new ECKeyGenerationParameters(ecParams, random));
final boolean usePointCompression = engineSpec.getPointCompression();
EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {
public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
return ((ECPublicKeyParameters) keyParameter).getQ().getEncoded(usePointCompression);
}
});
// Encrypt the buffer
try {
engine.init(key, params, kGen);
return engine.processBlock(in, 0, in.length);
} catch (final Exception e) {
throw new BadBlockException("unable to process block", e);
}
} else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) {
// Decrypt the buffer
try {
engine.init(key, params, new ECIESPublicKeyParser(ecParams));
return engine.processBlock(in, 0, in.length);
} catch (InvalidCipherTextException e) {
throw new BadBlockException("unable to process block", e);
}
} else {
throw new IllegalStateException("cipher not initialised");
}
}
Aggregations