Solved

a simple encryption example

Posted on 2000-03-24
14
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java imports not found 4 71
Eclipse Java import and method not resolved 4 132
import as existing maven project 3 77
Tomcat 9 + java 8 error while trying to deploy a war file 2 853
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
This video teaches viewers about errors in exception handling.
Suggested Courses

734 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