a simple encryption example

Dear,

      May you show me the code how to use a string "fyp99" to encrypt another string "00" and then put the result into a file?Thanks!
LVL 2
hpchong7Asked:
Who is Participating?
 
swwongConnect With a Mentor Commented:
Okay, here I've got an example for you, which uses RC4 for encryption and decryption. But you should download a package called "cryptix" first, it's free.

import java.io.*;
import java.security.*;
import cryptix.provider.*;

public class encrypt      {
      
      cryptix.provider.cipher.RC4 algorithm = new cryptix.provider.cipher.RC4();
      java.security.Key key;
      
      public static void main(String args[])      {
            
            String keystr = new String("This is the key.");
            byte[] bytes = {65,66,67,68,69,70};
            
            encrypt rc4 = new encrypt();
            
            rc4.setKey(keystr);
            
            System.out.println("Encrypt: " + new String(bytes));
            bytes = rc4.RC4Encrypt(bytes);
            System.out.println("Decrypt: " + new String(rc4.RC4Decrypt(bytes)));            
      }
      
      public void setKey (String symkey)      {
            try      {
                  toKey(cryptix.util.core.Hex.fromString(symkey));      
            }
            catch (Exception e)      {}
      }
      
      private void toKey (byte[] keybyte)      {
            try      {
                  cryptix.provider.key.RC4KeyGenerator rc4key = new cryptix.provider.key.RC4KeyGenerator();
                  
                  do      {
                        key = rc4key.generateKey(keybyte);
                  }while((new String(key.getEncoded()).length())==0);
            }
            catch (WeakKeyException e)      {
                  e.printStackTrace();
            }
            catch (InvalidKeyException e)      {
                  e.printStackTrace();
            }
      }
      
      public byte[] RC4Encrypt (byte[] inbyte)      {
            try      {
                  algorithm.initEncrypt(key);            
            }
            catch (Exception e)      {
                  e.printStackTrace();                  
            }            
            return algorithm.crypt(inbyte);
      }

      public byte[] RC4Decrypt (byte[] inbyte)      {
            try      {
                  algorithm.initDecrypt(key);            
            }
            catch(Exception e)      {
                  e.printStackTrace();
            }            
            return algorithm.crypt(inbyte);
      }                  
}

You can mail me at swwong38@hotmail.com for sending you the cryptix package.      
0
 
hpchong7Author Commented:
ie.Use "fyp99" as an symmetric key.Thanks!
0
 
diakovCommented:
For what purpose do you want to encrypt? Depending on your goal, the scheme may vary.

Cheers,
  Nik
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
hpchong7Author Commented:
For login checking:use the password enter by user to encrypt string "00" to see if the result is matched.
0
 
Ash_Commented:
For login checking, it is also possible to take a digest of the password "00" and compare it with the password entered. Try any of the digest algorithm's such as MD5,SHA,etc.. I think Sun's JDK includes a SHA implementation ...
0
 
hpchong7Author Commented:
I've thought about it.However,I want encryption rather than message digest.
0
 
swwongCommented:
The following is a simple code for generating a digest by using the MD5 algorithm. And you could use the hash string for comparing the password.

import java.security.*;
import java.io.*;
import java.util.*;

public class encrypt      {
      
      public static void main(String args[])      {
            String message = new String("fyp99");
            System.out.println(message);
            System.out.println(encrypt.Digest(message));
      }
      
      public static String Digest(String message)      {
            String hashcode = new String();
            MessageDigest md;
            
            try      {
                  md=MessageDigest.getInstance("MD5");
                  
                  StringBufferInputStream ins=new StringBufferInputStream(message);                  
                  DigestInputStream d=new DigestInputStream(ins,md);
                  
                  int check;
                  
                  while((check=d.read())!=-1);                  
                  md.digest();

                  StringTokenizer token=new StringTokenizer(md.toString(),"<>");
                  token.nextToken();
                  hashcode=token.nextToken();
            }            
            catch (NoSuchAlgorithmException e)      {
                   e.printStackTrace();                        
            }
            catch(IOException e)      {
                   e.printStackTrace();      
            }      
            return hashcode;
      }
}
0
 
swwongCommented:
The following is a simple code for generating a digest by using the MD5 algorithm. And you could use the hash string for comparing the password.

import java.security.*;
import java.io.*;
import java.util.*;

public class encrypt      {
      
      public static void main(String args[])      {
            String message = new String("fyp99");
            System.out.println(message);
            System.out.println(encrypt.Digest(message));
      }
      
      public static String Digest(String message)      {
            String hashcode = new String();
            MessageDigest md;
            
            try      {
                  md=MessageDigest.getInstance("MD5");
                  
                  StringBufferInputStream ins=new StringBufferInputStream(message);                  
                  DigestInputStream d=new DigestInputStream(ins,md);
                  
                  int check;
                  
                  while((check=d.read())!=-1);                  
                  md.digest();

                  StringTokenizer token=new StringTokenizer(md.toString(),"<>");
                  token.nextToken();
                  hashcode=token.nextToken();
            }            
            catch (NoSuchAlgorithmException e)      {
                   e.printStackTrace();                        
            }
            catch(IOException e)      {
                   e.printStackTrace();      
            }      
            return hashcode;
      }
}
0
 
hpchong7Author Commented:
thanks for your code.However,as I said before,I want encryption result rather than message digest.Thanks!
0
 
Ash_Commented:
You can get the cryptix package at

www.cryptix.org
0
 
hpchong7Author Commented:
I got the cryptix package finally.But how to use it and set the classpath?Thanks!
0
 
swwongCommented:
Suppose you've unzipped the cryptix package and put it under the path C:\javalib\cryptix\. Then you would find a file called "cryptix.jar" in the above path. Now set your classpath under your DOS-Prompt

set CLASSPATH = %CLASSPATH%;C:\javalib\cryptix\cryptix.jar

then you could refer the classes through import cryptix.*;

0
 
hpchong7Author Commented:
thanks!BTW,Mr Wong,You program using RC4 seems not working.Have you run it before?Thanks!
0
 
hpchong7Author Commented:
thanks!
0
All Courses

From novice to tech pro — start learning today.