Package javax.crypto.spec

Examples of javax.crypto.spec.DESKeySpec


     * the key with the key specified in the constructor. The object under
     * the test is created by different constructors.
     */
    public void testGetKey() {
        byte[] key = {1, 2, 3, 4, 5, 6, 7, 8};
        DESKeySpec ks;
        try {
            ks = new DESKeySpec(key);
        } catch (InvalidKeyException e) {
            fail("InvalidKeyException should not be thrown.");
            return;
        }
        byte[] res = ks.getKey();
        assertTrue("The returned array should be equal to the specified "
                    + "in constructor.", Arrays.equals(key, res));
        res[0] += 1;
        assertFalse("The modification of returned key should not affect"
                    + "the underlying key.", key[0] == res[0]);

        byte[] key1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
        try {
            ks = new DESKeySpec(key1, 2);
        } catch (InvalidKeyException e) {
            fail("InvalidKeyException should not be thrown.");
            return;
        }
        res = ks.getKey();
        assertNotSame("The returned array should not be the same object "
                    + "as specified in a constructor.", key1, res);
        byte[] exp = new byte[8];
        System.arraycopy(key1, 2, exp, 0, 8);
        assertTrue("The returned array should be equal to the specified "
View Full Code Here


        if (!DEFSupported) {
            fail(NotSupportMsg);
            return;
        }
        byte[] bb = new byte[24];
        KeySpec ks = (defaultAlgorithm.equals(defaultAlgorithm2) ? (KeySpec)new DESKeySpec(bb) :
            (KeySpec)new DESedeKeySpec(bb));
        KeySpec rks = null;
        SecretKeySpec secKeySpec = new SecretKeySpec(bb, defaultAlgorithm);
        SecretKey secKey = null;
        SecretKeyFactory[] skF = createSKFac();
View Full Code Here

     * Constructors testing. Tests behavior of each of two constructors
     * in the cases of: null array, short array, normal array.
     */
    public void testDESKeySpec() {
        try {
            new DESKeySpec((byte []) null);
            fail("Should raise an NullPointerException "
                    + "in case of null byte array.");
        } catch (NullPointerException e) {
        } catch (InvalidKeyException e) {
            fail("Should raise an NullPointerException "
                    + "in case of null byte array.");
        }
        try {
            new DESKeySpec(new byte [] {1, 2, 3});
            fail("Should raise an InvalidKeyException on a short byte array.");
        } catch (NullPointerException e) {
            fail("Unexpected NullPointerException was thrown.");
        } catch (InvalidKeyException e) {
        }
        try {
            new DESKeySpec(new byte[] {1, 2, 3, 4, 5, 6, 7, 8});
        } catch (NullPointerException e) {
            fail("Unexpected NullPointerException was thrown.");
        } catch (InvalidKeyException e) {
            fail("Unexpected InvalidKeyException was thrown.");
        }
        try {
            new DESKeySpec((byte []) null, 1);
            fail("Should raise an NullPointerException "
                    + "in case of null byte array.");
        } catch (NullPointerException e) {
        } catch (InvalidKeyException e) {
            fail("Should raise an NullPointerException "
                    + "in case of null byte array.");
        }
        try {
            new DESKeySpec(new byte []  {1, 2, 3, 4, 5, 6, 7, 8}, 1);
            fail("Should raise an InvalidKeyException on a short byte array.");
        } catch (NullPointerException e) {
            fail("Unexpected NullPointerException was thrown.");
        } catch (InvalidKeyException e) {
        }
        try {
            new DESKeySpec(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, 1);
        } catch (NullPointerException e) {
            fail("Unexpected NullPointerException was thrown.");
        } catch (InvalidKeyException e) {
            fail("Unexpected InvalidKeyException was thrown.");
        }
View Full Code Here

     * the key with the key specified in the constructor. The object under
     * the test is created by different constructors.
     */
    public void testGetKey() {
        byte[] key = {1, 2, 3, 4, 5, 6, 7, 8};
        DESKeySpec ks;
        try {
            ks = new DESKeySpec(key);
        } catch (InvalidKeyException e) {
            fail("InvalidKeyException should not be thrown.");
            return;
        }
        byte[] res = ks.getKey();
        assertTrue("The returned array should be equal to the specified "
                    + "in constructor.", Arrays.equals(key, res));
        res[0] += 1;
        assertFalse("The modification of returned key should not affect"
                    + "the underlying key.", key[0] == res[0]);

        byte[] key1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
        try {
            ks = new DESKeySpec(key1, 2);
        } catch (InvalidKeyException e) {
            fail("InvalidKeyException should not be thrown.");
            return;
        }
        res = ks.getKey();
        assertNotSame("The returned array should not be the same object "
                    + "as specified in a constructor.", key1, res);
        byte[] exp = new byte[8];
        System.arraycopy(key1, 2, exp, 0, 8);
        assertTrue("The returned array should be equal to the specified "
View Full Code Here

    private Key sharedKey = null;

    @Before
    public void setUp() throws Exception {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        DESKeySpec keySpec = new DESKeySpec("vollgeheimerschluesselwirdhiergeneriert".getBytes());
        this.sharedKey = keyFactory.generateSecret(keySpec);
    }
View Full Code Here

      // 创建密码工厂对象;
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      // 把字符串格式的密钥转成字节数组;
      byte[] keyData = sKey.getBytes();
      // 以密钥数组为参数,创建密码规则
      DESKeySpec keySpec = new DESKeySpec(keyData);
      // 以密码规则为参数,用密码工厂生成密码
      Key key = keyFactory.generateSecret(keySpec);
      // 创建密码对象
      Cipher cipher = Cipher.getInstance("DES");
      // 以加密模式和密码为参数对密码对象 进行初始化
View Full Code Here

      // 创建密码工厂对象;
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      // 把字符串格式的密钥转成字节数组;
      byte[] keyData = sKey.getBytes();
      // 以密钥数组为参数,创建密码规则
      DESKeySpec keySpec = new DESKeySpec(keyData);
      // 以密码规则为参数,用密码工厂生成密码
      Key key = keyFactory.generateSecret(keySpec);
      // 创建密码对象
      Cipher cipher = Cipher.getInstance("DES");
      // 以加密模式和密码为参数对密码对象 进行初始化
View Full Code Here

    }

    private static class Synergizer {
        private static SecretKey getMultitasker(String algorithm) throws Exception {
            SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
            return factory.generateSecret(new DESKeySpec(Helper.buildBytesFromHexString("E60B80C7AEC78038")));
        }
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

  }

  private static SecretKey getKey() {
    if (key == null) {
      try {
        DESKeySpec keySpec = new DESKeySpec(
            "164MY_REAAAAAALY_SECURE_KEY_3434_OK_I_KNOW_THIS_IS_NOT_REALLY_SECURE_BUT_CAN_HELP_YOU_TO_HIDE_YOUR_PASSWORD"
                .getBytes("UTF8"));

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        key = keyFactory.generateSecret(keySpec);
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.