congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
KeyGenerator.generateKey
Code IndexAdd Tabnine to your IDE (free)

How to use
generateKey
method
in
javax.crypto.KeyGenerator

Best Java code snippets using javax.crypto.KeyGenerator.generateKey (Showing top 20 results out of 3,717)

Refine searchRefine arrow

  • KeyGenerator.init
  • KeyGenerator.getInstance
  • SecretKey.getEncoded
origin: gocd/gocd

public static SecretKey generateAESKey() throws NoSuchAlgorithmException {
  KeyGenerator generator = KeyGenerator.getInstance("AES");
  generator.init(128); // The AES key size in number of bits
  return generator.generateKey();
}
origin: gocd/gocd

private byte[] generateKey() throws NoSuchAlgorithmException {
  KeyGenerator keygen = KeyGenerator.getInstance("AES");
  keygen.init(128);
  byte[] key = keygen.generateKey().getEncoded();
  return key;
}
origin: pentaho/pentaho-kettle

public static Key generateSingleKey() throws NoSuchAlgorithmException {
 Key key = KeyGenerator.getInstance( SINGLE_KEY_ALGORITHM ).generateKey();
 return key;
}
origin: jenkinsci/jenkins

public CombinedCipherOutputStream(OutputStream out, Cipher asym, String algorithm) throws IOException, GeneralSecurityException {
  super(out);
  // create a new symmetric cipher key used for this stream
  String keyAlgorithm = getKeyAlgorithm(algorithm);
  SecretKey symKey = KeyGenerator.getInstance(keyAlgorithm).generateKey();
  // place the symmetric key by encrypting it with asymmetric cipher
  out.write(asym.doFinal(symKey.getEncoded()));
  // the rest of the data will be encrypted by this symmetric cipher
  Cipher sym = Secret.getCipher(algorithm);
  sym.init(Cipher.ENCRYPT_MODE,symKey, keyAlgorithm.equals(algorithm) ? null : new IvParameterSpec(symKey.getEncoded()));
  super.out = new CipherOutputStream(out,sym);
}
origin: stackoverflow.com

 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); // bm is the bitmap object   
byte[] b = baos.toByteArray();  

byte[] keyStart = "this is a key".getBytes();
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(keyStart);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] key = skey.getEncoded();    

// encrypt
byte[] encryptedData = encrypt(key,b);
// decrypt
byte[] decryptedData = decrypt(key,encryptedData);
origin: stackoverflow.com

 import java.nio.file.Files;
import java.nio.file.Paths;

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey key = kgen.generateKey();
byte[] encoded = key.getEncoded();
Files.write(Paths.get("target-file"), encoded);
origin: stackoverflow.com

 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256); // for example
SecretKey secretKey = keyGen.generateKey();
origin: gocd/gocd

  @Override
  public byte[] createIV() throws NoSuchAlgorithmException {
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);
    return keygen.generateKey().getEncoded();
  }
}
origin: wildfly/wildfly

public static SecretKey createSecretKey(String sym_alg, int key_size) throws NoSuchAlgorithmException {
  // KeyGenerator keyGen=KeyGenerator.getInstance(getAlgorithm(sym_alg));
  KeyGenerator keyGen=KeyGenerator.getInstance(sym_alg);
  keyGen.init(key_size);
  return keyGen.generateKey();
}
origin: alibaba/jstorm

  /**
   * Produce a blowfish key to be used in "Storm jar" command
   */
  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();
    }
  }
}
origin: stackoverflow.com

 KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey key = kgen.generateKey();
byte[] encoded = key.getEncoded();
FileOutputStream output = new FileOutputStream(new File("target-file"));
IOUtils.write(encoded, output);
origin: google/data-transfer-project

@Override
public SecretKey generate() {
 try {
  KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
  return generator.generateKey();
 } catch (NoSuchAlgorithmException e) {
  monitor.severe(() -> "NoSuchAlgorithmException for: " + ALGORITHM, e);
  throw new RuntimeException("Error creating key generator", e);
 }
}
origin: aa112901/remusic

