Package javax.crypto

Examples of javax.crypto.SecretKey


   */
  public static byte[] createAESKey() {
    try {
          KeyGenerator kgen = KeyGenerator.getInstance("AES");
          kgen.init(0x100);
          SecretKey skey = kgen.generateKey();
          return skey.getEncoded();
        } catch (NoSuchAlgorithmException e) {
           throw new IllegalArgumentException("Cannot find key generator: AES", e);
        }
  }
View Full Code Here


   * Encrypts griven string with given pass-phrase with DES.
   */
  public static String cipher(String msg, byte[] passPhrase) {
    try {
      KeySpec keySpec = new DESKeySpec(passPhrase);
      SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(
          keySpec);
      Cipher cipher = Cipher.getInstance(key.getAlgorithm());
      cipher.init(Cipher.ENCRYPT_MODE, key);
      return toHexString(cipher.doFinal(msg.getBytes()));
    } catch (Exception e) {
      return null;
    }
View Full Code Here

   * Decrypts griven string with given pass-phrase with DES.
   */
  public static String decipher(String msg, byte[] passPhrase) {
    try {
      KeySpec keySpec = new DESKeySpec(passPhrase);
      SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(
          keySpec);
      Cipher cipher = Cipher.getInstance(key.getAlgorithm());
      cipher.init(Cipher.DECRYPT_MODE, key);
      return new String(cipher.doFinal(fromHexString(msg)));
    } catch (Exception e) {
      return null;
    }
View Full Code Here

      System.out.println("Decrypted: "+theMsg);

      // Try a key that should fail
      KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
      kgen.init(320);
      SecretKey key = kgen.generateKey();
      cipher.init(Cipher.DECRYPT_MODE, key);
      try
      {
         String tmp = (String) msg.getObject(cipher);
         throw new IllegalArgumentException("Should have failed to decrypt the msg");
View Full Code Here

   static void testKey() throws Exception
   {
      int size = 8 * 24;
      KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
      kgen.init(size);
      SecretKey key = kgen.generateKey();
      byte[] kbytes = key.getEncoded();
      System.out.println("key.Algorithm = "+key.getAlgorithm());
      System.out.println("key.Format = "+key.getFormat());
      System.out.println("key.Encoded Size = "+kbytes.length);
     
      SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
      BigInteger bi = new BigInteger(320, rnd);
      byte[] k2bytes = bi.toByteArray();
      SecretKeySpec keySpec = new SecretKeySpec(k2bytes, "Blowfish");
      System.out.println("key2.Algorithm = "+key.getAlgorithm());
      System.out.println("key2.Format = "+key.getFormat());
      System.out.println("key2.Encoded Size = "+kbytes.length);
   }
View Full Code Here

        }
      }
      key = (new DESedeKeySpec(desedekey)).getKey();
    }

    SecretKey secretKey = new SecretKeySpec(key,algorithm);

    /* DES in ECB mode does not require any parameters */
    if (mode.equals("ECB")) {
      _cipher.init(opmode, secretKey);

View Full Code Here

      if (salt.length() > 8) salt = salt.substring(0,8);
      if (salt.length() < 8) salt = (salt + "XXXXXXXX").substring(0,8);
     
      // create the PBE key
      final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(method);
      final SecretKey key = keyFactory.generateSecret(keySpec);

      // create parameter spec for PBE
      final PBEParameterSpec paramSpec = new PBEParameterSpec(UTF8.getBytes(salt), 1000 /*ITERATIONS*/);
       
      // Create a cipher and initialize it for encrypting end decrypting
View Full Code Here

        try {
            KeyGenerator generator = provider == null
                                     ? KeyGenerator.getInstance(cipherName)
                                     : KeyGenerator.getInstance(cipherName,
                                         provider);
            SecretKey key = generator.generateKey();
            byte[]    raw = key.getEncoded();

            return raw;
        } catch (java.security.NoSuchAlgorithmException e) {
            throw Error.error(ErrorCode.X_S0531, e);
        } catch (NoSuchProviderException e) {
View Full Code Here

            //Cipher encrypt = Cipher.getInstance("DES");           
            //SecretKey key = new SecretKeySpec(pass.getBytes(), "DES");
           
            Cipher encrypt = Cipher.getInstance("DESede/ECB/NoPadding", "BC");

            SecretKey key = new SecretKeySpec(pass.getBytes(), "DESede");

            encrypt.init(Cipher.ENCRYPT_MODE, key);

            // encode string mit utf8           
            byte[] utf8 = str.getBytes(SOSCrypt.charset);
View Full Code Here

            //Cipher decrypt = Cipher.getInstance("DES");
            //SecretKey key = new SecretKeySpec(pass.getBytes(), "DES");
            Cipher decrypt = Cipher.getInstance("DESede/ECB/NoPadding", "BC");

            SecretKey key = new SecretKeySpec(pass.getBytes(), "DESede");

            decrypt.init(Cipher.DECRYPT_MODE, key);


            // decode base64 zu bytes                               
View Full Code Here

TOP

Related Classes of javax.crypto.SecretKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.