Package gnu.javax.crypto.mode

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


   */
  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

   */
  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

Related Classes of gnu.javax.crypto.mode.IMode

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.