Solved

a simple encryption example

Posted on 2000-03-24
14
421 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

838 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