Tutorial for Cryptography in Android DES(YA Imam Reza)

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Tutorial for Cryptography in Android DES(YA Imam Reza)

Postby ababaei2000 » Wed Mar 12, 2008 7:11 am

Hi all
here is the code that i designed my self,i think it would be useful for encrypt important data or set a password for entering. 8)
here is the class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DES {
  2.  
  3.        
  4.  
  5.  
  6.  
  7.         private static Cipher pbeCipher;
  8.  
  9.         private static SecretKey pbeKey;
  10.  
  11.         public KeyGenerator kgen ;
  12.  
  13.         private static PBEKeySpec pbeKeySpec;
  14.  
  15.         private static PBEParameterSpec pbeParamSpec;
  16.  
  17.         private static SecretKeyFactory keyFac;
  18.  
  19.         byte[] ciphertext={} ;  
  20.  
  21.         byte[] salt = {
  22.  
  23.                         (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
  24.  
  25.                         (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
  26.  
  27.                         };
  28.  
  29.         int count=20;
  30.  
  31. public DES(){
  32.  
  33.        
  34.  
  35.         try{
  36.  
  37.          pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
  38.  
  39. }
  40.  
  41. catch(NoSuchAlgorithmException e){
  42.  
  43.         Log.e("Ya Reza", e.toString());}
  44.  
  45. catch(NoSuchPaddingException e){
  46.  
  47.         Log.e("Ya Reza", e.toString());}
  48.  
  49. }
  50.  
  51. public void setpass(String Key){
  52.  
  53.         pbeKeySpec = new PBEKeySpec(Key.toCharArray());
  54.  
  55.         try{
  56.  
  57.                 keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");}
  58.  
  59.         catch(NoSuchAlgorithmException e){
  60.  
  61.                 Log.e("Ya Reza", e.toString());}
  62.  
  63.        
  64.  
  65.         try{
  66.  
  67.                  pbeKey = keyFac.generateSecret(pbeKeySpec);}
  68.  
  69.                  catch(InvalidKeySpecException e){
  70.  
  71.                           Log.e("Ya Reza", e.toString());
  72.  
  73.                           }
  74.  
  75.        
  76.  
  77.         }
  78.  
  79. public String encrypt(String data)throws Exception{
  80.  
  81.        
  82.  
  83.        
  84.  
  85.         try{
  86.  
  87.           pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);}
  88.  
  89.          catch(InvalidKeyException e){
  90.  
  91.                   Log.e("Ya Reza", e.toString());
  92.  
  93.                   }
  94.  
  95.          catch(InvalidAlgorithmParameterException e){
  96.  
  97.                  Log.e("Ya Reza", e.toString());
  98.  
  99.          }
  100.  
  101.           try{
  102.  
  103.                   ciphertext= pbeCipher.doFinal(data.getBytes());}
  104.  
  105.           catch(IllegalBlockSizeException e){
  106.  
  107.                   Log.e("Ya Reza", e.toString());
  108.  
  109.                  
  110.  
  111.                   }
  112.  
  113.           catch(BadPaddingException e){
  114.  
  115.                   Log.e("Ya Reza", e.toString());
  116.  
  117.                  
  118.  
  119.                   }
  120.  
  121.  
  122.  
  123.           return new String(ciphertext);
  124.  
  125.  
  126.  
  127. public String decrypt(String data)throws Exception{
  128.  
  129.         try{
  130.  
  131.                   pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);}
  132.  
  133.                  catch(InvalidKeyException e){
  134.  
  135.                           Log.e("Ya Reza", e.toString());
  136.  
  137.                           }
  138.  
  139.                  catch(InvalidAlgorithmParameterException e){
  140.  
  141.                          Log.e("Ya Reza", e.toString());
  142.  
  143.                  }
  144.  
  145.                   try{
  146.  
  147.                           ciphertext= pbeCipher.doFinal(data.getBytes());}
  148.  
  149.                   catch(IllegalBlockSizeException e){
  150.  
  151.                           Log.e("Ya Reza", e.toString());
  152.  
  153.                          
  154.  
  155.                           }
  156.  
  157.                   catch(BadPaddingException e){
  158.  
  159.                           Log.e("Ya Reza", e.toString());
  160.  
  161.                          
  162.  
  163.                           }
  164.  
  165.           return new String(ciphertext);
  166.  
  167. }
  168.  
  169. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4

in main class you have to call the constructor and after it call setpass() function to send your Key.for encrypt and decrypot you just need to send your data,of course you have to know your data and your key must be String type.hope it would be benefit for you
Yours Sincerely
Armin
ababaei2000
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 12, 2008 9:19 am

Top

Postby venkat » Wed Mar 12, 2008 8:32 am

Hi Armin,

Thanks for posting your code here. this is what i am looking for.

Thanks :)
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby kimmin » Fri Oct 17, 2008 8:58 am

I have error when run your class because pbeParamSpec is not initial .
So I add "pbeParamSpec = new PBEParameterSpec(salt, count);" to the constructor but it does not work . It throws IllegalBlockSizeException error.

Please give an advice.
kimmin
Freshman
Freshman
 
Posts: 2
Joined: Mon Aug 04, 2008 4:05 am
Location: Vietnam

Postby ababaei2000 » Fri Oct 17, 2008 5:52 pm

Hey Kimmin
I think maybe the version of your android platfrom or eclipse would be diffrent,because i gave this program to all of my friend and they achieved results,I used android sdk m3-rc37a and eclips-java-europa-fall 2-win32
hope to be success
ababaei2000
Freshman
Freshman
 
Posts: 5
Joined: Tue Feb 12, 2008 9:19 am

Update to latet SDK

Postby Vimes » Sun Oct 19, 2008 2:37 am

Is everyone not using the following SDK now:

android-sdk-windows-1.0_r1

Anything developed before that has got to step up now, otherwise it is a waste of time posting obsolete code. Developer must do the migration to r1, as there are too many developers trying to learn Android and we need to develop/test against the current SDK/hardware.
User avatar
Vimes
Freshman
Freshman
 
Posts: 3
Joined: Sat Sep 13, 2008 4:20 am
Location: Vancouver, Canada

Postby kimmin » Sun Oct 19, 2008 4:28 am

Hi ababaei2000,

I use Android SDK 1.0 with Eclipse 3.3.2.

Thanks,
kimmin
Freshman
Freshman
 
Posts: 2
Joined: Mon Aug 04, 2008 4:05 am
Location: Vietnam

Top

Postby Vimes » Sun Oct 19, 2008 6:14 am

Perhaps I should have stated that anything posted prior to August 08 has potential issues with newer SDKs and that threads have been posted on migration from previous SDKs.

I do appreciate the efforts of everyone who posts code here, however until we all get comfortable with r1, there will be some potentially obsolete code floating around. Any assistance to help with any step-ups is also appreciated.
User avatar
Vimes
Freshman
Freshman
 
Posts: 3
Joined: Sat Sep 13, 2008 4:20 am
Location: Vancouver, Canada

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests