List of usage examples for javax.crypto KeyGenerator generateKey
public final SecretKey generateKey()
From source file:org.craftercms.security.authentication.impl.CipheredAuthenticationCookieFactory.java
/** * Generates a random encryption key./*from w w w.java 2 s .c o m*/ */ protected Key generateRandomKey() throws CrafterSecurityException { KeyGenerator keyGenerator; try { keyGenerator = KeyGenerator.getInstance(CIPHER_ALGORITHM); keyGenerator.init(secureRandom); return keyGenerator.generateKey(); } catch (Exception e) { throw new CrafterSecurityException("Unable to generate random encryption key", e); } }
From source file:com.tcloud.bee.key.server.service.impl.KeyManageServiceImpl.java
@Override public QueryResult createKey(Param param, String owner) throws NoSuchAlgorithmException, FileNotFoundException, IOException { logger.info("User is trying to create key. userName:" + owner + ", keyName:" + param.getKeyName()); File newKeyfile = new File(env.getProperty("keyfile.path") + param.getKeyName()); if (newKeyfile.exists()) { logger.info("keyName \"" + param.getKeyName() + "\" exists, please choose another keyName."); return new QueryResult(BeeConstants.ResponseStatus.FAIL, BeeConstants.ErrorMap.get(BeeConstants.ResponseCode.ERROR_KM_KEYNAME_EXISTS), null); }// w w w .j a v a 2 s . co m KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(256); SecretKey secretKey = keyGen.generateKey(); String hexkey = Hex.encodeHexString(secretKey.getEncoded()); Properties prop = new Properties(); prop.setProperty("owner", owner); prop.setProperty("keyName", param.getKeyName()); prop.setProperty("hexkey", hexkey); prop.setProperty("users", param.getUsers()); File keyFileFolder = new File(env.getProperty("keyfile.path")); if (!keyFileFolder.exists()) { keyFileFolder.mkdirs(); Runtime.getRuntime().exec("chmod 700 " + env.getProperty("keyfile.path")); } prop.store(new FileOutputStream(env.getProperty("keyfile.path") + param.getKeyName()), null); Runtime.getRuntime().exec("chmod 600 " + env.getProperty("keyfile.path") + param.getKeyName()); logger.info("save keyfile \"{}\" to keyfile folder: {}", param.getKeyName(), env.getProperty("keyfile.path")); return new QueryResult(BeeConstants.ResponseStatus.SUCCESS, "Key(" + param.getKeyName() + ") created", null); }
From source file:org.sonatype.sisu.encryptor.RsaAesEncryptor.java
public void encrypt(InputStream plainInput, OutputStream encryptedOutput, PublicKey key) throws IOException, GeneralSecurityException { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(KEY_SIZE);/* ww w. j ava2 s .c om*/ SecretKey aesKey = kgen.generateKey(); byte[] data = IOUtil.toByteArray(plainInput); byte[] encryptedData = getCipher("AES", aesKey, Cipher.ENCRYPT_MODE).doFinal(data); byte[] raw = aesKey.getEncoded(); byte[] encryptedKey = getCipher("RSA/ECB/PKCS1Padding", key, javax.crypto.Cipher.ENCRYPT_MODE).doFinal(raw); // useful when debugging but can't be left uncommented due to NEXUS-2530 // if ( getLogger().isDebugEnabled() ) // { // log.debug( "before encrypt: " + new String( Base64.encodeBase64( raw ) ) ); // log.debug( "Encrypted key: " + new String( Base64.encodeBase64( encryptedKey ) ) ); // log.debug( "Encrypted data: " + new String( Base64.encodeBase64( encryptedData ) ) ); // } Base64OutputStream output = new Base64OutputStream(encryptedOutput); IOUtil.copy(encryptedKey, output); IOUtil.copy(encryptedData, output); output.close(); encryptedOutput.flush(); }
From source file:com.projectsontracks.model.CaribooKey.java
/** * Creates a new AES key//from ww w .ja v a2s. c om * * @throws java.security.NoSuchAlgorithmException */ public void createKey() throws NoSuchAlgorithmException { // Initialize the key generator KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(this.size); // Generates the key SecretKey skey = kgen.generateKey(); // Returns the key in its primary encoding format, or null if this key does not support encoding. key = skey.getEncoded(); //used to construct a SecretKey from a byte array, without having to go through a (provider-based) SecretKeyFactory. keySpec = new SecretKeySpec(key, "AES"); }
From source file:org.kuali.rice.core.impl.encryption.EncryptionServiceImplTest.java
private String generateDESKey() throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecretKey desKey = keygen.generateKey(); SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES"); DESKeySpec desSpec = (DESKeySpec) desFactory.getKeySpec(desKey, javax.crypto.spec.DESKeySpec.class); byte[] rawDesKey = desSpec.getKey(); return new String(Base64.encodeBase64(rawDesKey)); }
From source file:org.apache.abdera.ext.oauth.OAuthScheme.java
private String sign(String method, String baseString, Certificate cert) throws AuthenticationException { if (method.equalsIgnoreCase("HMAC-MD5") || method.equalsIgnoreCase("HMAC-SHA1")) { try {//from w ww . j ava 2 s . c o m String[] tokens = method.split("-"); String methodName = tokens[0].substring(0, 1).toUpperCase() + tokens[0].substring(1).toLowerCase() + tokens[1]; KeyGenerator kg = KeyGenerator.getInstance(methodName); Mac mac = Mac.getInstance(kg.getAlgorithm()); mac.init(kg.generateKey()); byte[] result = mac.doFinal(baseString.getBytes()); return new String(Base64.encodeBase64(result)); } catch (Exception e) { throw new AuthenticationException(e.getMessage(), e); } } else if (method.equalsIgnoreCase("md5")) { return new String(Base64.encodeBase64(DigestUtils.md5(baseString))); } else if (method.equalsIgnoreCase("sha1")) { return new String(Base64.encodeBase64(DigestUtils.sha(baseString))); } else if (method.equalsIgnoreCase("RSA-SHA1")) { if (cert == null) { throw new AuthenticationException("a cert is mandatory to use SHA1 with RSA"); } try { Cipher cipher = Cipher.getInstance("SHA1withRSA"); cipher.init(Cipher.ENCRYPT_MODE, cert); byte[] result = cipher.doFinal(baseString.getBytes()); return new String(Base64.encodeBase64(result)); } catch (Exception e) { throw new AuthenticationException(e.getMessage(), e); } } else { throw new AuthenticationException("unsupported algorithm method: " + method); } }
From source file:org.apache.ws.security.message.SymmetricSignatureTest.java
/** * Setup method/* w ww . ja v a 2 s .c o m*/ * <p/> * * @throws Exception Thrown when there is a problem in setup */ @org.junit.Before public void setUp() throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey key = keyGen.generateKey(); keyData = key.getEncoded(); }
From source file:org.kuali.rice.core.impl.encryption.EncryptionServiceImplTest.java
private String generateDESedeKey() throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("DESede"); SecretKey desedeKey = keygen.generateKey(); SecretKeyFactory desedeFactory = SecretKeyFactory.getInstance("DESede"); DESedeKeySpec desedeSpec = (DESedeKeySpec) desedeFactory.getKeySpec(desedeKey, javax.crypto.spec.DESedeKeySpec.class); byte[] rawDesedeKey = desedeSpec.getKey(); return new String(Base64.encodeBase64(rawDesedeKey)); }
From source file:org.codice.ddf.configuration.migration.MigrationZipFileTest.java
private SecretKey createSecretKey(Path keyPath) throws Exception { KeyGenerator keyGenerator = null; keyGenerator = KeyGenerator.getInstance(MigrationZipConstants.KEY_ALGORITHM); SecretKey secretKey = keyGenerator.generateKey(); char[] hexKey = encodeHex(secretKey.getEncoded()); writeStringToFile(keyPath.toFile(), String.valueOf(hexKey), Charsets.UTF_8); return secretKey; }
From source file:org.kuali.rice.krad.devtools.maintainablexml.EncryptionService.java
private SecretKey unwrapEncodedKeyOld(String key) throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecretKey desKey = keygen.generateKey(); // Create the cipher Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init((Cipher.UNWRAP_MODE), desKey); byte[] bytes = Base64.decodeBase64(key.getBytes()); // If decoding was done with commons-codec 1.3 and the key not ended with '=' bytes[6] = 1;//ww w . j a v a 2 s. c o m bytes[7] = 1; SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES"); DESKeySpec keyspec = new DESKeySpec(bytes); SecretKey k = desFactory.generateSecret(keyspec); return k; }