use of com.github.zhenwei.core.crypto.EphemeralKeyPair in project LinLong-Java by zhenwei1108.
the class EthereumIESEngine method processBlock.
public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {
if (forEncryption) {
if (keyPairGenerator != null) {
EphemeralKeyPair ephKeyPair = keyPairGenerator.generate();
this.privParam = ephKeyPair.getKeyPair().getPrivate();
this.V = ephKeyPair.getEncodedPublicKey();
}
} else {
if (keyParser != null) {
ByteArrayInputStream bIn = new ByteArrayInputStream(in, inOff, inLen);
try {
this.pubParam = keyParser.readKey(bIn);
} catch (IOException e) {
throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
}
int encLength = (inLen - bIn.available());
this.V = Arrays.copyOfRange(in, inOff, inOff + encLength);
}
}
// Compute the common value and convert to byte array.
agree.init(privParam);
BigInteger z = agree.calculateAgreement(pubParam);
byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);
// Create input to KDF.
if (V.length != 0) {
byte[] VZ = Arrays.concatenate(V, Z);
Arrays.fill(Z, (byte) 0);
Z = VZ;
}
try {
// Initialise the KDF.
KDFParameters kdfParam = new KDFParameters(Z, param.getDerivationV());
kdf.init(kdfParam);
return forEncryption ? encryptBlock(in, inOff, inLen) : decryptBlock(in, inOff, inLen);
} finally {
Arrays.fill(Z, (byte) 0);
}
}
use of com.github.zhenwei.core.crypto.EphemeralKeyPair in project LinLong-Java by zhenwei1108.
the class IESEngine method processBlock.
public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {
if (forEncryption) {
if (keyPairGenerator != null) {
EphemeralKeyPair ephKeyPair = keyPairGenerator.generate();
this.privParam = ephKeyPair.getKeyPair().getPrivate();
this.V = ephKeyPair.getEncodedPublicKey();
}
} else {
if (keyParser != null) {
ByteArrayInputStream bIn = new ByteArrayInputStream(in, inOff, inLen);
try {
this.pubParam = keyParser.readKey(bIn);
} catch (IOException e) {
throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
}
int encLength = (inLen - bIn.available());
this.V = Arrays.copyOfRange(in, inOff, inOff + encLength);
}
}
// Compute the common value and convert to byte array.
agree.init(privParam);
BigInteger z = agree.calculateAgreement(pubParam);
byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);
// Create input to KDF.
if (V.length != 0) {
byte[] VZ = Arrays.concatenate(V, Z);
Arrays.fill(Z, (byte) 0);
Z = VZ;
}
try {
// Initialise the KDF.
KDFParameters kdfParam = new KDFParameters(Z, param.getDerivationV());
kdf.init(kdfParam);
return forEncryption ? encryptBlock(in, inOff, inLen) : decryptBlock(in, inOff, inLen);
} finally {
Arrays.fill(Z, (byte) 0);
}
}
Aggregations