private static byte[] genKey() {
  if (!isInited) {
    init();
  }
  //首先 生成一个密钥(SecretKey),
  //然后,通过这个秘钥,返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null。
  return keyGen.generateKey().getEncoded();
}
origin: stackoverflow.com

public byte[] keyGen() throws NoSuchAlgorithmException {
 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
 keyGenerator.init(192);
 return keyGenerator.generateKey().getEncoded();
}
origin: SonarSource/sonarqube

String generateRandomSecretKey() {
 try {
  KeyGenerator keyGen = KeyGenerator.getInstance(CRYPTO_KEY);
  keyGen.init(KEY_SIZE_IN_BITS, new SecureRandom());
  SecretKey secretKey = keyGen.generateKey();
  return Base64.encodeBase64String(secretKey.getEncoded());
 } catch (Exception e) {
  throw new IllegalStateException("Fail to generate secret key", e);
 }
}
origin: mpusher/mpush

public static SecretKey getSecretKey(byte[] seed) throws Exception {
  SecureRandom secureRandom = new SecureRandom(seed);
  KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
  keyGenerator.init(secureRandom);
  return keyGenerator.generateKey();
}
origin: jenkinsci/jenkins

private SecretKey getKey() {
  if (key==null) {
    synchronized (this) {
      if (key==null) {
        try {
          byte[] encoded = load();
          if (encoded==null) {
            KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
            SecretKey key = kg.generateKey();
            store(encoded=key.getEncoded());
          }
          key = new SecretKeySpec(encoded,ALGORITHM);
        } catch (IOException e) {
          throw new Error("Failed to load the key: "+getId(),e);
        } catch (NoSuchAlgorithmException e) {
          throw new Error("Failed to load the key: "+getId(),e);
        }
      }
    }
  }
  return key;
}
origin: stackoverflow.com

 // 1. Generate a session key
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128)
SecretKey sessionKey = keyGen.generateKey();

// 2. Encrypt the session key with the RSA public key
Cipher rsaCipher = Cipher.getInstance("RSA");
rsaCipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey)
byte[] encryptedSessionKey = rsaCipher.doFinal(sessionKey.getEncoded());

// 3. Encrypt the data using the session key (unencrypted)
Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, sessionKey); <-- sessionKey is the unencrypted
//                                                   session key.
// ... use aesCipher to encrypt your data

// 4. Save the encrypted data along with the encrypted 
// session key (encryptedSessionKey).
// PLEASE NOTE THAT BECAUSE OF THE ENCRYPTION MODE (CBC),
// YOU ALSO NEED TO ALSO SAVE THE IV (INITIALIZATION VECTOR).
// aesCipher.aesCipher.getParameters().
//     getParametersSpec(IvParameters.class).getIV();
origin: jwtk/jjwt

  gen = KeyGenerator.getInstance(alg.getJcaName());
} catch (NoSuchAlgorithmException e) {
  throw new IllegalStateException("The " + alg.getJcaName() + " algorithm is not available.  " +
return gen.generateKey();
origin: stackoverflow.com

 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();
javax.cryptoKeyGeneratorgenerateKey

Javadoc

Generates a secret key.

Popular methods of KeyGenerator

  • getInstance
    Creates a new KeyGenerator instance that provides the specified key algorithm from the specified pro
  • init
    Initializes this KeyGenerator instance with the specified algorithm parameters and randomness source
  • getProvider
    Returns the provider of this KeyGenerator instance.
  • <init>
    Creates a new KeyGenerator instance.
  • getAlgorithm
    Returns the name of the key generation algorithm.
  • initialize

Popular in Java

  • Start an intent from android
  • addToBackStack (FragmentTransaction)
  • startActivity (Activity)
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • Container (java.awt)
    A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT co
  • BufferedInputStream (java.io)
    A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the i
  • EOFException (java.io)
    Thrown when a program encounters the end of a file or stream during an input operation.
  • ResultSet (java.sql)
    An interface for an object which represents a database table entry, returned as the result of the qu
  • SQLException (java.sql)
    An exception that indicates a failed JDBC operation. It provides the following information about pro
  • LoggerFactory (org.slf4j)
    The LoggerFactory is a utility class producing Loggers for various logging APIs, most notably for lo
  • Top Vim plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now