Example usage for javax.crypto.spec DESKeySpec DESKeySpec

List of usage examples for javax.crypto.spec DESKeySpec DESKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec DESKeySpec DESKeySpec.

Prototype

public DESKeySpec(byte[] key) throws InvalidKeyException 

Source Link

Document

Creates a DESKeySpec object using the first 8 bytes in key as the key material for the DES key.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("keyfile"));
    DESKeySpec ks = new DESKeySpec((byte[]) ois.readObject());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey key = skf.generateSecret(ks);

    Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
    c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) ois.readObject()));
    CipherInputStream cis = new CipherInputStream(new FileInputStream("ciphertext"), c);
    BufferedReader br = new BufferedReader(new InputStreamReader(cis));
    System.out.println(br.readLine());
}

From source file:TestCipher.java

public static void main(String args[]) throws Exception {
    Set set = new HashSet();
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        Point point = new Point(random.nextInt(1000), random.nextInt(2000));
        set.add(point);//from ww w  . j a  va 2  s  . com
    }
    int last = random.nextInt(5000);

    // Create Key
    byte key[] = password.getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

    // Create Cipher
    Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    // Create stream
    FileOutputStream fos = new FileOutputStream("out.des");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    CipherOutputStream cos = new CipherOutputStream(bos, desCipher);
    ObjectOutputStream oos = new ObjectOutputStream(cos);

    // Write objects
    oos.writeObject(set);
    oos.writeInt(last);
    oos.flush();
    oos.close();

    // Change cipher mode
    desCipher.init(Cipher.DECRYPT_MODE, secretKey);

    // Create stream
    FileInputStream fis = new FileInputStream("out.des");
    BufferedInputStream bis = new BufferedInputStream(fis);
    CipherInputStream cis = new CipherInputStream(bis, desCipher);
    ObjectInputStream ois = new ObjectInputStream(cis);

    // Read objects
    Set set2 = (Set) ois.readObject();
    int last2 = ois.readInt();
    ois.close();

    // Compare original with what was read back
    int count = 0;
    if (set.equals(set2)) {
        System.out.println("Set1: " + set);
        System.out.println("Set2: " + set2);
        System.out.println("Sets are okay.");
        count++;
    }
    if (last == last2) {
        System.out.println("int1: " + last);
        System.out.println("int2: " + last2);
        System.out.println("ints are okay.");
        count++;
    }
    if (count != 2) {
        System.out.println("Problem during encryption/decryption");
    }
}

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  ww  w . j  a va  2s . c o  m
    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:DataStudioCrak.java

