Example usage for java.security SecureRandom SecureRandom

List of usage examples for java.security SecureRandom SecureRandom

Introduction

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

Prototype

public SecureRandom() 

Source Link

Document

Constructs a secure random number generator (RNG) implementing the default random number algorithm.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SecureRandom sr = new SecureRandom();

    byte[] bytes = new byte[8];
    sr.nextBytes(bytes);//www.  ja va2  s .  c o m
    System.out.println(Arrays.toString(bytes));
}

From source file:MainClass.java

public static void main(String[] unused) {
    Random prng = new SecureRandom(); // self-seeding
    System.out.println(BigInteger.probablePrime(10, prng));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    int certainty = 90; // 1 - 1/2(90) certainty
    BigInteger mod = new BigInteger(bitLength, certainty, rnd);

    System.out.println(mod);/*w w w .  ja v  a 2s.  co m*/
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    int certainty = 90; // 1 - 1/2(90) certainty
    System.out.println("BitLength : " + bitLength);
    BigInteger mod = new BigInteger(bitLength, certainty, rnd);
    BigInteger exponent = BigInteger.probablePrime(bitLength, rnd);
    BigInteger n = BigInteger.probablePrime(bitLength, rnd);

    BigInteger result = n.modPow(exponent, mod);
    System.out.println("Number ^ Exponent MOD Modulus = Result");
    System.out.println("Number");
    System.out.println(n);//from   w ww  .  j a va2s  . com
    System.out.println("Exponent");
    System.out.println(exponent);
    System.out.println("Modulus");
    System.out.println(mod);
    System.out.println("Result");
    System.out.println(result);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    SecureRandom random = new SecureRandom();
    byte[] keyBytes = new byte[20];
    random.nextBytes(keyBytes);/*  w ww .  j ava2  s.  co m*/
    SecretKeySpec key = new SecretKeySpec(keyBytes, "HMACSHA1");

    System.out.println("Key:" + new BASE64Encoder().encode(key.getEncoded()));

    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(key);

    mac.update("test".getBytes("UTF8"));
    byte[] result = mac.doFinal();

    System.out.println("MAC: " + new BASE64Encoder().encode(result));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
    kpg.initialize(1024, new SecureRandom());
    KeyPair dsaKeyPair = kpg.generateKeyPair();

}

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   w  w w  .  j  a va  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());

    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);// w  w  w  .j  a v  a 2  s .  c  o m

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

From source file:cn.lynx.emi.license.GenerateKeyPairs.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    SecureRandom secrand = new SecureRandom();
    secrand.setSeed("cn.lynx.emi".getBytes());
    keygen.initialize(4096, secrand);//from  w ww  . j a  v a 2  s .  c o  m
    KeyPair keys = keygen.genKeyPair();

    PublicKey pubkey = keys.getPublic();
    PrivateKey prikey = keys.getPrivate();

    String myPubKey = Base64.encodeBase64String(pubkey.getEncoded());
    String myPriKey = Base64.encodeBase64String(prikey.getEncoded());
    System.out.println(prikey);
    System.out.println("pubKey=" + myPubKey);
    System.out.println("priKey=" + myPriKey);
}

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);/*w  w  w.  j  av  a  2s  .  com*/

    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));
}