Solved

a simple encryption example

Posted on 2000-03-24
14
395 Views
Last Modified: 2012-06-27
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!
0
Comment
Question by:hpchong7
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 2

Author Comment

by:hpchong7
ID: 2655981
ie.Use "fyp99" as an symmetric key.Thanks!
0
 
LVL 8

Expert Comment

by:diakov
ID: 2656203
For what purpose do you want to encrypt? Depending on your goal, the scheme may vary.

Cheers,
  Nik
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2656323
For login checking:use the password enter by user to encrypt string "00" to see if the result is matched.
0
 

Expert Comment

by:Ash_
ID: 2656737
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
 
LVL 2

Author Comment

by:hpchong7
ID: 2656856
I've thought about it.However,I want encryption rather than message digest.
0
 
LVL 1

Expert Comment

by:swwong
ID: 2656910
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
 
LVL 1

Expert Comment

by:swwong
ID: 2656916
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Author Comment

by:hpchong7
ID: 2657415
thanks for your code.However,as I said before,I want encryption result rather than message digest.Thanks!
0
 
LVL 1

Accepted Solution

by:
swwong earned 20 total points
ID: 2658532
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
 

Expert Comment

by:Ash_
ID: 2663909
You can get the cryptix package at

www.cryptix.org
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2681566
I got the cryptix package finally.But how to use it and set the classpath?Thanks!
0
 
LVL 1

Expert Comment

by:swwong
ID: 2682860
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
 
LVL 2

Author Comment

by:hpchong7
ID: 2683722
thanks!BTW,Mr Wong,You program using RC4 seems not working.Have you run it before?Thanks!
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2719082
thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now