Java Symmetric


Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Java Symmetric

  1. 1. GS: Chapter 4 Symmetric Encryption in Java
  2. 2. Topics <ul><li>Blowfish </li></ul><ul><li>Password-based encryption (PBE) </li></ul><ul><li>Key storage </li></ul><ul><li>Modes </li></ul><ul><li>Cipher streams and IV (initialization vector) </li></ul><ul><li>Sealed objects </li></ul>
  3. 3. Applications of symmetric encryptions <ul><li>File encryption </li></ul><ul><li>Network encryption </li></ul><ul><li>Database encryption </li></ul><ul><li>Applications that require encryption of large amount of data. </li></ul>
  4. 4. Javax.crypto. KeyGenerator <ul><li> </li></ul><ul><ul><li>Provides the functionality of a (symmetric) key generator </li></ul></ul><ul><ul><li>Key generators are constructed using one of the getInstance class methods. </li></ul></ul><ul><ul><li>KeyGenerator objects are reusable, i.e., after a key has been generated, the same KeyGenerator object can be re-used to generate further keys. </li></ul></ul><ul><ul><li>There are two ways to generate a key: in an algorithm-independent manner, and in an algorithm-specific manner. The only difference between the two is the initialization of the object. </li></ul></ul>
  5. 5. Javax.crypto. KeyGenerator <ul><li>Using KeyGenerator </li></ul><ul><li>Create a new key generator: </li></ul><ul><ul><li>KeyGenerator keyGenerator = KeyGenerator.getInstance (“DESede”); </li></ul></ul><ul><ul><li>Note: DESede is a triple DES variant with three DES keys k1, k2, k3. The message is encrypted with k1 first, then decrypted with k2, and finally encrypted again with k3. This increases the key space and prevents brute force attacks. </li></ul></ul><ul><li>Initialize the key generator with the size of the key: </li></ul><ul><ul><li>keyGenerator.init (168); // initialized to 168 bits </li></ul></ul><ul><li>Generate the key object: </li></ul><ul><ul><li>Key myKey = keyGenerator.generateKey ( ); </li></ul></ul>
  6. 6. Key <ul><li> </li></ul><ul><li> Interface Key </li></ul><ul><li>All Superinterfaces: </li></ul><ul><ul><li>Serializable </li></ul></ul><ul><li>All Known Subinterfaces: </li></ul><ul><ul><li>DHPrivateKey , DHPublicKey , DSAPrivateKey , DSAPublicKey , PBEKey , PrivateKey , PublicKey , RSAMultiPrimePrivateCrtKey , RSAPrivateCrtKey , RSAPrivateKey , RSAPublicKey , SecretKey </li></ul></ul><ul><li>All Known Implementing Classes: </li></ul><ul><ul><li>KerberosKey , SecretKeySpec </li></ul></ul>
  7. 7. Key <ul><li>The Key interface is the top-level interface for all keys. It defines the functionality shared by all key objects. </li></ul><ul><li>All keys have three characteristics: </li></ul><ul><ul><li>The key algorithm for that key; </li></ul></ul><ul><ul><li>An external encoded form for the key used when a standard representation of the key is needed outside the Java Virtual Machine, as when transmitting the key to some other party; </li></ul></ul><ul><ul><li>The name of the format of the encoded key </li></ul></ul><ul><li>Keys are generally obtained through key generators, key factory, certificates, or various Identity classes used to manage keys. </li></ul><ul><li>Examples: javax.crypto.KeyGenerator( ) ; ) ; </li></ul>
  8. 8. Javax.crypto. Cipher <ul><li> </li></ul><ul><ul><li>public class Cipher </li></ul></ul><ul><li>extends Object </li></ul><ul><li>This class provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework. </li></ul><ul><li>To use a Cipher: getInstance( ), init( ), update( ), doFinal( ). </li></ul>
  9. 9. Javax.crypto. Cipher. getInstance( ) <ul><li>In order to create a Cipher object, the application calls the Cipher's getInstance method, and passes the name of the requested transformation to it. </li></ul><ul><ul><li>static  Cipher getInstance ( String  transformation) Generates a Cipher object that implements the specified transformation. </li></ul></ul><ul><ul><li>static  Cipher getInstance ( String  transformation, Provider  provider) Creates a Cipher object that implements the specified transformation, as supplied by the specified provider. </li></ul></ul><ul><ul><li>static  Cipher getInstance ( String  transformation, String  provider) Creates a Cipher object that implements the specified transformation, as supplied by the specified provider. </li></ul></ul>
  10. 10. Javax.crypto. Cipher. getInstance( ) <ul><li>Examples: </li></ul><ul><ul><li>Cipher cipher = Cipher.getInstance(&quot; DES/CBC/PKCS5Padding &quot;); </li></ul></ul><ul><ul><li>Cipher cipher = Cipher.getInstance(“DESede/ECB/PKCS5Padding”); </li></ul></ul>
  11. 11. Javax.crypto. Cipher. init( ) <ul><li>Initialize an instance of Cipher: </li></ul><ul><ul><li>Declares the operating mode (ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE, UNWRAP_MODE) </li></ul></ul><ul><ul><li>Pass a key ( to the cipher </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>Cipher.init (Cipher.ENCRYPT_MODE, myKey); </li></ul></ul></ul><ul><ul><ul><li>Note: When a Cipher object is initialized, it loses all previously-acquired state. In other words, initializing a Cipher is equivalent to creating a new instance of that Cipher and initializing it. </li></ul></ul></ul>
  12. 12. Javax.crypto. Cipher. update( ) <ul><li>Pass the information to be encrypted/decrypted to the cipher: </li></ul><ul><ul><li>The information must be in the form of a byte array. </li></ul></ul><ul><ul><li>Note: Ciphers typically buffer their output. If the buffer has not been filled, null will be returned. </li></ul></ul><ul><ul><li>Alternative update( ) methods: </li></ul></ul><ul><ul><li>byte[ ] update (byte[] input) </li></ul></ul><ul><ul><ul><li>byte[ ] plaintext = myString.getBytes (“UTF8”); </li></ul></ul></ul><ul><ul><ul><li>byte[ ] ciphertext = cipher.update (plaintext); </li></ul></ul></ul><ul><ul><li>int update (byte[ ] input, int inputOffset, int inputLen, byte[ ] output, int outputOffset) </li></ul></ul><ul><ul><li>Continues a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part. </li></ul></ul>
  13. 13. Javax.crypto. Cipher. doFinal( ) <ul><li>Finish the operation: </li></ul><ul><ul><li>  byte[ ] doFinal ( ) Finishes a multiple-part encryption or decryption operation, depending on how this cipher was initialized. </li></ul></ul><ul><ul><li>  byte[ ] doFinal (byte[] input) Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation. </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><li>Byte[ ] ciphertext = cipher.doFinal ( ); </li></ul></ul>
  14. 14. <ul><li>P.69: (see proJavaSecurityCode .html ) </li></ul><ul><li>Sample output: </li></ul><ul><li>> java SimpleExample &quot;How are you doing?&quot; </li></ul><ul><ul><li>Plain Message=How are you doing? </li></ul></ul><ul><ul><li>Generating a TripleDES key... </li></ul></ul><ul><ul><li>Done generating the key. </li></ul></ul><ul><ul><li>Now encrypting the message </li></ul></ul><ul><ul><li>Message Encrypted </li></ul></ul><ul><ul><li>Ciphertext=-74-45759-44-115-19-8-56-99-47794393-45-107-41-125-127-233271855 </li></ul></ul><ul><ul><li>Now decrypting the message </li></ul></ul><ul><ul><li>Message decrypted </li></ul></ul><ul><ul><li>Decrypted text: How are you doing? </li></ul></ul>
  15. 15. <ul><li>Blowfish keys can be any bit size from 8 to 448, as long as the number if divisible by 8. </li></ul><ul><li>p.69: (see proJavaSecurityCode .html ) </li></ul><ul><li>Sample output: </li></ul><ul><li>> java BlowfishExample &quot;It's a wonderful day!&quot; </li></ul><ul><ul><li>Generating a Blowfish key... </li></ul></ul><ul><ul><li>Done generating the key. </li></ul></ul><ul><ul><li>Plaintext: </li></ul></ul><ul><ul><li>73 116 39 115 32 97 32 119 111 110 100 101 114 102 117 108 32 100 97 121 33 </li></ul></ul><ul><ul><li>Ciphertext: </li></ul></ul><ul><ul><li>-77 56 -88 61 -52 -12 -57 43 -10 66 -54 -98 -86 56 -86 51 -127 -125 30 48 -64 11 </li></ul></ul><ul><ul><li>2 -37 -125 </li></ul></ul><ul><ul><li>Decrypted text: It's a wonderful day! </li></ul></ul>
  16. 16. Password-based encryption (PBE) <ul><li>hashing + symmetric encryption </li></ul><ul><ul><li>The user-provided password is hashed by a message digest algorithm , such as SHA. </li></ul></ul><ul><ul><li>The hash value is then used to construct a key for a symmetric encryption algorithm , such as Blowfish. </li></ul></ul><ul><ul><li>The plaintext is then encrypted by the symmetric encryption algorithm. </li></ul></ul><ul><li>Problems? </li></ul><ul><ul><li>PBE is usually less secure, due to its smaller key space. </li></ul></ul><ul><ul><li>Passwords may suffer ‘dictionary attack’. </li></ul></ul><ul><ul><li>Two people might choose the same password, which would create two identical entries in the password file. </li></ul></ul>
  17. 17. Password-based encryption (PBE) <ul><li>PBE + salt + iteration count </li></ul><ul><ul><li>A salt is a randomly generated piece of data, say 64 bits, that is added to each password. </li></ul></ul><ul><ul><li>The combined salt+password is used to generate the key. </li></ul></ul><ul><ul><li>The key is then used to generate a symmetric cipher. </li></ul></ul><ul><ul><li>For the purpose of decryption, the salt must be stored as part of the ciphertext. </li></ul></ul><ul><ul><li>See figures on page 74. </li></ul></ul>
  18. 18. Password-based encryption (PBE)
  19. 19. Base64 Encoding <ul><li>Effective in representing ASCII data as 6-bit characters (save one bit per character) </li></ul><ul><li>Widely used in networking transmissions of data; e.g., in MIME emails & other Internet-related applications </li></ul><ul><li>Input: N bytes </li></ul><ul><li>Number of output characters </li></ul><ul><ul><li>(N * 8 / 24) * 4, if N*8 % 24 is zero; </li></ul></ul><ul><ul><li>(N * 8 / 24 + 1) * 4, otherwise. </li></ul></ul><ul><ul><li>Example: N = 8 bytes. </li></ul></ul><ul><ul><li>(64 / 24 + 1) * 4  12 characters </li></ul></ul><ul><li>See , RFC2045, and Appendix C. </li></ul>
  20. 20. Password-based encryption (PBE)
  21. 21. Password-based encryption (PBE) <ul><li>Random.nextBytes (byte[ ] bytes) Generates random bytes and places them into a user-supplied byte array. </li></ul><ul><li>public class PBEKeySpec </li></ul><ul><li>extends Object </li></ul><ul><li>implements KeySpec </li></ul><ul><li>A user-chosen password that can be used with password-based encryption (PBE). </li></ul><ul><li>The password can be viewed as some kind of raw key material, from which the encryption mechanism that uses it derives a cryptographic key. </li></ul>
  22. 22. Password-based encryption (PBE) <ul><li>public class SecretKeyFactory extends Object </li></ul><ul><li>This class represents a factory for secret keys. </li></ul><ul><li>Key factories are used to convert keys (opaque cryptographic keys of type Key ) into key specifications (transparent representations of the underlying key material), and vice versa. Secret key factories operate only on secret (symmetric) keys. </li></ul><ul><li>Key factories are bi-directional, i.e., they allow to build an opaque key object from a given key specification (key material), or to retrieve the underlying key material of a key object in a suitable format. </li></ul><ul><li>Application developers should refer to their provider's documentation to find out which key specifications are supported by the generateSecret and getKeySpec methods. </li></ul>
  23. 23. Password-based encryption <ul><li>Twofish encryption algorithm: </li></ul><ul><ul><li>A symmetric block cipher that accepts keys of any length, up to 256 bits; </li></ul></ul><ul><ul><li>Among the new encryption algorithms being considered by the National Institute of Science and Technology (NIST) as a replacement for the DES algorithm; </li></ul></ul><ul><ul><li>Highly secure and flexible; </li></ul></ul><ul><ul><li>Works extremely well with large microprocessors, 8-bit smart card microprocessors, and dedicated hardware. </li></ul></ul><ul><ul><li>(Source: http://www. wiley .com/ cda /product/0,,0471353817,00.html ) </li></ul></ul>
  24. 24. Password-based encryption <ul><li>An example program: (see proJavaSecurityCode .html ) </li></ul><ul><li>Sample PBE encryption/decryption: </li></ul><ul><li>> java PBE -e sasquatch &quot;Hello World!&quot; </li></ul><ul><li>yrVhjq5djco=eSIS1LbeAtu5KIKf5ntNhg== </li></ul><ul><li>> java PBE -e sasquatch &quot;Hello World!&quot; </li></ul><ul><li>lQ1lzMl8ONM=GBJFXSnpbltXowvJTmck1w== </li></ul><ul><li>> java PBE -d sasquatch &quot;lQ1lzMl8ONM=GBJFXSnpbltXowvJTmck1w==&quot; </li></ul><ul><li>Hello World! </li></ul>
  25. 25. Key storage <ul><li>Storage of keys in a persistent media (file, database) for later retrieval or transportation </li></ul><ul><li>Objectives: The stored keys must be protected. </li></ul><ul><li>Problems? </li></ul><ul><ul><li>If the key storage is compromised, the data protected by the keys become unprotected. </li></ul></ul><ul><li>Solutions? </li></ul><ul><ul><li>Use PBE to encrypt the keys. Problems? </li></ul></ul>
  26. 26. Key storage <ul><li>Key Wrapping </li></ul><ul><ul><li>The wrap( ) method, defined in javax.crypto.Cipher , takes a key as an argument and returns the encrypted value of the key as a byte array. </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><li>cipher .init (Cipher. WRAP_MODE , passwordKey, paramSpec); </li></ul></ul><ul><ul><li>byte[ ] encryptedKeyBytes = cipher .wrap (secretKey); </li></ul></ul><ul><li>To decrypt the key: </li></ul><ul><ul><li>cipher .init (Cipher. UNWRAP_MODE , passwordKey, paramSpec); </li></ul></ul><ul><ul><li>Key key = cipher .unwrap(encryptedKeyBytes, “Blowfish”, Cipher.SECRET_KEY); </li></ul></ul>
  27. 27. Key storage <ul><li>Key Encryption </li></ul><ul><ul><li>Use the getEncoded( ) method, as defined in , to encrypt the key. </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><li>byte[ ] keyBytes = myKey. getEncoded( ) ; </li></ul></ul><ul><ul><li>cipher .init (Cipher. ENCRYPT_MODE , passwordKey, paramSpec); </li></ul></ul><ul><ul><li>byte[ ] encryptedKeyBytes = cipher .doFinal (keyBytes); </li></ul></ul><ul><li>To decrypt the key: </li></ul><ul><ul><li>cipher .init (Cipher. DECRYPT_MODE , passwordKey, paramSpec); </li></ul></ul><ul><ul><li>byte[ ] keyBytes = cipher.doFinal (encryptedKeyBytes); </li></ul></ul><ul><ul><li>SecretKeySpec myKey = new SecretKeySpec (keyBytes, “Blowfish” ); </li></ul></ul>
  28. 28. Padding <ul><li>Padding is needed to make the size of the plaintext to be a multiple of the block size. </li></ul><ul><li>Most symmetric algorithms use one of two types of padding: </li></ul><ul><ul><li>No padding – requires the data end on a block exactly </li></ul></ul><ul><ul><li>PKCS#5 padding – (PKCS = Public Key Cryptography Standard) </li></ul></ul><ul><ul><li>Suppose there are N bytes in a block that need to be padded. </li></ul></ul><ul><ul><li>Fill each of the N bytes with the value N. </li></ul></ul><ul><ul><li>If the data end on a multiple of the block size, add an entire block of padding. </li></ul></ul><ul><ul><li>(See the illustration on p.81.) </li></ul></ul>
  29. 29. Modes of DES <ul><li>ECB, CBC </li></ul><ul><li>CFB (Cipher FeedBack) </li></ul><ul><ul><li>Similar to CBC, but may work on smaller chunks of data (8 bits for example). </li></ul></ul><ul><li>OFB (Output FeedBack) </li></ul><ul><ul><li>Similar to CFB, but provides better protection against data loss during transmission. </li></ul></ul><ul><ul><li>That is, a single-bit error will not cause the whole block to be lost, as in the cases of ECB, CBC and CFB. </li></ul></ul>
  30. 30. Cipher streams and IV <ul><li>Javax.crypto. CipherInputStream </li></ul><ul><li>javax.crypto. CipherOutputStream </li></ul><ul><ul><li>They provide convenient wrappers around standard input and output streams for them to be automatically encrypted or decrypted. </li></ul></ul><ul><li>Initialization Vector (IV): </li></ul><ul><ul><li>A sequence of random bytes appended to the front of the plaintext before encryption by a block cipher. </li></ul></ul><ul><ul><li>Adding the initialization vector to the beginning of the plaintext eliminates the possibility of having the initial ciphertext block the same for any two messages. </li></ul></ul><ul><ul><li>How to determine the size of a IV, given a cipher? Example: A 256-bit Rijndael cipher needs a 16-byte IV. </li></ul></ul>
  31. 31. IV in Java <ul><li>public class IvParameterSpec </li></ul><ul><ul><li>extends Object </li></ul></ul><ul><ul><li>implements AlgorithmParameterSpec </li></ul></ul><ul><ul><li>This class specifies an initialization vector (IV). Examples which use IVs are ciphers in feedback mode , e.g., DES in CBC mode and RSA ciphers with OAEP encoding operation. </li></ul></ul><ul><ul><li>(NOTE: See page 434 for RSA-OAEP padding.) </li></ul></ul>
  32. 32. Rijndael <ul><li>What is Rijndael ? (Dutch, pronounced as ‘Rain Doll’) </li></ul><ul><li>“ Rijndael is a block cipher, designed by Joan Daemen and Vincent Rijmen as a candidate algorithm for the AES. The cipher has a variable block length and key length. We currently specified how to use keys with a length of 128, 192, or 256 bits to encrypt blocks with al length of 128, 192 or 256 bits.” </li></ul><ul><ul><li>(Source: http://www. esat . kuleuven rijmen / rijndael / ) </li></ul></ul><ul><li>After nearly four years of evaluation, in October 2000, Rijndael was selected by the NIST as the `AES' (Advanced Encryption Standard). See the press release . </li></ul>
  33. 33. <ul><li> (see proJavaSecurityCode .html ) </li></ul><ul><li>Four functions: </li></ul><ul><ul><li>createKey ( password ) </li></ul></ul><ul><ul><li>loadKey ( password ) </li></ul></ul><ul><ul><li>encrypt ( password, inputFile, outputEncryptedFile ) </li></ul></ul><ul><ul><li>decrypt ( password, inputEncryptedFile, outputfile) </li></ul></ul>
  34. 34. Sealed objects <ul><li>Sealed object: An object that is encrypted. </li></ul><ul><li>The object must be serializable. </li></ul><ul><li>Sealed objects can be useful for storing or transferring an encrypted version of an object. </li></ul><ul><li>The default JDK 1.2 prevents extensions from using the class loader to create classes that are neither standard objects nor extensions. That is, a custom object such as a CreditCard object, won’t be able to be decrypted. </li></ul><ul><li>See Appendix D “the EncryptedObject class” for a better sealed object implementation. </li></ul>
  35. 35. Sealed objects <ul><li>SealedObjectExample .java (see proJavaSecurityCode .html ) </li></ul><ul><li>Sample output: </li></ul><ul><ul><li>> java SealedObjectExample </li></ul></ul><ul><ul><li>Creating a key. </li></ul></ul><ul><ul><li>Encrypting the object. </li></ul></ul><ul><ul><li>Unencrypting the object. </li></ul></ul><ul><ul><li>Credit card number: 1234567890 </li></ul></ul>
  36. 36. Next <ul><li>Asymmetric Encryption (GS: 5) </li></ul><ul><li>Relevant links: </li></ul><ul><ul><li>RFC 1829 - The ESP DES-CBC Transform - This document describes the DES-CBC security transform for the IP Encapsulating Security Payload (ESP). </li></ul></ul><ul><ul><li>The GNU Crypto project – This project aims at providing free, versatile, high-quality, and provably correct implementations of cryptographic primitives and tools in the Java programming language for use by programmers and end-users. It’s also got a comprehensive listing of crypto-related algorithms. </li></ul></ul>