List of usage examples for javax.crypto SecretKey getEncoded
public byte[] getEncoded();
From source file:Main.java
public static void main(String[] argv) throws Exception { // Generate a key KeyGenerator keyGen = KeyGenerator.getInstance("DESede"); SecretKey key = keyGen.generateKey(); // Get the bytes of the key byte[] keyBytes = key.getEncoded(); int numBytes = keyBytes.length; // The bytes can be converted back to a SecretKey SecretKey key2 = new SecretKeySpec(keyBytes, "DESede"); boolean b = key.equals(key2); // true }
From source file:backtype.storm.security.serialization.BlowfishTupleSerializer.java
/** * Produce a blowfish key to be used in "Storm jar" command *//*from w w w . j a v a 2s . c o m*/ public static void main(String[] args) { try { KeyGenerator kgen = KeyGenerator.getInstance("Blowfish"); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); String keyString = new String(Hex.encodeHex(raw)); System.out.println("storm -c " + SECRET_KEY + "=" + keyString + " -c " + Config.TOPOLOGY_TUPLE_SERIALIZER + "=" + BlowfishTupleSerializer.class.getName() + " ..."); } catch (Exception ex) { LOG.error(ex.getMessage()); ex.printStackTrace(); } }
From source file:org.apache.storm.security.serialization.BlowfishTupleSerializer.java
/** * Produce a blowfish key to be used in "Storm jar" command *///w ww . ja v a 2 s. c o m public static void main(String[] args) { try { KeyGenerator kgen = KeyGenerator.getInstance("Blowfish"); kgen.init(256); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); String keyString = new String(Hex.encodeHex(raw)); System.out.println("storm -c " + SECRET_KEY + "=" + keyString + " -c " + Config.TOPOLOGY_TUPLE_SERIALIZER + "=" + BlowfishTupleSerializer.class.getName() + " ..."); } catch (Exception ex) { LOG.error(ex.getMessage()); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "11111111111111111111111111111111111111111111111111" + "2F78C7"; BigInteger base = BigInteger.valueOf(2); BigInteger modulous = new BigInteger(s, 16); DHParameterSpec skipParameterSpec = new DHParameterSpec(modulous, base); KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH"); kpg1.initialize(skipParameterSpec);/* w w w.j a v a2 s. c om*/ KeyPair kp1 = kpg1.generateKeyPair(); KeyAgreement ka1 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate(); DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic(); ka1.init(privateKey1); System.out.println("1 is using " + publicKey1.getY() + " for its public key"); KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH"); kpg2.initialize(skipParameterSpec); KeyPair kp2 = kpg2.generateKeyPair(); KeyAgreement ka2 = KeyAgreement.getInstance("DH"); DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate(); DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic(); ka2.init(privateKey2); System.out.println("2 is using " + publicKey2.getY() + "for its public key"); // Use the KeyAgreement object of 1 to generate its shared key ka1.doPhase(publicKey2, true); SecretKey sharedKey1 = ka1.generateSecret("DES"); System.out.println("1 is using " + new String(sharedKey1.getEncoded()) + " as its DES session key"); // Use the KeyAgreement object of 2 to generate its shared key ka2.doPhase(publicKey1, true); SecretKey sharedKey2 = ka2.generateSecret("DES"); System.out.println("2 is using " + new String(sharedKey2.getEncoded()) + "as its DES session key"); }
From source file:org.apache.juddi.samples.DES.java
public static void main(String[] args) throws Exception { DES des = new DES(); KeyGenerator kgen;/*from w w w. ja va 2 s.c o m*/ try { kgen = KeyGenerator.getInstance(DESEDE_ENCRYPTION_SCHEME); kgen.init(168); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); myEncryptionKey = new String(Base64.encodeBase64(raw)); myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME; arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); ks = new DESedeKeySpec(arrayBytes); skf = SecretKeyFactory.getInstance(myEncryptionScheme); cipher = Cipher.getInstance(myEncryptionScheme); key = skf.generateSecret(ks); System.out.println(new String(Base64.encodeBase64(raw))); System.out.println(des.encrypt("password")); System.out.println(des.decrypt(des.encrypt("password"))); } catch (Exception ex) { ex.printStackTrace(); ; } }
From source file:Main.java
public static byte[] initKey(int size) throws NoSuchAlgorithmException { KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM); kg.init(size);//from ww w . j ava2s. com SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); }
From source file:Main.java
public static byte[] initKey() throws NoSuchAlgorithmException { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(256); //192 256 SecretKey secretKey = keyGen.generateKey(); return secretKey.getEncoded(); }
From source file:Main.java
public static byte[] generateKey(String password) throws Exception { byte[] keyStart = password.getBytes("UTF-8"); KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(keyStart);/*from w ww . j a v a2 s . c om*/ kgen.init(128, sr); SecretKey skey = kgen.generateKey(); return skey.getEncoded(); }
From source file:Main.java
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(seed);/*from w ww .j a va2s. c o m*/ kgen.init(256, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
From source file:Main.java
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(seed);/* w w w.j ava 2 s .co m*/ kgen.init(128, sr); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }