Using the KeyGenerator class and showing how to create a SecretKeySpec from an encoded key : Key Generator « Security « Java





Using the KeyGenerator class and showing how to create a SecretKeySpec from an encoded key

   
import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Basic example using the KeyGenerator class and showing how to create a
 * SecretKeySpec from an encoded key.
 */
public class MainClass {
  public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());        
    byte[] input = "www.java2s.com".getBytes();
    
    byte[] ivBytes = new byte[] { 0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x01 };

    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
    KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");

    generator.init(192);

    Key encryptionKey = generator.generateKey();

    System.out.println("key   : " + Utils.toHex(encryptionKey.getEncoded()));
    System.out.println("input : " + new String(input));

    // encryption pass
    cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes));
    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
    ctLength += cipher.doFinal(cipherText, ctLength);

    // decryption pass
    Key decryptionKey = new SecretKeySpec(encryptionKey.getEncoded(), encryptionKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec(ivBytes));
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
    int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    ptLength += cipher.doFinal(plainText, ptLength);
    System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength);
  }
}


class Utils
{
    private static String digits = "0123456789abcdef";
    
    /**
     * Return length many bytes of the passed in byte array as a hex string.
     * 
     * @param data the bytes to be converted.
     * @param length the number of bytes in the data block to be converted.
     * @return a hex representation of length bytes of data.
     */
    public static String toHex(byte[] data, int length)
    {
        StringBuffer  buf = new StringBuffer();
        
        for (int i = 0; i != length; i++)
        {
            int v = data[i] & 0xff;
            
            buf.append(digits.charAt(v >> 4));
            buf.append(digits.charAt(v & 0xf));
        }
        
        return buf.toString();
    }
    
    /**
     * Return the passed in byte array as a hex string.
     * 
     * @param data the bytes to be converted.
     * @return a hex representation of data.
     */
    public static String toHex(byte[] data)
    {
        return toHex(data, data.length);
    }
}

           
         
    
    
  










HowToCreateASecretKeySpecFromAnEncodedKey.zip( 1,199 k)

Related examples in the same category

1.Key Generator Mac
2.Generate DSA key pair
3.KeyPair Generator For Private Key
4.KeyPair Generator For Public Key
5.Wrap And Unwrap Key
6.Generating a Public/Private Key Pair
7.Generate a 576-bit DH key pair
8.Generate a 1024-bit RSA key pair
9.Getting the Bytes of a Generated Key Pair
10.Get the bytes of the public and private keys
11.The bytes can be converted back to public and private key objects
12.Generate a key for the HMAC-SHA1 keyed-hashing algorithm
13.Asymmetric Key Maker
14.Generating a Symmetric Key
15.Xml dap Certs And Keys
16.Diffie-Hellman key exchange