List of usage examples for javax.crypto.spec DESKeySpec getKey
public byte[] getKey()
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("DES"); kg.init(new SecureRandom()); SecretKey key = kg.generateKey(); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); Class spec = Class.forName("javax.crypto.spec.DESKeySpec"); DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("keyfile")); oos.writeObject(ks.getKey()); Cipher c = Cipher.getInstance("DES/CFB8/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("ciphertext"), c); PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos)); pw.println("Stand and unfold yourself"); pw.close();/* w w w .j a v a 2 s. c o m*/ oos.writeObject(c.getIV()); oos.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("DES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); random.setSeed(101L);/*from w w w. j a v a 2 s . c om*/ keyGen.init(56, random); SecretKey sKey = keyGen.generateKey(); SecretKeyFactory kfactory = SecretKeyFactory.getInstance("DES"); DESKeySpec kspec = (DESKeySpec) kfactory.getKeySpec(sKey, DESKeySpec.class); System.out.println(sKey); FileOutputStream fos = new FileOutputStream("secretKeys"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(kspec.getKey()); FileInputStream fin = new FileInputStream("secretKeys"); ObjectInputStream ois = new ObjectInputStream(fin); byte[] kMaterial = (byte[]) ois.readObject(); DESKeySpec keyspec = new DESKeySpec(kMaterial); SecretKey newKey = kfactory.generateSecret(keyspec); System.out.println(newKey); System.out.println("Do the keys equal :" + newKey.equals(sKey)); }
From source file:it.scoppelletti.security.keygen.DESKeyToPropertySetProvider.java
public Properties toProperties(Key key) { byte[] data;/*from ww w . j a v a 2s . c o m*/ SecretKey desKey; SecretKeyFactory keyFactory; DESKeySpec param; Properties props; if (!(key instanceof SecretKey)) { return null; } try { keyFactory = SecretKeyFactory.getInstance(DESKeyFactory.ALGORITHM); } catch (NoSuchAlgorithmException ex) { return null; } try { desKey = keyFactory.translateKey((SecretKey) key); } catch (InvalidKeyException ex) { return null; } try { param = (DESKeySpec) keyFactory.getKeySpec(desKey, DESKeySpec.class); } catch (InvalidKeySpecException ex) { return null; } props = new Properties(); props.setProperty(CryptoUtils.PROP_KEYFACTORY, DESKeyFactory.class.getName()); data = param.getKey(); props.setProperty(DESKeyFactory.PROP_KEY, Hex.encodeHexString(data)); Arrays.fill(data, Byte.MIN_VALUE); return props; }
From source file:org.duracloud.common.util.EncryptionUtil.java
public EncryptionUtil(String key) throws DuraCloudRuntimeException { if (key == null) { throw new IllegalArgumentException("'key' parameter must be non-null"); }/* www. j a va 2s .c om*/ int keySize = DEFAULT_KEY.length(); if (key.length() > keySize) { key = key.substring(0, keySize); } key = StringUtils.leftPad(key, keySize); this.keyBytes = key.getBytes(); try { // Create cipher this.cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // Create Key DESKeySpec deskey = new DESKeySpec(this.keyBytes); this.key = new SecretKeySpec(deskey.getKey(), "DES"); } catch (Exception e) { throw new DuraCloudRuntimeException(e); } }
From source file:org.kuali.rice.core.impl.encryption.DemonstrationGradeEncryptionServiceImpl.java
/** * /*w w w . j a v a 2s . c om*/ * This method generates keys. This method is implementation specific and should not be present in any general purpose interface * extracted from this class. * * @return * @throws Exception */ public static String generateEncodedKey() throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecretKey desKey = keygen.generateKey(); // Create the cipher Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init((Cipher.WRAP_MODE), desKey); 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.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)); }