Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.jcajce.provider.symmetric;
  
  
 
 
 public final class DESede
 {
     private DESede()
     {
     }
 
     static public class ECB
         extends BaseBlockCipher
     {
         public ECB()
         {
             super(new DESedeEngine());
         }
     }
 
     static public class CBC
         extends BaseBlockCipher
     {
         public CBC()
         {
             super(new CBCBlockCipher(new DESedeEngine()), 64);
         }
     }

    
DESede CFB8
 
     public static class DESedeCFB8
         extends BaseMac
     {
         public DESedeCFB8()
         {
             super(new CFBBlockCipherMac(new DESedeEngine()));
         }
     }

    
DESede64
 
     public static class DESede64
         extends BaseMac
     {
         public DESede64()
         {
             super(new CBCBlockCipherMac(new DESedeEngine(), 64));
         }
     }

    
DESede64with7816-4Padding
 
     public static class DESede64with7816d4
         extends BaseMac
     {
         public DESede64with7816d4()
         {
             super(new CBCBlockCipherMac(new DESedeEngine(), 64, new ISO7816d4Padding()));
         }
     }
     
     public static class CBCMAC
         extends BaseMac
     {
         public CBCMAC()
         {
            super(new CBCBlockCipherMac(new DESedeEngine()));
        }
    }
    static public class CMAC
        extends BaseMac
    {
        public CMAC()
        {
            super(new CMac(new DESedeEngine()));
        }
    }
    public static class Wrap
        extends BaseWrapCipher
    {
        public Wrap()
        {
            super(new DESedeWrapEngine());
        }
    }
    public static class RFC3211
        extends BaseWrapCipher
    {
        public RFC3211()
        {
            super(new RFC3211WrapEngine(new DESedeEngine()), 8);
        }
    }

  
DESede - the default for this is to generate a key in a-b-a format that's 24 bytes long but has 16 bytes of key material (the first 8 bytes is repeated as the last 8 bytes). If you give it a size, you'll get just what you asked for.
    public static class KeyGenerator
        extends BaseKeyGenerator
    {
        private boolean     keySizeSet = false;
        public KeyGenerator()
        {
            super("DESede", 192, new DESedeKeyGenerator());
        }
        protected void engineInit(
            int             keySize,
            SecureRandom random)
        {
            super.engineInit(keySizerandom);
             = true;
        }
        protected SecretKey engineGenerateKey()
        {
            if ()
            {
                .init(new KeyGenerationParameters(new SecureRandom(), ));
                 = false;
            }
            //
            // if no key size has been defined generate a 24 byte key in
            // the a-b-a format
            //
            if (!)
            {
                byte[]     k = .generateKey();
                System.arraycopy(k, 0, k, 16, 8);
                return new SecretKeySpec(k);
            }
            else
            {
                return new SecretKeySpec(.generateKey(), );
            }
        }
    }

    
generate a desEDE key in the a-b-c format.
    public static class KeyGenerator3
        extends BaseKeyGenerator
    {
        public KeyGenerator3()
        {
            super("DESede3", 192, new DESedeKeyGenerator());
        }
    }

    
PBEWithSHAAnd3-KeyTripleDES-CBC
    static public class PBEWithSHAAndDES3Key
        extends BaseBlockCipher
    {
        public PBEWithSHAAndDES3Key()
        {
            super(new CBCBlockCipher(new DESedeEngine()));
        }
    }

    
PBEWithSHAAnd2-KeyTripleDES-CBC
    static public class PBEWithSHAAndDES2Key
        extends BaseBlockCipher
    {
        public PBEWithSHAAndDES2Key()
        {
            super(new CBCBlockCipher(new DESedeEngine()));
        }
    }

    
PBEWithSHAAnd3-KeyTripleDES-CBC
    static public class PBEWithSHAAndDES3KeyFactory
        extends DES.DESPBEKeyFactory
    {
        public PBEWithSHAAndDES3KeyFactory()
        {
            super("PBEwithSHAandDES3Key-CBC".true, 192, 64);
        }
    }

    
PBEWithSHAAnd2-KeyTripleDES-CBC
    static public class PBEWithSHAAndDES2KeyFactory
        extends DES.DESPBEKeyFactory
    {
        public PBEWithSHAAndDES2KeyFactory()
        {
            super("PBEwithSHAandDES2Key-CBC".true, 128, 64);
        }
    }
    public static class AlgParamGen
        extends BaseAlgorithmParameterGenerator
    {
        protected void engineInit(
            AlgorithmParameterSpec genParamSpec,
            SecureRandom            random)
            throws InvalidAlgorithmParameterException
        {
            throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for DES parameter generation.");
        }
        protected AlgorithmParameters engineGenerateParameters()
        {
            byte[]  iv = new byte[8];
            if ( == null)
            {
                 = new SecureRandom();
            }
            .nextBytes(iv);
            AlgorithmParameters params;
            try
            {
                params = createParametersInstance("DES");
                params.init(new IvParameterSpec(iv));
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.getMessage());
            }
            return params;
        }
    }
    static public class KeyFactory
        extends BaseSecretKeyFactory
    {
        public KeyFactory()
        {
            super("DESede"null);
        }
        protected KeySpec engineGetKeySpec(
            SecretKey key,
            Class keySpec)
        throws InvalidKeySpecException
        {
            if (keySpec == null)
            {
                throw new InvalidKeySpecException("keySpec parameter is null");
            }
            if (key == null)
            {
                throw new InvalidKeySpecException("key parameter is null");
            }
            if (SecretKeySpec.class.isAssignableFrom(keySpec))
            {
                return new SecretKeySpec(key.getEncoded(), );
            }
            else if (DESedeKeySpec.class.isAssignableFrom(keySpec))
            {
                byte[]  bytes = key.getEncoded();
                try
                {
                    if (bytes.length == 16)
                    {
                        byte[]  longKey = new byte[24];
                        System.arraycopy(bytes, 0, longKey, 0, 16);
                        System.arraycopy(bytes, 0, longKey, 16, 8);
                        return new DESedeKeySpec(longKey);
                    }
                    else
                    {
                        return new DESedeKeySpec(bytes);
                    }
                }
                catch (Exception e)
                {
                    throw new InvalidKeySpecException(e.toString());
                }
            }
            throw new InvalidKeySpecException("Invalid KeySpec");
        }
        protected SecretKey engineGenerateSecret(
            KeySpec keySpec)
        throws InvalidKeySpecException
        {
            if (keySpec instanceof DESedeKeySpec)
            {
                DESedeKeySpec desKeySpec = (DESedeKeySpec)keySpec;
                return new SecretKeySpec(desKeySpec.getKey(), "DESede");
            }
            return super.engineGenerateSecret(keySpec);
        }
    }
    public static class Mappings
        extends AlgorithmProvider
    {
        private static final String PREFIX = DESede.class.getName();
        private static final String PACKAGE = "org.bouncycastle.jcajce.provider.symmetric"// JDK 1.2
                
        public Mappings()
        {
        }
        public void configure(ConfigurableProvider provider)
        {
            provider.addAlgorithm("Cipher.DESEDE" + "$ECB");
            provider.addAlgorithm("Cipher." + . + "$CBC");
            provider.addAlgorithm("Cipher.DESEDEWRAP" + "$Wrap");
            provider.addAlgorithm("Cipher." + . + "$Wrap");
            provider.addAlgorithm("Cipher.DESEDERFC3211WRAP" + "$RFC3211");
            provider.addAlgorithm("Alg.Alias.Cipher.TDEA""DESEDE");
            provider.addAlgorithm("Alg.Alias.Cipher.TDEAWRAP""DESEDEWRAP");
            provider.addAlgorithm("Alg.Alias.KeyGenerator.TDEA""DESEDE");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.TDEA""DESEDE");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator.TDEA""DESEDE");
            provider.addAlgorithm("Alg.Alias.SecretKeyFactory.TDEA""DESEDE");
            if (provider.hasAlgorithm("MessageDigest""SHA-1"))
            {
                provider.addAlgorithm("Cipher.PBEWITHSHAAND3-KEYTRIPLEDES-CBC" + "$PBEWithSHAAndDES3Key");
                provider.addAlgorithm("Cipher.BROKENPBEWITHSHAAND3-KEYTRIPLEDES-CBC" + "$BrokePBEWithSHAAndDES3Key");
                provider.addAlgorithm("Cipher.OLDPBEWITHSHAAND3-KEYTRIPLEDES-CBC" + "$OldPBEWithSHAAndDES3Key");
                provider.addAlgorithm("Cipher.PBEWITHSHAAND2-KEYTRIPLEDES-CBC" + "$PBEWithSHAAndDES2Key");
                provider.addAlgorithm("Cipher.BROKENPBEWITHSHAAND2-KEYTRIPLEDES-CBC" + "$BrokePBEWithSHAAndDES2Key");
                provider.addAlgorithm("Alg.Alias.Cipher." + ."PBEWITHSHAAND3-KEYTRIPLEDES-CBC");
                provider.addAlgorithm("Alg.Alias.Cipher." + ."PBEWITHSHAAND2-KEYTRIPLEDES-CBC");
                provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1ANDDESEDE""PBEWITHSHAAND3-KEYTRIPLEDES-CBC");
                provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1AND3-KEYTRIPLEDES-CBC""PBEWITHSHAAND3-KEYTRIPLEDES-CBC");
                provider.addAlgorithm("Alg.Alias.Cipher.PBEWITHSHA1AND2-KEYTRIPLEDES-CBC""PBEWITHSHAAND2-KEYTRIPLEDES-CBC");
            }
            provider.addAlgorithm("KeyGenerator.DESEDE" + "$KeyGenerator");
            provider.addAlgorithm("KeyGenerator." + . + "$KeyGenerator3");
            provider.addAlgorithm("KeyGenerator.DESEDEWRAP" + "$KeyGenerator");
            provider.addAlgorithm("SecretKeyFactory.DESEDE" + "$KeyFactory");
            provider.addAlgorithm("Mac.DESEDECMAC" + "$CMAC");
            provider.addAlgorithm("Mac.DESEDEMAC" + "$CBCMAC");
            provider.addAlgorithm("Alg.Alias.Mac.DESEDE""DESEDEMAC");
            provider.addAlgorithm("Mac.DESEDEMAC/CFB8" + "$DESedeCFB8");
            provider.addAlgorithm("Alg.Alias.Mac.DESEDE/CFB8""DESEDEMAC/CFB8");
            provider.addAlgorithm("Mac.DESEDEMAC64" + "$DESede64");
            provider.addAlgorithm("Alg.Alias.Mac.DESEDE64""DESEDEMAC64");
            provider.addAlgorithm("Mac.DESEDEMAC64WITHISO7816-4PADDING" + "$DESede64with7816d4");
            provider.addAlgorithm("Alg.Alias.Mac.DESEDE64WITHISO7816-4PADDING""DESEDEMAC64WITHISO7816-4PADDING");
            provider.addAlgorithm("Alg.Alias.Mac.DESEDEISO9797ALG1MACWITHISO7816-4PADDING""DESEDEMAC64WITHISO7816-4PADDING");
            provider.addAlgorithm("Alg.Alias.Mac.DESEDEISO9797ALG1WITHISO7816-4PADDING""DESEDEMAC64WITHISO7816-4PADDING");
            provider.addAlgorithm("AlgorithmParameters.DESEDE" + ".util.IvAlgorithmParameters");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters." + ."DESEDE");
            provider.addAlgorithm("AlgorithmParameterGenerator.DESEDE",   + "$AlgParamGen");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameterGenerator." + ."DESEDE");
            provider.addAlgorithm("SecretKeyFactory.PBEWITHSHAAND3-KEYTRIPLEDES-CBC" + "$PBEWithSHAAndDES3KeyFactory");
            provider.addAlgorithm("SecretKeyFactory.PBEWITHSHAAND2-KEYTRIPLEDES-CBC" + "$PBEWithSHAAndDES2KeyFactory");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAAND3-KEYTRIPLEDES""PKCS12PBE");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAAND2-KEYTRIPLEDES""PKCS12PBE");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAAND3-KEYTRIPLEDES-CBC""PKCS12PBE");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAAND2-KEYTRIPLEDES-CBC""PKCS12PBE");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAANDDES3KEY-CBC""PKCS12PBE");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.PBEWITHSHAANDDES2KEY-CBC""PKCS12PBE");
            provider.addAlgorithm("Alg.Alias.SecretKeyFactory.1.2.840.113549.1.12.1.3""PBEWITHSHAAND3-KEYTRIPLEDES-CBC");
            provider.addAlgorithm("Alg.Alias.SecretKeyFactory.1.2.840.113549.1.12.1.4""PBEWITHSHAAND2-KEYTRIPLEDES-CBC");
            provider.addAlgorithm("Alg.Alias.SecretKeyFactory.PBEWithSHAAnd3KeyTripleDES""PBEWITHSHAAND3-KEYTRIPLEDES-CBC");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.1.2.840.113549.1.12.1.3""PKCS12PBE");
            provider.addAlgorithm("Alg.Alias.AlgorithmParameters.1.2.840.113549.1.12.1.4""PKCS12PBE");
            provider.addAlgorithm("Alg.Alias.Cipher.PBEWithSHAAnd3KeyTripleDES",  "PBEWITHSHAAND3-KEYTRIPLEDES-CBC");
        }
    }
New to GrepCode? Check out our FAQ X