Solved

a simple encryption example

Posted on 2000-03-24
14
414 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Jpanels and Jframe 8 33
session migration servlets 2 39
couple of eclipse 5 38
Way to decrease size of apk file 9 71
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

831 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