Java Cryptography This is a presentation on how


















![Save the Encrypted/Signed File • Translate the header into a byte[] and save to Save the Encrypted/Signed File • Translate the header into a byte[] and save to](https://slidetodoc.com/presentation_image_h2/b25dfaefa858c082d7f58eb7c92a3db1/image-19.jpg)














- Slides: 33

Java Cryptography This is a presentation on how to implement a hybrid combination of RSA asymmetric encryption and AES symmetric encryption using the Java language and the Bouncy Castle Cryptographic libraries. It is assumed the audience is familiar with Cryptography and aware of the difference between asymmetric and symmetric encryption. Also, the audience should be experienced in coding with Java. 9/17/2021 Java Cryptography 1

Required Software The following software should be downloaded to implement the RSA/AES cryptographic functions. • Bouncy Castle Jars ver 1. 49 – www. Bouncy. Castle. org. - bcprov-jdk 15 on-149. jar - bcpkix-jdk 15 on-149. jar • Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 7 http: //www. oracle. com/technetwork/javase/downloads /jce-7 -download-432124. html Copy the contents of the zip file to the d: jre 7libsecurity directory. (Make a backup before overwriting) - Unlimited. JCEPolicy. JDK 7. zip • Java Development Kit version 7. 9/17/2021 Java Cryptography 2

Implementation Plan To implement RSA/AES cryptographic functions using Java, we will use the following strategy. • Generate a pair of RSA public/private (encryption/decryption) keys. • Save the encryption key in an X 509 Certificate and the decryption key and certificate in a password protected Key. Store. • Generate an AES symmetric key. Load the encryption key from the certificate to encrypt the symmetric key and store in a header. • Encrypt and digitally sign data to temporary encrypted file and store signature in header. Write out header and attach temporary encrypted file. • Load the certificate and verify signature with encryption key. • Load the decryption key from the Key. Store and decipher the symmetric key in the header. Use the symmetric key to decrypt the encrypted file. 9/17/2021 Java Cryptography 3

Generate Asymmetric Key Pair 9/17/2021 Java Cryptography 4

Generate Asymmetric Key Pair • Instantiate a Key. Pair. Generator for generating a pair of RSA asymmetric keys. • Generate a secure random number. • Initialize the Key. Pair. Generator with desired key size and the secure random number. • Generate a pair of public/private keys. • Code Snippet Key. Pair. Generator t. KPGen = Key. Pair. Generator. get. Instance("RSA", "BC"); Secure. Random t. Random = new Secure. Random(); t. KPGen. initialize(2048, t. Random); //-Initialize the Asymmetric KEY Size Key. Pair t. Pair = t. KPGen. generate. Key. Pair(); Public. Key t. User. Pub. Key = t. Pair. get. Public(); //-Encryption key Private. Key t. User. Priv. Key = t. Pair. get. Private(); //-Decryption key 9/17/2021 Java Cryptography 5

Create X 509 Certificate • Use the X 509 v 3 Certificate. Builder class to generate a version 3 certificate and digitally sign it with the SHA 512 With. RSAEncryption algorithm. • Code Snippet Jca. Content. Signer. Builder t. Sign. Bldr = new Jca. Content. Signer. Builder("SHA 512 With. RSAEncryption"); t. Sign. Bldr. set. Provider("BC"); //-Set provider as Bouncy. Castle Content. Signer t. Sig. Gen = t. Sign. Bldr. build(t. User. Priv. Key); X 500 Name. Builder t. Builder = new X 500 Name. Builder(BCStyle. INSTANCE); t. Builder. add. RDN(BCStyle. CN, "John X. Doe"); //-Common Name t. Builder. add. RDN(BCStyle. E, "jxdoe@acme. com"); //-E-mail t. Builder. add. RDN(BCStyle. O, "ACME Inc"); //-Organization t. Builder. add. RDN(BCStyle. OU, "Executive Group"); //-Organization Unit t. Builder. add. RDN(BCStyle. L, "Detroit"). add. RDN(BCStyle. ST, "MI"). add. RDN(BCStyle. C, "USA"); //-City, State, Country org. bouncycastle. asn 1. x 500. X 500 Name t. X 500 Name = t. Builder. build(); Calendar t. Cal = Calendar. get. Instance(); t. Cal. set(2014, 11, 31); //-Dec. 31, 2014, Month is zero indexed X 509 v 3 Certificate. Builder t. V 3 Cert. Gen = new Jca. X 509 v 3 Certificate. Builder( t. X 500 Name, //-Issuer is same as Subject Big. Integer. value. Of( System. current. Time. Millis()), //-Serial Number new java. util. Date(), //-Date start = today t. Cal. get. Time(), //-Date end t. X 500 Name, //-Subject t. User. Pub. Key); //-Public RSA Key X 509 Certificate. Holder t. Cert. Holder = t. V 3 Cert. Gen. build(t. Sig. Gen); Jca. X 509 Certificate. Converter t. Converter = new Jca. X 509 Certificate. Converter(). set. Provider("BC"); X 509 Certificate t. Cert = t. Converter. get. Certificate(t. Cert. Holder); 9/17/2021 Java Cryptography 6

Save X 509 Certificate to File • The certificate can be validated by date and its digital signature verified with the public encryption key. • Convert certificate to byte array and write it out to file. • Code Snippet t. Cert. check. Validity(new Date()); //-Throws exception if certificate expired t. Cert. verify(t. User. Pub. Key); //-Throws exception if certificate is invalid byte[] t. BA = t. Cert. get. Encoded(); //-Convert certificate to byte array File t. File = new File("F: \ jxdoe_nnnn. cer"); File. Output. Stream t. FOS = new File. Output. Stream(t. File); t. FOS. write(t. BA); t. FOS. close(); 9/17/2021 Java Cryptography 7

Generate and Save Key. Store • Instantiate an array of certificates called a chain and store the built one holding the public encryption key into it. • Instantiate a Key. Store to hold RSA private decryption key and certificate chain. It is password protected. • Code Snippet Key. Store t. KStore = Key. Store. get. Instance("JKS", "SUN"); t. KStore. load(null, null); //-Initialize the Key. Store X 509 Certificate[] t. Chain = new X 509 Certificate[1]; //-Put certificate into a chain t. Chain[0] = t. Cert; t. KStore. set. Key. Entry("jxdoe_nnnn", t. User. Priv. Key, "password". to. Char. Array(), t. Chain); t. FOS = new File. Output. Stream("F: \jxdoe_nnnn. jks"); t. KStore. store(t. FOS, "password". to. Char. Array()); //-Set Key. Store password t. FOS. close(); 9/17/2021 Java Cryptography 8

Encrypting/Signing a File 9/17/2021 Java Cryptography 9

Encrypting/Signing a File A file can be encrypted using the following steps. • Select a file to be encrypted, e. g. C: Sample. File. txt • Generate a symmetric AES 256 -bit key. • Generate an initialization vector. • Load the certificate and retrieve the public encryption key to encrypt the symmetric key and vector and store in header. • Load the keystore and retrieve the private decryption key. • Instantiate a cipher with the symmetric key and initialization vector to encrypt the file. • Use the cipher to encrypt the file and write out the results to temporary file. Use the decryption key to generate a signature and save in the header. • Write out the header and append the temporary encrypted file. 9/17/2021 Java Cryptography 10

Generating Symmetric Key and Initialization Vector • The Key. Generator will create a 256 -bit AES symmetric key. • The initialization vector is a byte[] that is populated by the Secure. Random class. • Once the key and vector are created, they can be encrypted with the public RSA key and stored in the encrypted file. • Code Snippet Key. Generator t. Key. Gen = Key. Generator. get. Instance("AES", "BC"); Secure. Random t. Random 2 = new Secure. Random(); t. Key. Gen. init(256, t. Random 2); //-256 -bit Secret. Key t. Symmetric. Key = t. Key. Gen. generate. Key(); //-Generate the initialization vector since the mode chosen requires one int t. Size = Cipher. get. Instance("AES", "BC"). get. Block. Size(); byte[] t. Init. Vector = new byte[t. Size]; Secure. Random t. Random 3 = new Secure. Random(); t. Random 3. next. Bytes(t. Init. Vector); //-Fill the initialization vector Iv. Parameter. Spec t. IVSpec = new Iv. Parameter. Spec(t. Init. Vector); 9/17/2021 Java Cryptography 11

Load Certificate and Retrieve Public Encryption Key • The Certificate. Factory will load a certificate allowing access to the public RSA encryption key. • After the certificate is loaded, extract the public key. • The certificate can be distributed to others so that they can encrypt files and e-mail them to you as attachments since only the owner of the private key can decrypt them. • Code Snippet Input. Stream t. IStream = new File. Input. Stream("F: \jxdoe_nnnn. cer"); Certificate. Factory t. Factory = Certificate. Factory. get. Instance("X. 509", "BC"); X 509 Certificate t. Loaded. Cert = (X 509 Certificate)t. Factory. generate. Certificate(t. IStream); t. IStream. close(); t. User. Pub. Key = t. Loaded. Cert. get. Public. Key(); //-Encryption key 9/17/2021 Java Cryptography 12

Generate Cryptography Header • A Cryptography header is needed to hold the information used to encrypt and digitally sign a file or Java object. • Code Snippet //-Generate a Cryptography header that stores cryptographic information used //-to later decrypt the file and verify the digital signature. Save the //-symmetric algorithm, mode and padding in the header. Crypto. Header t. Head = new Crypto. Header(); t. Head. set. Encrypt. Flag(true); t. Head. set. Signed. Flag(true); t. Head. sym. Key. Alg(1); //-AES t. Head. sym. Key. Mode(5); //-CTR Segmented Integer Counter mode (requires an init vector) t. Head. sym. Key. Padding(2); //-PKCS 7 Padding t. Head. decrypt. IDLength(t. Unique. Alias. length()); t. Head. decrypt. ID(t. Unique. Alias); //-Owner’s unique alias t. Head. asym. Key. Alg(1); //-Asymmetric algorithm RSA 9/17/2021 Java Cryptography 13

Encrypt the Symmetric Key and Initialization Vector • The Java and Bouncy Castle libraries provide features to protect the symmetric key and initialization vector. • Instantiate a Cipher object with the Public Encryption Key. Encrypt the key and vector and store in the header. • Code Snippet //-Wrap(Encrypt) the AES symmetric key using the asymmetric public key and //-store it in the header. Cipher t. Cipher. RSA = Cipher. get. Instance("RSA", "BC"); t. Cipher. RSA. init(Cipher. WRAP_MODE, t. User. Pub. Key); byte[] t. Wrapped. Key = t. Cipher. RSA. wrap(t. Symmetric. Key); t. Head. wrapped. Sym. Key. Length(t. Wrapped. Key. length); t. Head. wrapped. Sym. Key(t. Wrapped. Key); //-Encrypt the initialization vector using the same cipher and store it //-in the header. t. Cipher. RSA. init(Cipher. ENCRYPT_MODE, t. User. Pub. Key); byte[] t. Init. Vector. Encrypted = t. Cipher. RSA. do. Final(t. IVSpec. get. IV()); t. Head. init. Vector. Length(t. Init. Vector. Encrypted. length); t. Head. init. Vector(t. Init. Vector. Encrypted); 9/17/2021 Java Cryptography 14

Load Key. Store and Retrieve Private Decryption Key • Load the password protected keystore and retrieve the RSA private decryption key. • Instantiate a Signature Engine with the decryption key to sign the encrypted data. Initialize it to generate a signature. • Code Snippet //-Retrieve the private decryption key stored in a Java key store associated to a unique alias File. Input. Stream t. Store. FIS = new File. Input. Stream(t. Working. Dir + "\jxdoe_nnnn. jks"); Key. Store t. My. KStore = Key. Store. get. Instance("JKS", "SUN"); char[] t. PW = "password". to. Char. Array(); t. My. KStore. load(t. Store. FIS, t. PW); Private. Key t. Priv. Key = (Private. Key)t. My. KStore. get. Key(t. Unique. Alias, t. PW); t. Store. FIS. close(); //-Generate a Java Signature object to sign the encrypted file and store the //-information in the header. Signature t. Sig. Engine = Signature. get. Instance("SHA 512 With. RSAEncryption", "BC"); t. Sig. Engine. init. Sign(t. Priv. Key); //-Initialize engine to GENERATE a signature t. Head. signature. Alg(12); //-SHA 512 With. RSAEncryption 9/17/2021 Java Cryptography 15

Create a Symmetric Key Cipher • The Cipher class provides the encrypt/decrypt functions for both asymmetric and symmetric keys. • Instantiate an AES symmetric key cipher using Segmented Integer Counter mode (CTR) and PKCS 7 padding which will be used to encrypt the file. • Modes are used so that each block of data is encrypted differently. • Code Snippet //-Generate a Java Cipher object based on the symmetric algorithm, mode, padding //-and provider which will be used to encrypt the target file. Cipher t. Cipher. Encrypt = Cipher. get. Instance("AES/CTR/PKCS 7 Padding", "BC"); t. Cipher. Encrypt. init(Cipher. ENCRYPT_MODE, t. Symmetric. Key, t. IVSpec); 9/17/2021 Java Cryptography 16

Use the Cipher to Encrypt • Loop through the source file and encrypt the data a buffer at a time. Process each encrypted buffer for a digital signature. Write out the results to a temporary file which will be deleted afterwards. • Code Snippet Input. Stream t. File. IS = new File. Input. Stream(t. Working. Dir + "\sample. File. txt"); File. Output. Stream t. File. OS = new File. Output. Stream(t. Working. Dir + "\$$$$. tmp"); byte[] t. In. Buffer = new byte[4096]; byte[] t. Out. Buffer = new byte[4096]; int t. Num. Of. Bytes. Read = t. File. IS. read(t. In. Buffer); while (t. Num. Of. Bytes. Read == t. In. Buffer. length) { //-Encrypt the input data and store in output buffer int t. Num. Of. Bytes. Updated = t. Cipher. Encrypt. update(t. In. Buffer, 0, t. In. Buffer. length, t. Out. Buffer); t. Sig. Engine. update(t. Out. Buffer, 0, t. Num. Of. Bytes. Updated); //-Sign the encrypted data t. File. OS. write(t. Out. Buffer, 0, t. Num. Of. Bytes. Updated); //-Write encrypted data to temp file t. Num. Of. Bytes. Read = t. File. IS. read(t. In. Buffer); //-Read in the next buffer of source data } if (t. Num. Of. Bytes. Read > 0) { //-Process the remaining bytes in the input file. t. Out. Buffer = t. Cipher. Encrypt. do. Final(t. In. Buffer, 0, t. Num. Of. Bytes. Read); } else { t. Out. Buffer = t. Cipher. Encrypt. do. Final(); } t. Sig. Engine. update(t. Out. Buffer); //-Sign the remaining bytes t. File. OS. write(t. Out. Buffer, 0, t. Out. Buffer. length); … //-Close the input and temporary files 9/17/2021 Java Cryptography 17

Generate Digital Signature • Once the signature engine has finished processing the encrypted data, it can generate a digital signature. • The signature is saved in the header as a byte[]. • Code Snippet //-Generate the digital signature from the signature engine after //-signing the file and store the information in the header. byte[] t. Signature = t. Sig. Engine. sign(); t. Head. signature(t. Signature); t. Head. signature. Length(t. Signature. length); t. Head. verify. Sig. Cert. Name( “jxdoe_nnnn. cer"); t. Head. verify. Sig. Cert. Name. Length(t. Head. verify. Sig. Cert. Name(). length()); 9/17/2021 Java Cryptography 18
![Save the EncryptedSigned File Translate the header into a byte and save to Save the Encrypted/Signed File • Translate the header into a byte[] and save to](https://slidetodoc.com/presentation_image_h2/b25dfaefa858c082d7f58eb7c92a3db1/image-19.jpg)
Save the Encrypted/Signed File • Translate the header into a byte[] and save to disk. • Append the temporary encrypted file. • Code Snippet //-Calculate the total size of the header and save. Write out header using Java File. Output. Stream t. File. OStream = new File. Output. Stream(“d: Sample. File. txt. jxdoe_nnnn. asg”); byte[] t. Array = (byte[])t. Head. write. Out. Header. As. Byte. Array(); t. File. OStream. write(t. Array, 0, t. Array. length); //-Append the temporary “encrypted” file to the output stream. t. File. IS = new File. Input. Stream(t. Temp. File. Name); byte[] t. Buffer = new byte[4096]; int t. Length = t. File. IS. read(t. Buffer); while (t. Length > 0) { t. File. OStream. write(t. Buffer, 0, t. Length); t. Length = t. File. IS. read(t. Buffer); } t. File. OStream. close(); t. File. IS. close(); … //-Securely delete the temporary file by overwriting it. 9/17/2021 Java Cryptography 19

Verifying Digital Signature 9/17/2021 Java Cryptography 20

Verifying Digital Signature A signed encrypted file can verify the signature using the following steps. • Select a file to be validated, e. g. C: Sample. File. txt. jxdoe_nnnn. asg. • Read in the Crypto Header • Load the certificate so you have access to the public encryption key. • Instantiate a Signature engine object in VERIFY mode using the encryption key. • Use the Signature engine to process the encrypted file. • Use the Signature engine to verify the validity of the signature generated with the decryption key stored in the header. 9/17/2021 Java Cryptography 21

Read the Cryptography Header • After selecting the encrypted file to decipher, extract the filename of the decrypted file. • Create a Data. Input. Stream to the encrypted file and read in the Cryptography header. • Code Snippet //-Create input stream to the encrypted file and read in the header. String t. Decrypt. File = t. Encrypted. Signed. File. get. Name(); //-Parse off the alias and. asg extension to derive the decrypted output file name t. Decrypt. File = t. Decrypt. File. substring(0, t. Decrypt. File. last. Index. Of('. ')); File. Output. Stream t. File. OStream = new File. Output. Stream(t. Working. Dir + "\" + t. Decrypt. File); Data. Input. Stream t. DIn. Stream = new Data. Input. Stream(new File. Input. Stream(t. Encrypted. Signed. File)); Object t. RC = Crypto. Header. read. Header(t. DIn. Stream); Crypto. Header t. Head = (Crypto. Header)t. RC; 9/17/2021 Java Cryptography 22

Load Certificate and Retrieve Public Encryption Key • The Certificate. Factory will load a certificate allowing access to the public RSA encryption key. • After the certificate is loaded, extract the encryption key and instantiate a signature engine. • Code Snippet Input. Stream t. IStream = new File. Input. Stream(“d: \jxdoe_nnnn. cer"); Certificate. Factory t. Factory = Certificate. Factory. get. Instance("X. 509", "BC"); X 509 Certificate t. Loaded. Cert = (X 509 Certificate)t. Factory. generate. Certificate(t. IStream); t. IStream. close(); t. User. Pub. Key = t. Loaded. Cert. get. Public. Key(); //-Encryption key //-Instantiate a Java signature engine associated with the signature algorithm //-stored in the header. Initialize it with the asymmetric public key. String t. Sig. Alg = t. Head. signature. Alg. Desc(); Signature t. Sgn. Verify. Engine = Signature. get. Instance(t. Sig. Alg, "BC"); t. Sgn. Verify. Engine. init. Verify(t. User. Pub. Key); //-Initialize to VERIFY a signature 9/17/2021 Java Cryptography 23

Use Engine to Process Encrypted File • After the signature engine is instantiated with the public encryption key, read in the rest of the encrypted input file and use the engine to process it. • Code Snippet //-Calculate hash number(signature) that will be compared with signature stored in the header. int t. Block. Size = 4096; byte[] t. Buffer = new byte[t. Block. Size]; int t. Length = t. DIn. Stream. read(t. Buffer); while (t. Length == t. Block. Size) { t. Sgn. Verify. Engine. update(t. Buffer, 0, t. Block. Size); t. Length = t. DIn. Stream. read(t. Buffer); } //-Are there any bytes left over to process? ? if (t. Length > 0) { t. Sgn. Verify. Engine. update(t. Buffer, 0, t. Length); } t. DIn. Stream. close(); 9/17/2021 Java Cryptography 24

Verify Signature • After processing the encrypted file, the signature engine will verify the signature generated with the private decryption key stored in the header. • A match means the files has NOT been altered. • Code Snippet //-After the file has been processed, use the Java signature engine to //-verify its result with the digital signature. //-A Boolean result is returned on whether the signature was valid. Boolean t. Result = t. Sgn. Verify. Engine. verify(t. Curr. Signature); if (t. Result) { System. out. println("-Signature has been VERIFIED. Result is " + t. Result); } else { System. out. println("-Signature has NOT been VERIFIED! Result is " + t. Result); System. exit(-1); //-Leave if signature is NOT valid } 9/17/2021 Java Cryptography 25

Decrypting an Encrypted File 9/17/2021 Java Cryptography 26

Decrypting an Encrypted File An encrypted file can be decrypted using the following steps. • Select a file to be decrypted, e. g. C: Sample. File. txt. jxdoe_nnnn. asg. • Read in the Crypto Header • Load the Key. Store so you have access to the private decryption key. • Use the private decryption key to decrypt the AES 256 bit symmetric key and initialization vector. • Instantiate a cipher with the symmetric key and initialization vector to decrypt the file. • Use the cipher to decrypt the file and write out the results. 9/17/2021 Java Cryptography 27

Read the Cryptography Header • After selected the encrypted file to decipher, extract the filename for the decrypted file. • Create a Data. Input. Stream to the encrypted file and read in the Cryptography header. • Code Snippet //-Create input stream to the encrypted file and read in the header. String t. Decrypt. File = t. Encrypted. Signed. File. get. Name(); //-Parse off the alias and. asg extension to derive the decrypted output file name t. Decrypt. File = t. Decrypt. File. substring(0, t. Decrypt. File. last. Index. Of('. ')); File. Output. Stream t. File. OStream = new File. Output. Stream(t. Working. Dir + "\" + t. Decrypt. File); Data. Input. Stream t. DIn. Stream = new Data. Input. Stream(new File. Input. Stream(t. Encrypted. Signed. File)); Object t. RC = Crypto. Header. read. Header(t. DIn. Stream); Crypto. Header t. Head = (Crypto. Header)t. RC; 9/17/2021 Java Cryptography 28

Loading a Key. Store • The Key. Store class will load a File. Input. Stream referencing the externally saved Key. Store file. • After the Key. Store is loaded, extract the private key via its password. • Code Snippet //-Load the key store using your password. Retrieve the asymmetric private decryption key //-from the key store using the same password. The decryption key will //-be used to decipher the symmetric key. File. Input. Stream t. FIStream = new File. Input. Stream(t. Working. Dir + "\" + t. Unique. Alias + ". jks"); Key. Store t. My. KStore = Key. Store. get. Instance("JKS", "SUN"); //t. PW = "password". to. Char. Array(); t. My. KStore. load(t. FIStream, t. PW); Private. Key t. Priv. Key = (Private. Key)t. My. KStore. get. Key(t. Unique. Alias, t. PW); t. FIStream. close(); //-Always close streams when done with them. 9/17/2021 Java Cryptography 29

Retrieve Symmetric Key and Initialization Vector • The symmetric key and initialization vector are stored in the header of the encrypted file. • The key and vector are decrypted with the RSA private decryption key. • A decryption Cipher is generated with the key and vector. • Code Snippet //-Generate a Java Cipher object using the asymmetric private key //-and set its mode to "Cipher. UNWRAP_MODE”. Cipher t. Cipher. RSA = Cipher. get. Instance("RSA", "BC"); t. Cipher. RSA. init(Cipher. UNWRAP_MODE, (Private. Key)t. Priv. Key); //-Use the Java Cipher to unwrap(decrypt) the symmetric key. //-The symmetric key will be used to decrypt the file. String t. Alg = t. Head. sym. Key. Alg. Desc(); byte[] t. Wrapped. Sym. Key = t. Head. wrapped. Sym. Key(); Secret. Key t. Sym. Key = (Secret. Key)t. Cipher. RSA. unwrap(t. Wrapped. Sym. Key, t. Alg, Cipher. SECRET_KEY); //-Re-initialize the same Cipher to Cipher. DECRYPT_MODE. //-Use the Cipher to decrypt the initialization vector. t. Cipher. RSA. init(Cipher. DECRYPT_MODE, (Private. Key)t. Priv. Key); byte[] t. Init. Vector = t. Cipher. RSA. do. Final(t. Head. init. Vector()); Iv. Parameter. Spec t. Iv. Parm. Spec = new Iv. Parameter. Spec(t. Init. Vector); 9/17/2021 Java Cryptography 30

Use the Cipher to Decrypt • After the cipher is instantiated, read in the rest of the input file and use the cipher to decrypt it and save it to disk. • Code Snippet //-Instantiate a symmetric Cipher for DECRYPTION Cipher t. Cipher. Decrypt = Cipher. get. Instance("AES/CTR/PKCS 7 Padding", "BC"); t. Cipher. Decrypt. init(Cipher. DECRYPT_MODE, t. Sym. Key, t. Iv. Parm. Spec); //-Use the Java Cipher to decrypt the rest of the file. The end result is a decrypted file. byte[] t. In. Buffer = new byte[4096]; byte[] t. Out. Buffer = new byte[4096]; int t. Num. Of. Bytes. Read = t. DIn. Stream. read(t. In. Buffer); while (t. Num. Of. Bytes. Read == t. In. Buffer. length) { //-Decrypt the input data and store in output buffer int t. Num. Of. Bytes. Updated = t. Cipher. Decrypt. update(t. In. Buffer, 0, t. In. Buffer. length, t. Out. Buffer); t. File. OStream. write(t. Out. Buffer, 0, t. Num. Of. Bytes. Updated); t. Num. Of. Bytes. Read = t. DIn. Stream. read(t. In. Buffer); } if (t. Num. Of. Bytes. Read > 0) { //-Process the remaining bytes in the input file. t. Out. Buffer = t. Cipher. Decrypt. do. Final(t. In. Buffer, 0, t. Num. Of. Bytes. Read); } else { t. Out. Buffer = t. Cipher. Decrypt. do. Final(); } t. File. OStream. write(t. Out. Buffer, 0, t. Out. Buffer. length); t. File. OStream. close(); t. DIn. Stream. close(); 9/17/2021 Java Cryptography 31

References • Source code for the presentation is in the file, Java. Crypto. Sample. java on the Logical Answers Inc. website on the education page. • Hook, David. Beginning Cryptography with Java. Wrox Press. ISBN: 0 -7645 -9633 -0. August 2005. 448 pages. • Horstman, Cay and Cornell, Gary. Core Java 2 Volume II -Advanced Features. Sun Microsystems Press. ISBN: 013 -092738 -4. 2002. 1024 pages. • Singh, Simon. The Code Book. Doubleday ISBN: 0 -38549531 -5. September 14, 1999. 416 pages. 9/17/2021 Java Cryptography 32

Contact Information Logical Answers Inc. 491 Leetonia Ave Troy, Michigan 48085 -5518 (248) 528 -4498 www. Logical. Answers. com jhwong@logicalanswers. com We offer custom programming and technology consulting services. Our Docu. Armor suite of cryptographic products are for sale offering encryption and secure socket communications. 9/17/2021 Java Cryptography 33