Examples of IMode


Examples of gnu.crypto.mode.IMode

      BASE64Decoder base64 = new BASE64Decoder();
      byte[] byHash = base64.decodeBuffer(strHash);
      byte[] byKey = getVersionPassword(hci.cVersion, hci.cSubVersion);
      byte[] byIV = getIV(hci.cVersion, hci.cSubVersion);
     
      IMode mode = ModeFactory.getInstance("CBC", "AES", 32);
      Map attributes = new HashMap();
      attributes.put(IMode.KEY_MATERIAL, byKey);
      attributes.put(IMode.CIPHER_BLOCK_SIZE, new Integer(32));
     
      attributes.put(IMode.STATE, new Integer(IMode.DECRYPTION));
      attributes.put(IMode.IV, byIV);
     
      mode.init(attributes);
      int bs = mode.currentBlockSize();
     
      byte[] byH;     
      if ((byHash.length % 256) == 0)
        byH = new byte[byHash.length];
      else
        byH = new byte[byHash.length + (256 - (byHash.length % 256))];
      for (int i = 0; i < byHash.length; ++i)
        byH[i] = byHash[i];
     
      for (int i = 0; i + bs < byH.length; i += bs)
        mode.update(byH, i, byOut, i);
     
      return byHash.length;
    } catch (Throwable e) {};
    return 0;
  }
View Full Code Here

