Tuesday, July 14, 2015

TripleDES Encryption and Decryption using Android and .NET

Here we using the TripleDES Encryption and Decryption code in Android

import org.apache.commons.codec.binary.Base64;

import java.security.MessageDigest;
import java.security.spec.KeySpec;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class TripleDES {

    String secretKey = "YOUR_KEY";
    public String _encrypt(String message) throws Exception {

        MessageDigest md = MessageDigest.getInstance("md5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        KeySpec keySpec = new DESedeKeySpec(keyBytes);
        SecretKey key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);

        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = message.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        String base64EncryptedString = new String(base64Bytes);

        return base64EncryptedString;
    }

    public String _decrypt(String encryptedText) throws Exception {

        byte[] message = Base64.decodeBase64(encryptedText.getBytes("utf-8"));

        MessageDigest md = MessageDigest.getInstance("md5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        KeySpec keySpec = new DESedeKeySpec(keyBytes);
        SecretKey key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        String result = new String(plainText, "UTF-8");

        return result;
    }

}



Then we using the same in .NET like this

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;

public class CryptorEngine
    {
        /// <summary>
        /// Encrypt a string using dual encryption method. Return a encrypted cipher Text
        /// </summary>
        /// <param name="toEncrypt">string to be encrypted</param>
        /// <param name="useHashing">use hashing? send to for extra secirity</param>
        /// <returns></returns>
        /// 

        static string key = "YOUR_KEY";
         
        public static string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            // Get the key from config file
            //string key = "YOUR_KEY";// (string)settingsReader.GetValue("SecurityKey", typeof(String));
            //System.Windows.Forms.MessageBox.Show(key);
            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        /// <summary>
        /// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
        /// </summary>
        /// <param name="cipherString">encrypted string</param>
        /// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
        /// <returns></returns>
        public static string Decrypt(string cipherString, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(cipherString);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            //Get your key from config file to open the lock!
            //string key = "YOUR_KEY"; //(string)settingsReader.GetValue("SecurityKey", typeof(String));

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            tdes.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
    }

Thursday, June 04, 2015

Setting Tabhost in Android


In the Layout

  <TabHost
         android:id="@android:id/tabhost"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_weight="1" >

         <LinearLayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:orientation="vertical" >

             <TabWidget                 android:id="@android:id/tabs"                 android:layout_width="match_parent"                 android:background="@color/app_color"                 android:layout_height="wrap_content" >
             </TabWidget>

             <FrameLayout                 android:id="@android:id/tabcontent"                 android:layout_width="match_parent"                 android:layout_height="match_parent" >
                 
                 
                 <LinearLayout                     android:id="@+id/tab2"                     android:layout_width="match_parent"                     android:layout_height="match_parent" >

                     <ScrollView                         android:id="@+id/scrollView1"                         android:layout_width="match_parent"                         android:layout_height="wrap_content"                          android:background="#ffffff">

                         <LinearLayout                             android:layout_width="match_parent"                             android:layout_height="match_parent"                             android:orientation="vertical"                              android:layout_marginTop="10dp"                             android:layout_marginBottom="10dp"                              >

                             <LinearLayout                                 android:layout_width="match_parent"                                 android:layout_height="wrap_content"                                 android:orientation="vertical"                                  android:padding="10dp"                                 >

                                 <TextView                                     android:id="@+id/textView1"                                     android:layout_width="wrap_content"                                     android:layout_height="wrap_content"                                     android:text="Address  :  "                                     android:textSize="@dimen/text_size_20" />
                                 
                                 <TextView                                     android:id="@+id/address"                                     android:layout_width="wrap_content"                                     android:layout_height="wrap_content"                                     android:text="Address here"                                     android:layout_marginRight="30dp"                                     android:textSize="@dimen/text_size_20"                                     android:layout_marginTop="5dp" />

                             </LinearLayout>
                             
                             <Viewandroid:layout_width="fill_parent"android:layout_height="1dp"android:background="#EEEEEE" />
                             
                             
                             
                             <LinearLayout                                 android:id="@+id/principal_linear"                                 android:layout_marginTop="5dp"                                 android:layout_width="match_parent"                                 android:layout_height="wrap_content"                                 android:orientation="vertical"                                  android:padding="10dp"                                  >

                                         <TextView                                             android:id="@+id/principal"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="Principal  :  ABC"                                              android:padding="2dp"                                             android:textSize="@dimen/text_size_20"/>

            
                             </LinearLayout>
                             
                               <Viewandroid:layout_width="fill_parent"android:layout_height="1dp"android:background="#EEEEEE" />
                             <LinearLayout                                 android:id="@+id/emial_linear"                                 android:layout_marginTop="5dp"                                 android:layout_width="match_parent"                                 android:layout_height="wrap_content"                                 android:orientation="vertical"                                  android:padding="10dp"                                 >

                                 <LinearLayout                                     android:layout_width="match_parent"                                     android:layout_height="match_parent"                                     android:orientation="horizontal"                                      android:weightSum="100">

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="80" >

                                         <TextView                                             android:id="@+id/email"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="Example@gmail.com"                                              android:padding="2dp"                                             android:textSize="@dimen/text_size_20"/>

                                     </LinearLayout>

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="20" >

                                         <TextView                                             android:id="@+id/email_send"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="Email"                                              android:paddingTop="3dp"                                             android:paddingBottom="3dp"                                             android:background="#5C5C5C"                                             android:textColor="#f0f0f0f0"                                             android:gravity="center_vertical|center_horizontal"                                             android:textSize="@dimen/text_size_20"/>

                                     </LinearLayout>
                                     
                                 </LinearLayout>

                             </LinearLayout>
                             
                     <Viewandroid:layout_width="fill_parent"android:layout_height="1dp"android:background="#EEEEEE" />        
                             <LinearLayout                                 android:id="@+id/land1_linear"                                 android:layout_marginTop="5dp"                                 android:layout_width="match_parent"                                 android:layout_height="wrap_content"                                 android:orientation="vertical"                                  android:padding="10dp"                                  >

                                 <LinearLayout                                     android:layout_width="match_parent"                                     android:layout_height="match_parent"                                     android:orientation="horizontal"                                      android:weightSum="100">

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="80" >

                                         <TextView                                             android:id="@+id/land_no"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="8888-888888888"                                              android:padding="2dp"                                             android:textSize="@dimen/text_size_20"/>

                                     </LinearLayout>

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="20" >

                                         <TextView                                             android:id="@+id/call_land_no"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="Call"                                              android:background="#5C5C5C"                                             android:textColor="#f0f0f0f0"                                             android:gravity="center_vertical|center_horizontal"                                             android:textSize="@dimen/text_size_20"                                             android:paddingTop="3dp"                                             android:paddingBottom="3dp"/>

                                     </LinearLayout>
                                     
                                 </LinearLayout>

                             </LinearLayout>
                            <Viewandroid:layout_width="fill_parent"android:layout_height="1dp"android:background="#EEEEEE" /> 
                             <LinearLayout                                 android:id="@+id/land2_linear"                                 android:layout_marginTop="5dp"                                 android:layout_width="match_parent"                                 android:layout_height="wrap_content"                                 android:orientation="vertical"                                  android:padding="10dp"                                  >

                                 <LinearLayout                                     android:layout_width="match_parent"                                     android:layout_height="match_parent"                                     android:orientation="horizontal"                                      android:weightSum="100">

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="80" >

                                         <TextView                                             android:id="@+id/land2_no"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="8888-888888"                                              android:padding="2dp"                                             android:textSize="@dimen/text_size_20"/>

                                     </LinearLayout>

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="20" >

                                         <TextView                                             android:id="@+id/call_land2_no"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="Call"                                              android:background="#5C5C5C"                                             android:textColor="#f0f0f0f0"                                             android:gravity="center_vertical|center_horizontal"                                             android:textSize="@dimen/text_size_20"                                             android:paddingTop="3dp"                                             android:paddingBottom="3dp"/>

                                     </LinearLayout>
                                     
                                 </LinearLayout>

                             </LinearLayout>
                             
                             <View          android:layout_width="fill_parent"          android:layout_height="1dp"          android:background="#EEEEEE" />
                             
                             
                             <LinearLayout                                 android:id="@+id/mobile1_linear"                                 android:layout_marginTop="5dp"                                 android:layout_width="match_parent"                                 android:layout_height="wrap_content"                                 android:orientation="vertical"                                  android:padding="10dp" >

                                 <LinearLayout                                     android:layout_width="match_parent"                                     android:layout_height="match_parent"                                     android:orientation="horizontal"                                      android:weightSum="100">

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="80" >

                                         <TextView                                             android:id="@+id/mobile1_no"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="8888-8888888"                                              android:padding="2dp"                                             android:textSize="@dimen/text_size_20"/>

                                     </LinearLayout>

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="20" >

                                         <TextView                                             android:id="@+id/call_mobile1_no"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="Call"                                              android:background="#5C5C5C"                                             android:textColor="#f0f0f0f0"                                             android:gravity="center_vertical|center_horizontal"                                             android:textSize="@dimen/text_size_20"                                             android:paddingTop="3dp"                                             android:paddingBottom="3dp"/>

                                     </LinearLayout>
                                     
                                 </LinearLayout>

                             </LinearLayout>
                             
                             <View          android:layout_width="fill_parent"          android:layout_height="1dp"          android:background="#EEEEEE" />
                             
                             
                             <LinearLayout                                 android:id="@+id/mobile2_linear"                                 android:layout_marginTop="5dp"                                 android:layout_width="match_parent"                                 android:layout_height="wrap_content"                                 android:orientation="vertical"                                  android:padding="10dp" >

                                 <LinearLayout                                     android:layout_width="match_parent"                                     android:layout_height="match_parent"                                     android:orientation="horizontal"                                      android:weightSum="100">

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="80" >

                                         <TextView                                             android:id="@+id/mobile2_no"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="8888-8888888"                                              android:padding="2dp"                                             android:textSize="@dimen/text_size_20"/>

                                     </LinearLayout>

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="20" >

                                         <TextView                                             android:id="@+id/call_mobile2_no"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="Call"                                              android:padding="2dp"                                             android:background="#5C5C5C"                                             android:textColor="#f0f0f0f0"                                             android:gravity="center_vertical|center_horizontal"                                             android:textSize="@dimen/text_size_20"                                             android:paddingTop="3dp"                                             android:paddingBottom="3dp"/>

                                     </LinearLayout>
                                     
                                 </LinearLayout>

                             </LinearLayout>
                             <Viewandroid:layout_width="fill_parent"android:layout_height="1dp"android:background="#EEEEEE" />
                             
                             <LinearLayout                                 android:id="@+id/url_linear"                                 android:layout_marginTop="5dp"                                 android:layout_width="match_parent"                                 android:layout_height="wrap_content"                                 android:orientation="vertical"                                  android:padding="10dp"                                  >

                                 <LinearLayout                                     android:layout_width="match_parent"                                     android:layout_height="match_parent"                                     android:orientation="horizontal"                                      android:weightSum="100">

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="80" >

                                         <TextView                                             android:id="@+id/url"                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="www.example.com"                                              android:padding="2dp"                                             android:textSize="@dimen/text_size_20"/>

                                     </LinearLayout>

                                     <LinearLayout                                         android:layout_width="0dp"                                         android:layout_height="match_parent"                                         android:orientation="vertical"                                         android:layout_weight="20" >

                                         <TextView                                             android:layout_width="match_parent"                                             android:layout_height="wrap_content"                                             android:text="Call"                                              android:padding="2dp"                                             android:background="#5C5C5C"                                             android:textColor="#f0f0f0f0"                                             android:gravity="center_vertical|center_horizontal"                                             android:textSize="@dimen/text_size_20"                                             android:paddingTop="3dp"                                             android:paddingBottom="3dp"/>

                                     </LinearLayout>
                                     
                                 </LinearLayout>

                             </LinearLayout>
                         
                         </LinearLayout>
                     </ScrollView>
                     
                 </LinearLayout>
                 
                 
                 

                 <LinearLayout                     android:id="@+id/tab1"                     android:layout_width="match_parent"                     android:layout_height="match_parent" >

                     <ScrollView       android:layout_width="fill_parent"       android:fillViewport="true"       android:background="@color/white"       android:layout_height="fill_parent">
                     
                         <LinearLayout                              android:layout_width="fill_parent"         android:background="@color/white"         android:layout_height="fill_parent"          android:orientation="vertical">
      <LinearLayout                              android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:background="#E8E8E8" >
         
          <TextView                           android:id="@+id/inst_name"                           android:layout_width="fill_parent"                           android:layout_height="wrap_content"                           android:padding="10dp"                           android:typeface="serif"                           android:text="Name"                            android:textSize="@dimen/text_size_15"                           android:textStyle="bold"                           android:textColor="#2E2E2E"/>
          
        </LinearLayout>   
                             <LinearLayout                              android:layout_width="fill_parent"         android:background="#F3F3F3"         android:layout_height="match_parent" >
           <WebView          android:id="@+id/textContent"          android:layout_width="fill_parent"          android:layout_height="match_parent"            />
                                  
         </LinearLayout>   
                           
                         </LinearLayout>
                         
                         
                         
                      
                      
                     </ScrollView>
                 </LinearLayout>

                 

       <!--       <LinearLayout                     android:id="@+id/tab3"                     android:layout_width="match_parent"                     android:padding="10dp"                     android:background="@color/gray"                     android:layout_height="match_parent" >                      <ScrollView        android:layout_width="fill_parent"        android:fillViewport="true"        android:background="@color/white"        android:layout_height="fill_parent">       <include           android:layout_width="fill_parent"           android:layout_height="wrap_content"           layout="@layout/location_layout" /></ScrollView>      </LinearLayout> -->             </FrameLayout>
         </LinearLayout>
     </TabHost>


In Java File

   TabHost tabHost = (TabHost)getView().findViewById(android.R.id.tabhost);
   tabHost.setup();

   TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
   TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
   TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
   
   firstTabSpec.setIndicator("About");
   secondTabSpec.setIndicator("Contact");
   thirdTabSpec.setIndicator("Location");
   
   firstTabSpec.setContent(R.id.tab1);
   secondTabSpec.setContent(R.id.tab2);
  // thirdTabSpec.setContent(R.id.tab3);      tabHost.addTab(firstTabSpec);
   tabHost.addTab(secondTabSpec);
 //  tabHost.addTab(thirdTabSpec);