use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class ECIESKeyEncapsulation method deriveKey.
protected KeyParameter deriveKey(int keyLen, byte[] C, byte[] PEH) {
byte[] kdfInput = PEH;
if (!SingleHashMode) {
kdfInput = Arrays.concatenate(C, PEH);
Arrays.fill(PEH, (byte) 0);
}
try {
// Initialise the KDF
kdf.init(new KDFParameters(kdfInput, null));
// Generate the secret key
byte[] K = new byte[keyLen];
kdf.generateBytes(K, 0, K.length);
// Return the ciphertext
return new KeyParameter(K);
} finally {
Arrays.fill(kdfInput, (byte) 0);
}
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class RSAKeyEncapsulation method generateKey.
protected KeyParameter generateKey(BigInteger n, BigInteger r, int keyLen) {
byte[] R = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, r);
// Initialise the KDF
kdf.init(new KDFParameters(R, null));
// Generate the secret key
byte[] K = new byte[keyLen];
kdf.generateBytes(K, 0, K.length);
return new KeyParameter(K);
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class Blake3Mac method init.
public void init(final CipherParameters pParams) {
CipherParameters myParams = pParams;
if (myParams instanceof KeyParameter) {
myParams = Blake3Parameters.key(((KeyParameter) myParams).getKey());
}
if (!(myParams instanceof Blake3Parameters)) {
throw new IllegalArgumentException("Invalid parameter passed to Blake3Mac init - " + pParams.getClass().getName());
}
final Blake3Parameters myBlakeParams = (Blake3Parameters) myParams;
if (myBlakeParams.getKey() == null) {
throw new IllegalArgumentException("Blake3Mac requires a key parameter.");
}
/* Configure the digest */
theDigest.init(myBlakeParams);
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class SCrypt method SingleIterationPBKDF2.
private static byte[] SingleIterationPBKDF2(byte[] P, byte[] S, int dkLen) {
PBEParametersGenerator pGen = new PKCS5S2ParametersGenerator(new SHA256Digest());
pGen.init(P, S, 1);
KeyParameter key = (KeyParameter) pGen.generateDerivedMacParameters(dkLen * 8);
return key.getKey();
}
use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.
the class DESExample method performDecrypt.
/*
* This method performs all the decryption and writes
* the plain text to the buffered output stream created
* previously.
*/
private void performDecrypt(byte[] key) {
// initialise the cipher for decryption
cipher.init(false, new KeyParameter(key));
/*
* As the decryption is from our preformatted file,
* and we know that it's a hex encoded format, then
* we wrap the InputStream with a BufferedReader
* so that we can read it easily.
*/
BufferedReader br = new BufferedReader(new InputStreamReader(in));
/*
* now, read the file, and output the chunks
*/
try {
int outL;
byte[] inblock = null;
byte[] outblock = null;
String rv = null;
while ((rv = br.readLine()) != null) {
inblock = Hex.decode(rv);
outblock = new byte[cipher.getOutputSize(inblock.length)];
outL = cipher.processBytes(inblock, 0, inblock.length, outblock, 0);
/*
* Before we write anything out, we need to make sure
* that we've got something to write out.
*/
if (outL > 0) {
out.write(outblock, 0, outL);
}
}
try {
/*
* Now, process the bytes that are still buffered
* within the cipher.
*/
outL = cipher.doFinal(outblock, 0);
if (outL > 0) {
out.write(outblock, 0, outL);
}
} catch (CryptoException ce) {
}
} catch (IOException ioeread) {
ioeread.printStackTrace();
}
}
Aggregations