Android Open Source - clinicalguide Encryption






From Project

Back to project page clinicalguide.

License

The source code is released under:

Apache License

If you think the Android project clinicalguide listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.get.oxicam.clinicalguide.encryption;
/*  ww w  . ja  v a2s  . c  om*/
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * This is the class used for encrypting or decrypting data. If
 * those using this class need data to be encrypted with a different
 * password all that needs to be done is changing the value of the 
 * password String below to something else, and must be at least 
 * 8 bytes in size. The IV can also be changed to something else,
 * and must be exactly 8 bytes in size.
 * 
 * @author Michael Dimchuk
 */
public class Encryption 
{
  /**
   * The cipher used in decryption of data.
   */
  private Cipher deCipher;
  /**
   * The cipher used in encryption of data.
   */
  private Cipher enCipher;
  /**
   * The key used in encrypting data, converted from an array of bytes of the password.
   */
  private SecretKeySpec key;
  /**
   * The iv used in encrypting data, converted from an array of bytes of the iv.
   * It is used to strengthen the encryption of data.
   */
  private IvParameterSpec ivSpec;
  /**
   * The name of the input file to either decrypt or encrypt.
   */
  private String inputfile;
  /**
   * The actual password which is converted to bytes and then a key is created out of it.
   * It has a minimum size of 8 bytes, in this case 8 characters, and can be very large. 
   * The password used here was randomly generated.
   */
  private String password = "M3607Go989VhY3320w647452DIL613D94554T8799U4b39lBWhv2TyfcC699655870487rJn5B" +
      "Wgxk5Tj6V6us6Y015eL3EaI458SG7L931868fR86Gk41A4LD1417BF0hb982gy00SDNV4068592Y55Sd35z19";
  /**
   * This is the IV. It is used to strengthen the encryption with the password. It must be 
   * 8 bytes in size, in this case 8 characters. This IV was randomly generated.
   */
  private String iv = "482UYaIM";
  
  public Encryption(String filename)
  {
      ivSpec       = new IvParameterSpec(iv.getBytes());
      DESKeySpec dkey = null;
    try 
    {
      dkey     = new  DESKeySpec(password.getBytes());
    } 
    catch (InvalidKeyException e) { e.printStackTrace(); }
    
      key       = new SecretKeySpec(dkey.getKey(), "DES");
      
      try 
      {
        deCipher   = Cipher.getInstance("DES/CBC/PKCS5Padding");
      enCipher   = Cipher.getInstance("DES/CBC/PKCS5Padding");
    } 
      catch (NoSuchAlgorithmException e) { e.printStackTrace(); } 
      catch (NoSuchPaddingException e) { e.printStackTrace(); }
      inputfile     = filename;
  }
  /**
   * Decrypts the contents of a file
   * @param inputfile
   * @return
   */
  public byte[] decryptFile(byte[] file)
  {
    byte[] input = null;
    try 
    {
      input = decrypt(file);
    } 
    catch (InvalidKeyException e) { e.printStackTrace(); } 
    catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } 
    catch (IllegalBlockSizeException e) { e.printStackTrace(); } 
    catch (BadPaddingException e) { e.printStackTrace(); }
    return input;
  }
  /**
   * Decrypts the contents of a file
   * @param inputfile
   * @return 
   */
  public byte[] decryptFile()
  {
    byte[] input   = null;
    try 
    {
      input     = decrypt(getEncryptedInput(inputfile));
    } 
    catch (InvalidKeyException e) { e.printStackTrace(); } 
    catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } 
    catch (IllegalBlockSizeException e) { return null; } 
    catch (BadPaddingException e) { return null; } 
    catch (IOException e) { return null; }
    return input;
  }
  /**
   * Gets all of the contents of an encrypted file.
   * @param filename The name of the file to read.
   * @throws IOException
   */
  private byte[] getEncryptedInput(String filename) throws IOException
  {
    FileInputStream read   = new FileInputStream(filename);
    byte[] input       = new byte[read.available()];
    read.read(input);
    read.close();
    return input;
  }
  /**
   * Decrypts the contents of a string.
   * @param data A string to decrypt.
   * @return An array of decrypted bytes.
   */
  public byte[] decryptData(String data)
  {
    byte[] input   = null;
    try 
    {
      input     = data.getBytes("ISO-8859-1");
    } 
    catch (UnsupportedEncodingException e1) { e1.printStackTrace(); }
    try 
    {
      input     = decrypt(input);
    } 
    catch (InvalidKeyException e) { e.printStackTrace(); } 
    catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } 
    catch (IllegalBlockSizeException e) { e.printStackTrace(); } 
    catch (BadPaddingException e) { e.printStackTrace(); } 
    return input;
  }
  
  /**
   * Encrypts the contents of a string.
   * @param data The array of bytes to encrypt.
   * @return An encrypted array of bytes.
   */
  public byte[] encryptData(String data)
  {
    byte[] input   = null;
    try 
    {
      input     = data.getBytes("ISO-8859-1");
    } 
    catch (UnsupportedEncodingException e1) { e1.printStackTrace(); }
    try 
    {
      input     = encrypt(input);
    } 
    catch (InvalidKeyException e) { e.printStackTrace(); } 
    catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } 
    catch (IllegalBlockSizeException e) { e.printStackTrace(); } 
    catch (BadPaddingException e) { e.printStackTrace(); } 
    return input;
  }
  /**
   * Encrypts an object converted into bytes.
   * @param objectToEncrypt The object that needs to be encrypted.
   * @return An array of bytes which have been encrypted.
   * @throws IOException
   * @throws InvalidKeyException
   * @throws InvalidAlgorithmParameterException
   * @throws IllegalBlockSizeException
   * @throws BadPaddingException
   */
  public byte[] encrypt(byte[] byteInput) throws InvalidKeyException, 
      InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
  {
      enCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
      return enCipher.doFinal(byteInput);
  }
  /**
   * Decrypts an array of bytes.
   * @param encrypted An array of bytes to decrypt.
   * @return A decrypted object.
   * @throws InvalidKeyException
   * @throws InvalidAlgorithmParameterException
   * @throws IllegalBlockSizeException
   * @throws BadPaddingException
   * @throws IOException
   * @throws ClassNotFoundException
   */
  private byte[] decrypt(byte[]  encrypted) throws InvalidKeyException, InvalidAlgorithmParameterException, 
            IllegalBlockSizeException, BadPaddingException
  {
    deCipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
      return deCipher.doFinal(encrypted);
  }
}




