Search in sources :

Example 31 with KeyParameter

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);
    }
}
Also used : KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 32 with KeyParameter

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);
}
Also used : KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 33 with KeyParameter

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);
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) Blake3Parameters(com.github.zhenwei.core.crypto.params.Blake3Parameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 34 with KeyParameter

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();
}
Also used : SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) PBEParametersGenerator(com.github.zhenwei.core.crypto.PBEParametersGenerator)

Example 35 with KeyParameter

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) CryptoException(com.github.zhenwei.core.crypto.CryptoException)

Aggregations

KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)91 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)41 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)15 AEADParameters (com.github.zhenwei.core.crypto.params.AEADParameters)10 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)6 ParametersWithSBox (com.github.zhenwei.core.crypto.params.ParametersWithSBox)6 RC2Parameters (com.github.zhenwei.core.crypto.params.RC2Parameters)6 BigInteger (java.math.BigInteger)6 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)4 StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)4 PKCS5S2ParametersGenerator (com.github.zhenwei.core.crypto.generators.PKCS5S2ParametersGenerator)4 HMac (com.github.zhenwei.core.crypto.macs.HMac)4 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)4 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)4 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 IvParameterSpec (javax.crypto.spec.IvParameterSpec)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 RC5Parameters (com.github.zhenwei.core.crypto.params.RC5Parameters)3 CMSException (com.github.zhenwei.pkix.cms.CMSException)3