Example usage for org.bouncycastle.crypto.params KeyParameter KeyParameter

List of usage examples for org.bouncycastle.crypto.params KeyParameter KeyParameter

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.params KeyParameter KeyParameter.

Prototype

public KeyParameter(byte[] key) 

Source Link

Usage

From source file:NoekeonVects.java

License:Creative Commons License

private KeyParameter maybe_schedule_key(byte[] key) {
    if (schedule_key) {
        noekeon.init(true, null_key);//from  w  w w .  ja  va2 s .c om
        byte[] scheduled = new byte[16];
        noekeon.processBlock(key, 0, scheduled, 0);
        return new KeyParameter(scheduled);
    } else
        return new KeyParameter(key);
}

From source file:ECToken3.java

License:Open Source License

public static final String encryptv3(String key, String input) throws java.io.UnsupportedEncodingException,
        java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException,
        java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException,
        javax.crypto.BadPaddingException, java.security.InvalidAlgorithmParameterException {

    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| Encrypt\n");
    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| key:                   %s\n", key);
    //System.out.format("| token:                 %s\n", input);

    //----------------------------------------------------
    // Get SHA-256 of key
    //----------------------------------------------------
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(key.getBytes("ASCII"));
    byte[] keyDigest = md.digest();

    //----------------------------------------------------
    // Get Random IV
    //----------------------------------------------------
    SecureRandom random = new SecureRandom();
    byte[] ivBytes = new byte[12];
    random.nextBytes(ivBytes);//from   w w w .j a va2s  .com

    //----------------------------------------------------
    // Encrypt
    //----------------------------------------------------
    AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    cipher.init(true, new AEADParameters(new KeyParameter(keyDigest), MAC_SIZE_BITS, ivBytes));
    byte[] inputBytes = input.getBytes("ASCII");

    byte[] enc = new byte[cipher.getOutputSize(inputBytes.length)];

    try {
        int res = cipher.processBytes(inputBytes, 0, inputBytes.length, enc, 0);
        cipher.doFinal(enc, res);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    byte[] ivPlusCipherText = new byte[ivBytes.length + enc.length];
    System.arraycopy(ivBytes, 0, ivPlusCipherText, 0, ivBytes.length);
    System.arraycopy(enc, 0, ivPlusCipherText, ivBytes.length, enc.length);

    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| iv:                    %s\n", bytesToHex(ivBytes));
    //System.out.format("| ciphertext:            %s\n", bytesToHex(Arrays.copyOfRange(enc, 0, enc.length - 16)));
    //System.out.format("| tag:                   %s\n", bytesToHex(Arrays.copyOfRange(enc, enc.length - 16, enc.length)));
    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| token:                 %s\n", bytesToHex(ivPlusCipherText));
    //System.out.format("+-------------------------------------------------------------\n");

    String result = null;
    byte[] temp = null;
    Base64 encoder = new Base64(0, temp, true);
    byte[] encodedBytes = encoder.encode(ivPlusCipherText);
    String encodedStr = new String(encodedBytes, "ASCII").trim();
    String encodedStrTrim = encodedStr.trim();
    return encodedStr.trim();
}

From source file:ECToken3.java

License:Open Source License

public static final String decryptv3(String key, String input) throws java.io.UnsupportedEncodingException,
        java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException,
        java.security.InvalidKeyException, javax.crypto.IllegalBlockSizeException,
        javax.crypto.BadPaddingException, java.security.InvalidAlgorithmParameterException {

    //----------------------------------------------------
    // Base64 decode
    //----------------------------------------------------
    String result = null;/*w  w w.  j a  v  a 2 s . co m*/
    Base64 encoder = new Base64(true);
    byte[] inputBytes = encoder.decode(input.getBytes("ASCII"));

    //----------------------------------------------------
    // Get SHA-256 of key
    //----------------------------------------------------
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(key.getBytes("ASCII"));
    byte[] keyDigest = md.digest();

    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| Decrypt\n");
    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| key:                   %s\n", key);
    //System.out.format("| token:                 %s\n", input);

    //----------------------------------------------------
    // Rip up the ciphertext
    //----------------------------------------------------
    byte[] ivBytes = new byte[12];
    ivBytes = Arrays.copyOfRange(inputBytes, 0, ivBytes.length);

    byte[] cipherBytes = new byte[inputBytes.length - ivBytes.length];
    cipherBytes = Arrays.copyOfRange(inputBytes, ivBytes.length, inputBytes.length);

    //----------------------------------------------------
    // Decrypt
    //----------------------------------------------------
    AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
    cipher.init(false, new AEADParameters(new KeyParameter(keyDigest), MAC_SIZE_BITS, ivBytes));

    //System.out.format("+-------------------------------------------------------------\n");
    //System.out.format("| iv:                    %s\n", bytesToHex(ivBytes));
    //System.out.format("| ciphertext:            %s\n", bytesToHex(Arrays.copyOfRange(cipherBytes, 0, cipherBytes.length - 16)));
    //System.out.format("| tag:                   %s\n", bytesToHex(Arrays.copyOfRange(cipherBytes, cipherBytes.length - 16, cipherBytes.length)));
    //System.out.format("+-------------------------------------------------------------\n");

    byte[] dec = new byte[cipher.getOutputSize(cipherBytes.length)];

    try {
        int res = cipher.processBytes(cipherBytes, 0, cipherBytes.length, dec, 0);
        cipher.doFinal(dec, res);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    //System.out.format("token: %s\n", new String(dec, "ASCII"));
    return new String(dec, "ASCII");
}

From source file:at.archistar.crypto.symmetric.AESEncryptor.java

@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
        InvalidAlgorithmParameterException, InvalidCipherTextException {

    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
    return cipherData(cipher, data);
}

From source file:at.archistar.crypto.symmetric.AESEncryptor.java

@Override
public byte[] decrypt(byte[] data, byte[] randomKeyBytes) throws InvalidKeyException,
        InvalidAlgorithmParameterException, IOException, IllegalStateException, InvalidCipherTextException {

    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
    cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
    return cipherData(cipher, data);
}

From source file:at.archistar.crypto.symmetric.AESGCMEncryptor.java

@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
        InvalidAlgorithmParameterException, InvalidCipherTextException, ImpossibleException {

    AEADBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
    cipher.init(true, new AEADParameters(new KeyParameter(randomKeyBytes), 128, randomIvBytes));
    return cipherData(cipher, data);
}

From source file:at.archistar.crypto.symmetric.AESGCMEncryptor.java

@Override
public byte[] decrypt(byte[] data, byte[] randomKey)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IOException, IllegalStateException,
        InvalidCipherTextException, ImpossibleException {

    AEADBlockCipher cipher = new GCMBlockCipher(new AESFastEngine());
    cipher.init(false, new AEADParameters(new KeyParameter(randomKey), 128, randomIvBytes));
    return cipherData(cipher, data);
}

From source file:at.archistar.crypto.symmetric.ChaCha20Encryptor.java

@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
        InvalidAlgorithmParameterException, InvalidCipherTextException, ImpossibleException {

    ChaChaEngine cipher = new ChaChaEngine();
    cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));

    byte[] result = new byte[data.length];
    cipher.processBytes(data, 0, data.length, result, 0);
    return result;
}