Java Source Code List

.PatientDetailsDataSource.java
org.get.oxicam.clinicalguide.ClinicalGuideActivity.java
org.get.oxicam.clinicalguide.FileUtils.java
org.get.oxicam.clinicalguide.LoginActivity.java
org.get.oxicam.clinicalguide.db.DatabaseHelper.java
org.get.oxicam.clinicalguide.db.Database.java
org.get.oxicam.clinicalguide.db.FollowupDataSource.java
org.get.oxicam.clinicalguide.db.FollowupDetails.java
org.get.oxicam.clinicalguide.db.FollowupSQLHelper.java
org.get.oxicam.clinicalguide.db.HistoryDetailsDataSource.java
org.get.oxicam.clinicalguide.db.HistoryDetailsSQLHelper.java
org.get.oxicam.clinicalguide.db.HistoryDetails.java
org.get.oxicam.clinicalguide.db.PatientDetailsSQLHelper.java
org.get.oxicam.clinicalguide.db.PatientDetails.java
org.get.oxicam.clinicalguide.db.RegistrationValidator.java
org.get.oxicam.clinicalguide.db.Validator.java
org.get.oxicam.clinicalguide.encryption.Encryption.java
org.get.oxicam.clinicalguide.ui.AnswersReviewFragment.java
org.get.oxicam.clinicalguide.ui.AssessmentDetailFragment.java
org.get.oxicam.clinicalguide.ui.ClassificationFragment.java
org.get.oxicam.clinicalguide.ui.ClassificationListItem.java
org.get.oxicam.clinicalguide.ui.DatePickerFragment.java
org.get.oxicam.clinicalguide.ui.ExportFragment.java
org.get.oxicam.clinicalguide.ui.ExportscreenListItem.java
org.get.oxicam.clinicalguide.ui.FollowUpFragment.java
org.get.oxicam.clinicalguide.ui.FormScreenFragment.java
org.get.oxicam.clinicalguide.ui.HomescreenFragment.java
org.get.oxicam.clinicalguide.ui.HomescreenListItem.java
org.get.oxicam.clinicalguide.ui.ListItemOnClickListener.java
org.get.oxicam.clinicalguide.ui.MainSymptomFragment.java
org.get.oxicam.clinicalguide.ui.MainSymptomListItem.java
org.get.oxicam.clinicalguide.ui.NumberTickerValueChangeListener.java
org.get.oxicam.clinicalguide.ui.NumberTicker.java
org.get.oxicam.clinicalguide.ui.PatientDetailsFragment.java
org.get.oxicam.clinicalguide.ui.PatientHistoryFragment.java
org.get.oxicam.clinicalguide.ui.PatientsFragment.java
org.get.oxicam.clinicalguide.ui.QuestionListItem.java
org.get.oxicam.clinicalguide.ui.QuestionnaireFragment.java
org.get.oxicam.clinicalguide.ui.StatScreenFragment.java
org.get.oxicam.clinicalguide.ui.SummaryScreenFragment.java
org.get.oxicam.clinicalguide.ui.TextViewCustomFont.java
org.get.oxicam.clinicalguide.ui.TreatmentConfirmationDialog.java
org.get.oxicam.clinicalguide.ui.TreatmentFragment.java
org.get.oxicam.clinicalguide.ui.TreatmentListItem.java
org.get.oxicam.clinicalguide.ui.ViewDetailScreenFragment.java
org.get.oxicam.clinicalguide.xml.CGFormParser.java
org.get.oxicam.clinicalguide.xml.CGParser.java
org.get.oxicam.clinicalguide.xml.CGStatsParser.java
org.get.oxicam.clinicalguide.xml.DateHelper.java
org.get.oxicam.clinicalguide.xml.FormGenerator.java
org.get.oxicam.clinicalguide.xml.ParserHelper.java
org.get.oxicam.clinicalguide.xml.StatsGenerator.java
org.get.oxicam.clinicalguide.xml.XMLHandler.java
org.get.oxicam.clinicalguide.xml.data.AbstractAnswer.java
org.get.oxicam.clinicalguide.xml.data.Annotation.java
org.get.oxicam.clinicalguide.xml.data.AnswerValidator.java
org.get.oxicam.clinicalguide.xml.data.Answer.java
org.get.oxicam.clinicalguide.xml.data.Assessment.java
org.get.oxicam.clinicalguide.xml.data.CombinedAnswer.java
org.get.oxicam.clinicalguide.xml.data.FollowUp.java
org.get.oxicam.clinicalguide.xml.data.FormQuery.java
org.get.oxicam.clinicalguide.xml.data.Info.java
org.get.oxicam.clinicalguide.xml.data.Option.java
org.get.oxicam.clinicalguide.xml.data.PatientAttribute.java
org.get.oxicam.clinicalguide.xml.data.Question.java
org.get.oxicam.clinicalguide.xml.data.Questionnaire.java
org.get.oxicam.clinicalguide.xml.data.SimpleAnswer.java
org.get.oxicam.clinicalguide.xml.data.Symptom.java
org.get.oxicam.clinicalguide.xml.data.TreatmentAction.java
org.get.oxicam.clinicalguide.xml.data.Treatment.java
org.get.oxicam.clinicalguide.xml.data.User.java
org.get.oxicam.clinicalguide.xml.forms.FormCell.java
org.get.oxicam.clinicalguide.xml.forms.FormColumn.java
org.get.oxicam.clinicalguide.xml.forms.FormDuration.java
org.get.oxicam.clinicalguide.xml.forms.Form.java
org.get.oxicam.clinicalguide.xml.query.QueryHelper.java
org.get.oxicam.clinicalguide.xml.query.QueryResultCell.java
org.get.oxicam.clinicalguide.xml.query.QueryResultRow.java
org.get.oxicam.clinicalguide.xml.query.QueryResultTable.java
org.get.oxicam.clinicalguide.xml.stats.AbstractStatsQuestion.java
org.get.oxicam.clinicalguide.xml.stats.StatsAnswerHolder.java
org.get.oxicam.clinicalguide.xml.stats.StatsColumnCompare.java
org.get.oxicam.clinicalguide.xml.stats.StatsComparatorOperator.java
org.get.oxicam.clinicalguide.xml.stats.StatsCompareConstraint.java
org.get.oxicam.clinicalguide.xml.stats.StatsConstraint.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionAverage.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionCount.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionExtrema.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionFactory.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionList.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionPercentage.java
org.get.oxicam.clinicalguide.xml.stats.StatsQuestionRatio.java
org.get.oxicam.clinicalguide.xml.stats.StatsSubject.java
org.get.oxicam.clinicalguide.xml.stats.StatsTimespan.java
org.get.oxicam.clinicalguide.xml.stats.Stats.java