Examples of gnu.crypto.mode.IMode

    byte[] ct = null;

    try {
      IPad padding = PadFactory.getInstance("PKCS7");
      padding.init(16);
      IMode mode = ModeFactory.getInstance("CBC", "AES", 16);
      Map attributes = new HashMap();
      byte[] pt1 = source;
      byte[] pad = padding.pad(pt1, 0, pt1.length);
      byte[] pt = null;
      // 判断是否要补空
      if (pad.length == 16) {
        pt = new byte[pt1.length];
        System.arraycopy(pt1, 0, pt, 0, pt1.length);
      } else {
        pt = new byte[pt1.length + pad.length];
        System.arraycopy(pt1, 0, pt, 0, pt1.length);
        System.arraycopy(pad, 0, pt, pt1.length, pad.length);
      }

      ct = new byte[pt.length];

      byte[] iv = i_iv;
      byte[] key = mykey;
      attributes.put(IMode.KEY_MATERIAL, key);
      attributes.put(IMode.CIPHER_BLOCK_SIZE, new Integer(16));
      attributes.put(IMode.IV, iv);
      attributes.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
      mode.init(attributes);

      for (int i = 0; i + 16 <= pt.length; i += 16) {
        mode.update(pt, i, ct, i);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
View Full Code Here

Examples of gnu.crypto.mode.IMode

    byte[] out = null;
    try {
      IPad padding = PadFactory.getInstance("PKCS7");
      padding.init(16);
      IMode mode = ModeFactory.getInstance("CBC", "AES", 16);
      Map attributes = new HashMap();

      byte[] iv = i_iv;
      byte[] ct = new byte[source.length];
      byte[] key = mykey;

      attributes.put(IMode.KEY_MATERIAL, key);
      attributes.put(IMode.CIPHER_BLOCK_SIZE, new Integer(16));
      attributes.put(IMode.IV, iv);
      attributes.put(IMode.STATE, new Integer(IMode.DECRYPTION));
      mode.init(attributes);

      for (int i = 0; i + 16 <= source.length; i += 16) {
        mode.update(source, i, ct, i);
      }

      try {
        int unpad = padding.unpad(ct, 0, ct.length);
        out = new byte[ct.length - unpad];
View Full Code Here

Examples of gnu.javax.crypto.mode.IMode

  public void test(TestHarness harness)
  {
    harness.checkPoint("TestOfModeFactory");
    String mode, cipher;
    int bs;
    IMode algorithm;
    for (Iterator mit = ModeFactory.getNames().iterator(); mit.hasNext();)
      {
        mode = (String) mit.next();
        for (Iterator cit = CipherFactory.getNames().iterator(); cit.hasNext();)
          {
View Full Code Here

Examples of gnu.javax.crypto.mode.IMode

   */
  public void testEquality(TestHarness harness)
  {
    harness.checkPoint("testEquality");
    String cipherName = null, modeName;
    IMode gnu = null;
    Cipher jce = null;
    HashMap attrib = new HashMap();
    byte[] pt = null;
    //      byte[] iv = null;
    byte[] ct1 = null, ct2 = null;
    byte[] cpt1 = null, cpt2 = null;
    Iterator ci, mi;
    int bs;
    try
      {
        for (ci = CipherFactory.getNames().iterator(); ci.hasNext();)
          {
            cipherName = (String) ci.next();
            IBlockCipher cipher = CipherFactory.getInstance(cipherName);
            bs = cipher.defaultBlockSize();
            for (mi = ModeFactory.getNames().iterator(); mi.hasNext();)
              {
                modeName = (String) mi.next();
                gnu = ModeFactory.getInstance(modeName, cipher, bs);
                jce = Cipher.getInstance(cipherName + "/" + modeName
                                         + "/NoPadding", Registry.GNU_CRYPTO);
                pt = new byte[bs];
                for (int i = 0; i < bs; i++)
                  {
                    pt[i] = (byte) i;
                  }
                attrib.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(bs));
                attrib.put(IMode.IV, pt);
                for (Iterator ks = cipher.keySizes(); ks.hasNext();)
                  {
                    byte[] kb = new byte[((Integer) ks.next()).intValue()];
                    for (int i = 0; i < kb.length; i++)
                      {
                        kb[i] = (byte) i;
                      }
                    attrib.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
                    attrib.put(IBlockCipher.KEY_MATERIAL, kb);
                    gnu.reset();
                    gnu.init(attrib);
                    ct1 = new byte[bs];
                    gnu.update(pt, 0, ct1, 0);
                    jce.init(Cipher.ENCRYPT_MODE,
                             new SecretKeySpec(kb, cipherName),
                             new IvParameterSpec(pt));
                    ct2 = new byte[bs];
                    jce.doFinal(pt, 0, bs, ct2, 0);
                    harness.check(Arrays.equals(ct1, ct2), "testEquality("
                                                           + cipherName + ")");

                    attrib.put(IMode.STATE, new Integer(IMode.DECRYPTION));
                    cpt1 = new byte[bs];
                    gnu.reset();
                    gnu.init(attrib);
                    gnu.update(ct1, 0, cpt1, 0);
                    harness.check(Arrays.equals(pt, cpt1), "testEquality("
                                                           + cipherName + ")");

                    jce.init(Cipher.DECRYPT_MODE,
                             new SecretKeySpec(kb, cipherName),
View Full Code Here

Examples of gnu.javax.crypto.mode.IMode

   */
  public void testPadding(TestHarness harness)
  {
    harness.checkPoint("testPadding");
    String padName = null;
    IMode gnu = ModeFactory.getInstance("ECB", "AES", 16);
    IPad pad;
    Cipher jce;
    byte[] kb = new byte[32];
    for (int i = 0; i < kb.length; i++)
      kb[i] = (byte) i;
    byte[] pt = new byte[42];
    for (int i = 0; i < pt.length; i++)
      pt[i] = (byte) i;
    byte[] ppt = new byte[48]; // padded plaintext.
    System.arraycopy(pt, 0, ppt, 0, 42);
    byte[] ct1 = new byte[48], ct2 = new byte[48];
    byte[] cpt1 = new byte[42], cpt2 = new byte[42];
    HashMap attrib = new HashMap();
    attrib.put(IBlockCipher.KEY_MATERIAL, kb);
    try
      {
        for (Iterator it = PadFactory.getNames().iterator(); it.hasNext();)
          {
            padName = (String) it.next();
            // skip EME-PKCS1-V1.5 padding since it's not a true block cipher
            // padding algorithm
            if (padName.equalsIgnoreCase(Registry.EME_PKCS1_V1_5_PAD))
              continue;

            pad = PadFactory.getInstance(padName);
            pad.reset();
            pad.init(16);

            byte[] padding = pad.pad(pt, 0, pt.length);
            System.arraycopy(padding, 0, ppt, 42, padding.length);
            attrib.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
            gnu.reset();
            gnu.init(attrib);
            for (int i = 0; i < ppt.length; i += 16)
              gnu.update(ppt, i, ct1, i);

            jce = Cipher.getInstance("AES/ECB/" + padName, Registry.GNU_CRYPTO);
            jce.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kb, "AES"));
            jce.doFinal(ct1, 0, ct1.length, cpt1, 0);
            harness.check(Arrays.equals(pt, cpt1),
                          "testPadding(" + padName + ")");

            jce.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kb, "AES"));
            jce.doFinal(pt, 0, pt.length, ct2, 0);

            attrib.put(IMode.STATE, new Integer(IMode.DECRYPTION));
            gnu.reset();
            gnu.init(attrib);
            byte[] pcpt = new byte[48];
            for (int i = 0; i < ct2.length; i += 16)
              gnu.update(ct2, i, pcpt, i);

            int trim = pad.unpad(pcpt, 0, pcpt.length);
            System.arraycopy(pcpt, 0, cpt2, 0, pcpt.length - trim);

            harness.check(Arrays.equals(pt, cpt2),
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.