From source file:at.archistar.crypto.symmetric.ChaCha20Encryptor.java

@Override
public byte[] decrypt(byte[] data, byte[] randomKeyBytes)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IOException, ImpossibleException,
        IllegalStateException, InvalidCipherTextException {

    ChaChaEngine cipher = new ChaChaEngine();
    cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));

    byte[] result = new byte[data.length];
    cipher.processBytes(data, 0, data.length, result, 0);
    return result;
}

From source file:br.com.elotech.karina.service.impl.GeradorSenhaElotech.java

@SneakyThrows
private String internalGenerate(String concatLicense) {

    System.out.println(concatLicense);

    String keyForSha1 = DigestUtils.md5Hex(KEY_ELOTECH).toUpperCase();
    System.out.println(keyForSha1);

    String keyForIdea = new String(Hex.encode(DigestUtils.getSha1Digest().digest(keyForSha1.getBytes())))
            .toUpperCase();//from  w  ww. ja  va  2 s . co  m

    System.out.println(keyForIdea);

    byte[] input = concatLicense.getBytes();
    byte[] out = new byte[input.length];

    KeyParameter keyParameter = new KeyParameter(keyForIdea.getBytes());

    BufferedBlockCipher cipher = new BufferedBlockCipher(new IDEAEngine());

    cipher.init(true, keyParameter);

    cipher.processBytes(input, 0, input.length, out, 0);

    String ideaSecret = Base64.getEncoder().encodeToString(out);

    System.out.println(ideaSecret);

    return null;

}