public static void main(String[] args) {
    String company = "labthink";
    byte[] companyByteArray = company.getBytes();
    byte[] companyByteIntArray = intToByteArray(compute(companyByteArray, companyByteArray.length));
    // byte[] ff = "zhulixia".getBytes();
    byte[] snByte = new byte[32];
    byte[] byte1 = new byte[] { 7, 1 };
    byte[] byte2 = "zhaodapengpojiehahahahahaha".getBytes();
    byte[] byte3 = new byte[] { 127 };
    byte[] snMain = new byte[24];
    System.arraycopy(byte1, 0, snMain, 0, 2);
    System.arraycopy(byte2, 0, snMain, 2, 17);
    System.arraycopy(companyByteIntArray, 0, snMain, 19, 4);
    System.arraycopy(byte3, 0, snMain, 23, 1);
    //  1 - single license,2 - site license ,3 educational license
    snMain[2] = (byte) 1;
    int intSn = compute(snMain, snMain.length);
    System.out.println("intSn=" + intSn);
    byte[] key1 = "dddd".getBytes();
    byte[] key2 = intToByteArray(intSn);
    byte[] key = new byte[8];
    System.arraycopy(key1, 0, key, 0, 4);
    System.arraycopy(key2, 0, key, 4, 4);

    byte encodedSnMain[] = new byte[snMain.length];
    try {/*w w  w. j a  va 2s  .co m*/
        DESKeySpec deskeyspec = new DESKeySpec(key);
        javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("DES").generateSecret(deskeyspec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretkey);
        cipher.update(snMain, 0, snMain.length, encodedSnMain);
        cipher.doFinal();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.arraycopy(key1, 0, snByte, 0, 4);
    System.arraycopy(key2, 0, snByte, 28, 4);
    System.arraycopy(encodedSnMain, 0, snByte, 4, 24);
    char[] snCharArray = Hex.encodeHex(snByte);
    String sn = new String(snCharArray);
    System.out.println("sn=" + sn);
}

From source file:net.labthink.run.DataStudioCrak.java

public static void main(String[] args) {
    String company = "Labthink";
    byte[] companyByteArray = company.getBytes();
    byte[] companyByteIntArray = intToByteArray(compute(companyByteArray, companyByteArray.length));
    // byte[] ff = "zhulixia".getBytes();
    byte[] snByte = new byte[32];
    byte[] byte1 = new byte[] { 7, 1 };
    byte[] byte2 = "zhaodapengpojiehahahahahaha".getBytes();
    byte[] byte3 = new byte[] { 127 };
    byte[] snMain = new byte[24];
    System.arraycopy(byte1, 0, snMain, 0, 2);
    System.arraycopy(byte2, 0, snMain, 2, 17);
    System.arraycopy(companyByteIntArray, 0, snMain, 19, 4);
    System.arraycopy(byte3, 0, snMain, 23, 1);
    //  1 - single license,2 - site license ,3 educational license
    snMain[2] = (byte) 1;
    int intSn = compute(snMain, snMain.length);
    System.out.println("intSn=" + intSn);
    byte[] key1 = "dddd".getBytes();
    byte[] key2 = intToByteArray(intSn);
    byte[] key = new byte[8];
    System.arraycopy(key1, 0, key, 0, 4);
    System.arraycopy(key2, 0, key, 4, 4);

    byte encodedSnMain[] = new byte[snMain.length];
    try {//from  w w  w. j a  v a  2 s . c  o  m
        DESKeySpec deskeyspec = new DESKeySpec(key);
        javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("DES").generateSecret(deskeyspec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretkey);
        cipher.update(snMain, 0, snMain.length, encodedSnMain);
        cipher.doFinal();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    System.arraycopy(key1, 0, snByte, 0, 4);
    System.arraycopy(key2, 0, snByte, 28, 4);
    System.arraycopy(encodedSnMain, 0, snByte, 4, 24);
    char[] snCharArray = Hex.encodeHex(snByte);
    String sn = new String(snCharArray);
    System.out.println("sn=" + sn);
}

From source file:uk.ac.ox.webauth.crypto.DesCbcCrc.java

public static void main(String[] args) throws Exception {
    /* mod-crc-32 tests from RFC 3961 section A.5:
       mod-crc-32("foo") =                                     33 bc 32 73
       mod-crc-32("test0123456789") =                          d6 88 3e b8
       mod-crc-32("MASSACHVSETTS INSTITVTE OF TECHNOLOGY") =   f7 80 41 e3
       mod-crc-32(8000) =                                      4b 98 83 3b
       mod-crc-32(0008) =                                      32 88 db 0e
       mod-crc-32(0080) =                                      20 83 b8 ed
       mod-crc-32(80) =                                        20 83 b8 ed
       mod-crc-32(80000000) =                                  3b b6 59 ed
       mod-crc-32(00000001) =                                  96 30 07 77
    *///from   w  w w.j  a v a  2s . c  o m
    test_modifiedCRC32(new String(Hex.encodeHex("foo".getBytes())), "33bc3273");
    test_modifiedCRC32(new String(Hex.encodeHex("test0123456789".getBytes())), "d6883eb8");
    test_modifiedCRC32(new String(Hex.encodeHex("MASSACHVSETTS INSTITVTE OF TECHNOLOGY".getBytes())),
            "f78041e3");
    test_modifiedCRC32("8000", "4b98833b");
    test_modifiedCRC32("0008", "3288db0e");
    test_modifiedCRC32("0080", "2083b8ed");
    test_modifiedCRC32("80", "2083b8ed");
    test_modifiedCRC32("80000000", "3bb659ed");
    test_modifiedCRC32("00000001", "96300777");
    SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
    KeySpec spec = new DESKeySpec(new byte[8]);
    SecretKey secretKey = factory.generateSecret(spec);
    ASN1Encodable apo = new APOptions();
    DesCbcCrc dcc = new DesCbcCrc(secretKey);
    byte[] encrypted = dcc.encrypt(apo);
    apo = dcc.decrypt(encrypted);
    System.out.println("Encrypt-decrypt test successful.");
}

From source file:Main.java

public static String deCrypto(String txt, String key) {
    SecretKeyFactory skeyFactory = null;
    Cipher cipher = null;/*from  www.j a  v  a  2s  .  c  o  m*/
    byte[] btxts = null;
    try {
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
        skeyFactory = SecretKeyFactory.getInstance("DES");
        cipher = Cipher.getInstance("DES");
        SecretKey deskey = skeyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.DECRYPT_MODE, deskey);
        btxts = new byte[txt.length() / 2];
        for (int i = 0, count = txt.length(); i < count; i += 2) {
            btxts[i / 2] = (byte) Integer.parseInt(txt.substring(i, i + 2), 16);
        }
        return (new String(cipher.doFinal(btxts)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

From source file:Main.java

public static String enCrypto(String txt, String key) {

    if (txt != null && !"".equals(txt) && !"null".equals(txt)) {

        try {/*from  w ww.  j  ava2 s .c om*/

            StringBuffer sb = new StringBuffer();
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
            SecretKeyFactory skeyFactory = null;
            Cipher cipher = null;
            try {
                skeyFactory = SecretKeyFactory.getInstance("DES");
                cipher = Cipher.getInstance("DES");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            SecretKey deskey = skeyFactory.generateSecret(desKeySpec);
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            byte[] cipherText = cipher.doFinal(txt.getBytes());
            for (int n = 0; n < cipherText.length; n++) {
                String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF));
                if (stmp.length() == 1) {
                    sb.append("0" + stmp);
                } else {
                    sb.append(stmp);
                }
            }
            return sb.toString().toUpperCase(Locale.US);

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
    return null;
}

From source file:Encrypt.java

private static String decrypt(String message) throws Exception {
    byte[] bytesrc = convertHexString(message);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    byte[] retByte = cipher.doFinal(bytesrc);
    return new String(retByte);
}

From source file:Main.java

public static Key getDESKey(byte[] key) throws Exception {
    DESKeySpec des = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    return keyFactory.generateSecret(des);
}