Package javax.crypto.spec

Examples of javax.crypto.spec.DESKeySpec


 
  @Before
  public void before() throws Exception
  {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    Key key = factory.generateSecret(new DESKeySpec(Base64.decodeBase64(KEY.getBytes())));
   
    this.codec = new CipherCodec(key);
  }
View Full Code Here


   * @param keyBytes 符合DES要求的密钥
   * @param mode Cipher.ENCRYPT_MODE 或 Cipher.DECRYPT_MODE
   */
  private static byte[] des(byte[] inputBytes, byte[] keyBytes, int mode) {
    try {
      DESKeySpec desKeySpec = new DESKeySpec(keyBytes);
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
      SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

      Cipher cipher = Cipher.getInstance(DES);
      cipher.init(mode, secretKey);
View Full Code Here

    } else {
      throw new IllegalArgumentException("Key must be 8 or 24 bytes");
    }
    KeySpec keySpec = _triple ?
        (KeySpec) new DESedeKeySpec(key) :
        (KeySpec) new DESKeySpec(key);
    _key = SecretKeyFactory.getInstance(_triple ? "DESede" : "DES").generateSecret(keySpec);
  }
View Full Code Here

        // Flip bytes (reverse bits) in key
        for (int i = 0; i < key.length; i++) {
            key[i] = flipByte(key[i]);
        }

        KeySpec desKeySpec = new DESKeySpec(key);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
View Full Code Here

  public void doInit(String key) throws Exception
  {
    while(((float)key.length()/8) != key.length()/8) key += "Z";

    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

    ecipher = Cipher.getInstance("DES");
    dcipher = Cipher.getInstance("DES");
View Full Code Here

public class PasswordUtility {
  private static SecretKey key = genetateKey();
           
    public static SecretKey genetateKey () {
        try {
            DESKeySpec keySpec = new DESKeySpec (
                    "My secret Key phrase".getBytes("UTF8"));
            SecretKeyFactory keyFactory =
                    SecretKeyFactory.getInstance("DES");
            return keyFactory.generateSecret(keySpec);
            //return KeyGenerator.getInstance("DES").generateKey();       
View Full Code Here

        byte[] toCrypt = new byte[LONG_SIZE_IN_BYTES + dataBytes.length];
        System.arraycopy(prependedBytes, 0, toCrypt, 0, LONG_SIZE_IN_BYTES);
        System.arraycopy(dataBytes, 0, toCrypt, LONG_SIZE_IN_BYTES, dataBytes.length);


        DESKeySpec desKeySpec = new DESKeySpec(CRYPTO_PASSWORD.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(cipher);
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

        Cipher desCipher = Cipher.getInstance(cipher);
        desCipher.init(Cipher.ENCRYPT_MODE, secretKey);
View Full Code Here

     * @throws InvalidKeySpecException if the given key specification is inappropriate for this secret-key factory to
     *         produce a secret key.
     * @throws IOException if an I/O error occurs.
     */
    public static String decrypt(String cipher, String data) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IOException {
        DESKeySpec desKeySpec = new DESKeySpec(CRYPTO_PASSWORD.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(cipher);
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

        Cipher desCipher = Cipher.getInstance(cipher);
        desCipher.init(Cipher.DECRYPT_MODE, secretKey);
View Full Code Here

  private static Key key;

  private static Key getKey() throws GeneralSecurityException {
    if(key == null) {
      final KeySpec keySpec = new DESKeySpec(encKey);
      final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CIPHER_TRANSFORMATION);
      key = keyFactory.generateSecret(keySpec);
    }
    return key;
  }
View Full Code Here

   * @return An encryption key value implementing the DES encryption algorithm.
   */
  private static SecretKey createKey() throws GeneralSecurityException,
      UnsupportedEncodingException {
    byte[] bytes = ENCRYPTION_KEY.getBytes("UTF8");
    DESKeySpec spec = new DESKeySpec(bytes);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
    return keyFactory.generateSecret(spec);
  }
View Full Code Here

TOP

Related Classes of javax.crypto.spec.DESKeySpec

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.