Example usage for java.security Security addProvider

List of usage examples for java.security Security addProvider

Introduction

In this page you can find the example usage for java.security Security addProvider.

Prototype

public static int addProvider(Provider provider) 

Source Link

Document

Adds a provider to the next position available.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
    generator.init(128);/*w  ww.  j  a v a 2s . c o m*/
    Key keyToBeWrapped = generator.generateKey();
    System.out.println("input    : " + new String(keyToBeWrapped.getEncoded()));

    // create a wrapper and do the wrapping
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC");
    KeyGenerator keyGen = KeyGenerator.getInstance("AES", "BC");
    keyGen.init(256);
    Key wrapKey = keyGen.generateKey();
    cipher.init(Cipher.ENCRYPT_MODE, wrapKey);
    byte[] wrappedKey = cipher.doFinal(keyToBeWrapped.getEncoded());
    System.out.println("wrapped  : " + new String(wrappedKey));

    // unwrap the wrapped key
    cipher.init(Cipher.DECRYPT_MODE, wrapKey);
    Key key = new SecretKeySpec(cipher.doFinal(wrappedKey), "AES");
    System.out.println("unwrapped: " + new String(key.getEncoded()));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "BC");

    keyGen.initialize(512, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("DSA", "BC");

    signature.initSign(keyPair.getPrivate(), new SecureRandom());
    byte[] message = "abc".getBytes();
    signature.update(message);//from ww w . j  a v a2 s .  com

    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);
    System.out.println(signature.verify(sigBytes));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");

    keyGen.initialize(512, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("SHA1withRSA", "BC");

    signature.initSign(keyPair.getPrivate(), new SecureRandom());

    byte[] message = "abc".getBytes();
    signature.update(message);//from www  . j a  v  a  2  s  .  co  m

    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);
    System.out.println(signature.verify(sigBytes));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "input".getBytes();
    byte[] keyBytes = "input123".getBytes();
    byte[] ivBytes = "12345123".getBytes();

    SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(new byte[8]);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS7Padding", "BC");

    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

    byte[] cipherText = new byte[cipher.getOutputSize(ivBytes.length + input.length)];

    int ctLength = cipher.update(ivBytes, 0, ivBytes.length, cipherText, 0);

    ctLength += cipher.update(input, 0, input.length, cipherText, ctLength);

    ctLength += cipher.doFinal(cipherText, ctLength);

    cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);

    byte[] buf = new byte[cipher.getOutputSize(ctLength)];

    int bufLength = cipher.update(cipherText, 0, ctLength, buf, 0);

    bufLength += cipher.doFinal(buf, bufLength);

    byte[] plainText = new byte[bufLength - ivBytes.length];

    System.arraycopy(buf, ivBytes.length, plainText, 0, plainText.length);

    System.out.println("plain : " + new String(plainText));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "input".getBytes();
    byte[] keyBytes = "input123".getBytes();
    byte[] msgNumber = "input".getBytes();

    IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);

    SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");

    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS7Padding", "BC");

    cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

    IvParameterSpec encryptionIv = new IvParameterSpec(cipher.doFinal(msgNumber), 0, 8);

    cipher.init(Cipher.ENCRYPT_MODE, key, encryptionIv);

    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];

    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);

    ctLength += cipher.doFinal(cipherText, ctLength);

    System.out.println("cipher: " + new String(cipherText) + " bytes: " + ctLength);
    cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

    IvParameterSpec decryptionIv = new IvParameterSpec(cipher.doFinal(msgNumber), 0, 8);

    cipher.init(Cipher.DECRYPT_MODE, key, decryptionIv);

    byte[] plainText = new byte[cipher.getOutputSize(ctLength)];

    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);

    ptLength += cipher.doFinal(plainText, ptLength);

    System.out.println("plain : " + new String(plainText, ptLength));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    byte[] input = "www.java2s.com".getBytes();
    byte[] keyBytes = new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef };

    SecretKeySpec key = new SecretKeySpec(keyBytes, "DES");
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS7Padding", "BC");
    System.out.println("input : " + new String(input));

    // encryption pass
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);
    System.out.println("cipher: " + new String(cipherText) + " bytes: " + ctLength);

    // decryption pass

    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    ptLength += cipher.doFinal(plainText, ptLength);
    System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    AlgorithmParameters params = AlgorithmParameters.getInstance("AES", "BC");
    IvParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
    params.init(ivSpec);//from  w  w w  .j  a v a2 s  .c om
    ASN1InputStream aIn = new ASN1InputStream(params.getEncoded("ASN.1"));

    System.out.println(ASN1Dump.dumpAsString(aIn.readObject()));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "ab".getBytes();
    Cipher cipher = Cipher.getInstance("ElGamal/None/NoPadding", "BC");
    KeyPairGenerator generator = KeyPairGenerator.getInstance("ElGamal", "BC");
    SecureRandom random = new SecureRandom();

    generator.initialize(128, random);/*from w  w  w.ja  v a 2s. c  o  m*/

    KeyPair pair = generator.generateKeyPair();
    Key pubKey = pair.getPublic();
    Key privKey = pair.getPrivate();
    cipher.init(Cipher.ENCRYPT_MODE, pubKey, random);
    byte[] cipherText = cipher.doFinal(input);
    System.out.println("cipher: " + new String(cipherText));

    cipher.init(Cipher.DECRYPT_MODE, privKey);
    byte[] plainText = cipher.doFinal(cipherText);
    System.out.println("plain : " + new String(plainText));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "abc".getBytes();
    Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
    SecureRandom random = new SecureRandom();
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");

    generator.initialize(256, random);//from   w  ww  .  jav  a 2  s  .co  m

    KeyPair pair = generator.generateKeyPair();
    Key pubKey = pair.getPublic();
    Key privKey = pair.getPrivate();

    cipher.init(Cipher.ENCRYPT_MODE, pubKey, random);
    byte[] cipherText = cipher.doFinal(input);
    System.out.println("cipher: " + new String(cipherText));

    cipher.init(Cipher.DECRYPT_MODE, privKey);
    byte[] plainText = cipher.doFinal(cipherText);
    System.out.println("plain : " + new String(plainText));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    byte[] input = "abc".getBytes();
    Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");
    SecureRandom random = new SecureRandom();
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");

    generator.initialize(386, random);/*w  w  w. j  ava 2s . co  m*/

    KeyPair pair = generator.generateKeyPair();
    Key pubKey = pair.getPublic();

    Key privKey = pair.getPrivate();

    cipher.init(Cipher.ENCRYPT_MODE, pubKey, random);
    byte[] cipherText = cipher.doFinal(input);
    System.out.println("cipher: " + new String(cipherText));

    cipher.init(Cipher.DECRYPT_MODE, privKey);
    byte[] plainText = cipher.doFinal(cipherText);
    System.out.println("plain : " + new String(plainText));
}