Invariant Properties

  • rss
  • Home

Getting A List of Available Cryptographic Algorithms

Bear Giles | August 3, 2014

How do you learn what cryptographic algorithms are available to you? The Java spec names several required ciphers, digests, etc., but a provider often offers more than that.

Fortunately this is easy to learn what’s available on our system.

  1. public class ListAlgorithms {
  2.     public static void main(String[] args) {
  3.         // Security.addProvider(new
  4.         // org.bouncycastle.jce.provider.BouncyCastleProvider());
  5.  
  6.         // get a list of services and their respective providers.
  7.         final Map<String, List<Provider>> services = new TreeMap<>();
  8.  
  9.         for (Provider provider : Security.getProviders()) {
  10.             for (Provider.Service service : provider.getServices()) {
  11.                 if (services.containsKey(service.getType())) {
  12.                     final List<Provider> providers = services.get(service
  13.                             .getType());
  14.                     if (!providers.contains(provider)) {
  15.                         providers.add(provider);
  16.                     }
  17.                 } else {
  18.                     final List<Provider> providers = new ArrayList<>();
  19.                     providers.add(provider);
  20.                     services.put(service.getType(), providers);
  21.                 }
  22.             }
  23.         }
  24.  
  25.         // now get a list of algorithms and their respective providers
  26.         for (String type : services.keySet()) {
  27.             final Map<String, List<Provider>> algs = new TreeMap<>();
  28.             for (Provider provider : Security.getProviders()) {
  29.                 for (Provider.Service service : provider.getServices()) {
  30.                     if (service.getType().equals(type)) {
  31.                         final String algorithm = service.getAlgorithm();
  32.                         if (algs.containsKey(algorithm)) {
  33.                             final List<Provider> providers = algs
  34.                                     .get(algorithm);
  35.                             if (!providers.contains(provider)) {
  36.                                 providers.add(provider);
  37.                             }
  38.                         } else {
  39.                             final List<Provider> providers = new ArrayList<>();
  40.                             providers.add(provider);
  41.                             algs.put(algorithm, providers);
  42.                         }
  43.                     }
  44.                 }
  45.             }
  46.  
  47.             // write the results to standard out.
  48.             System.out.printf("%20s : %s\n", "", type);
  49.             for (String algorithm : algs.keySet()) {
  50.                 System.out.printf("%-20s : %s\n", algorithm,
  51.                         Arrays.toString(algs.get(algorithm).toArray()));
  52.             }
  53.             System.out.println();
  54.         }
  55.     }
  56. }
public class ListAlgorithms {
    public static void main(String[] args) {
        // Security.addProvider(new
        // org.bouncycastle.jce.provider.BouncyCastleProvider());

        // get a list of services and their respective providers.
        final Map<String, List<Provider>> services = new TreeMap<>();

        for (Provider provider : Security.getProviders()) {
            for (Provider.Service service : provider.getServices()) {
                if (services.containsKey(service.getType())) {
                    final List<Provider> providers = services.get(service
                            .getType());
                    if (!providers.contains(provider)) {
                        providers.add(provider);
                    }
                } else {
                    final List<Provider> providers = new ArrayList<>();
                    providers.add(provider);
                    services.put(service.getType(), providers);
                }
            }
        }

        // now get a list of algorithms and their respective providers
        for (String type : services.keySet()) {
            final Map<String, List<Provider>> algs = new TreeMap<>();
            for (Provider provider : Security.getProviders()) {
                for (Provider.Service service : provider.getServices()) {
                    if (service.getType().equals(type)) {
                        final String algorithm = service.getAlgorithm();
                        if (algs.containsKey(algorithm)) {
                            final List<Provider> providers = algs
                                    .get(algorithm);
                            if (!providers.contains(provider)) {
                                providers.add(provider);
                            }
                        } else {
                            final List<Provider> providers = new ArrayList<>();
                            providers.add(provider);
                            algs.put(algorithm, providers);
                        }
                    }
                }
            }

            // write the results to standard out.
            System.out.printf("%20s : %s\n", "", type);
            for (String algorithm : algs.keySet()) {
                System.out.printf("%-20s : %s\n", algorithm,
                        Arrays.toString(algs.get(algorithm).toArray()));
            }
            System.out.println();
        }
    }
}

The system administrator can override the standard crypto libraries. In practice it’s safest to always load your own crypto library and either register it manually, as above, or better yet pass it as an optional parameter when creating new objects.

Algorithms

There are a few dozen standard algorithms. The ones we’re most likely to be interested in are:

Symmetric Cipher

  • KeyGenerator – creates symmetric key
  • SecretKeyFactor – converts between symmetric keys and raw bytes
  • Cipher – encryption cipher
  • AlgorithmParameters – algorithm parameters
  • AlgorithmParameterGernerator – algorithm parameters

Asymmetric Cipher

  • KeyPairGenerator – creates public/private keys
  • KeyFactor – converts between keypairs and raw bytes
  • Cipher – encryption cipher
  • Signature – digital signatures
  • AlgorithmParameters – algorithm parameters
  • AlgorithmParameterGernerator – algorithm parameters

Digests

  • MessageDigest – digest (MD5, SHA1, etc.)
  • Mac – HMAC. Like a message digest but requires an encryption key as well so it can’t be forged by attacker

Certificates and KeyStores

  • KeyStore – JKS, PKCS, etc.
  • CertStore – like keystore but only stores certs.
  • CertificateFactory – converts between digital certificates and raw bytes.

It is critical to remember that most algorithms are provided for backward compatibility and should not be used for in greenfield development. As I write this the generally accepted advice is:

  • Use a variant of AES. Only use AES-ECB if you know with absolute certainty that you will never encrypt more than one blocksize (16 bytes) of data.
  • Always use a good random IV even if you’re using AES-CBC. Do not use the same IV or an easily predicted one.
  • Do not use less than 2048 bits in an asymmetric key.
  • Use SHA-256 or better. MD-5 is considered broken, SHA-1 will be considered broken in the near future.
  • Use PBKDF2WithHmacSHA1 to create AES key from passwords/passphrases. (See also Creating Password-Based Encryption Keys.)

Some people might want to use one of the other AES-candidate ciphers (e.g., twofish). These ciphers are probably safe but you might run into problems if you’re sharing files with other parties since they’re not in the required cipher suite.

Beware US Export Restrictions

Finally it’s important to remember that the standard Java distribution is crippled due to US export restrictions. You can get full functionality by installing a standard US-only file on your system but it’s hard if not impossible for developers to verify this has been done. In practice many if not most people use a third-party cryptographic library like BouncyCastle. Many inexperienced developers forget about this and unintentionally use crippled functionality.

Comments
No Comments »
Categories
java, security
Comments rss Comments rss
Trackback Trackback

Archives

  • May 2020 (1)
  • March 2019 (1)
  • August 2018 (1)
  • May 2018 (1)
  • February 2018 (1)
  • November 2017 (4)
  • January 2017 (3)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (2)
  • March 2016 (1)
  • February 2016 (3)
  • January 2016 (6)
  • December 2015 (2)
  • November 2015 (3)
  • October 2015 (2)
  • August 2015 (4)
  • July 2015 (2)
  • June 2015 (2)
  • January 2015 (1)
  • December 2014 (6)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • July 2014 (1)
  • June 2014 (2)
  • May 2014 (2)
  • April 2014 (1)
  • March 2014 (1)
  • February 2014 (3)
  • January 2014 (6)
  • December 2013 (13)
  • November 2013 (6)
  • October 2013 (3)
  • September 2013 (2)
  • August 2013 (5)
  • June 2013 (1)
  • May 2013 (2)
  • March 2013 (1)
  • November 2012 (1)
  • October 2012 (3)
  • September 2012 (2)
  • May 2012 (6)
  • January 2012 (2)
  • December 2011 (12)
  • July 2011 (1)
  • June 2011 (2)
  • May 2011 (5)
  • April 2011 (6)
  • March 2011 (4)
  • February 2011 (3)
  • October 2010 (6)
  • September 2010 (8)

Recent Posts

  • 8-bit Breadboard Computer: Good Encapsulation!
  • Where are all the posts?
  • Better Ad Blocking Through Pi-Hole and Local Caching
  • The difference between APIs and SPIs
  • Hadoop: User Impersonation with Kerberos Authentication

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Pages

  • About Me
  • Notebook: Common XML Tasks
  • Notebook: Database/Webapp Security
  • Notebook: Development Tips

Syndication

Java Code Geeks

Know Your Rights

Support Bloggers' Rights
Demand Your dotRIGHTS

Security

  • Dark Reading
  • Krebs On Security Krebs On Security
  • Naked Security Naked Security
  • Schneier on Security Schneier on Security
  • TaoSecurity TaoSecurity

Politics

  • ACLU ACLU
  • EFF EFF

News

  • Ars technica Ars technica
  • Kevin Drum at Mother Jones Kevin Drum at Mother Jones
  • Raw Story Raw Story
  • Tech Dirt Tech Dirt
  • Vice Vice

Spam Blocked

53,314 spam blocked by Akismet
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox