The Artima Developer Community
Sponsored Link

Java Answers Forum
need help...I'm new.

9 replies on 1 page. Most recent reply: Apr 9, 2003 8:47 PM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
jeff

Posts: 7
Nickname: jeffcmd
Registered: Apr, 2003

need help...I'm new. Posted: Apr 8, 2003 1:07 PM
Reply to this message Reply
Advertisement
Hi, I am beginner. Can anyone tell me how to encrypt/encode a string (password/ID) in Java and later decrypt/decode it? Is there a method to do that? Thanks.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: need help...I'm new. Posted: Apr 8, 2003 2:16 PM
Reply to this message Reply
Do you mean real encryption, or just a little obfuscation?

This isn't really a beginner topic, but would want to look at the javax.crypto package in the API documentation.

jeff

Posts: 7
Nickname: jeffcmd
Registered: Apr, 2003

Re: need help...I'm new. Posted: Apr 8, 2003 2:49 PM
Reply to this message Reply
Thanks for the reply.
What i meant was that if i want to just encrypt a string, is there a possible simple way of doing it beside using complex encryption classes/packages?
What i found out is to break up the string into characters/bytes and perform some changes to each but how should the code be written?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: need help...I'm new. Posted: Apr 8, 2003 7:26 PM
Reply to this message Reply
As I asked before, do you want real encryption or just a little obfuscation?

To get a little obfuscation, you can XOR the things together (and throw in a little junk for good measure). This hardly qualifies as encryption, but it makes it a little hard to read with the human eye. If you want this, let me know and I'll post a little example.

You could also do a rot13 or the like, which is even weaker than the above, because most experienced programmers could immediately recogonise that it is rot13 (or a similar variant with shifting of values, etc.) just by looking at it.

To get something stronger you could use the RC4 algorithm. You can find textual descriptions for this thing easily on the net (with Google, etc.); these can be used to write the code that does it. You might even be able to find the thing implemented in Java already (I haven't searched).

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: need help...I'm new. Posted: Apr 9, 2003 6:39 AM
Reply to this message Reply
Why don't you try this piece of code and see whether it satisfies your needs??

  public static String encryptDecrypt(String Password) {
      int Counter,Length;
      String rString = "";
      String Key="?AK47rpg40#>!";
      Length = 0;
      for (Counter = 0; Counter < (Password.length()); Counter++)
          {
          rString = rString + (char)((int)Password.charAt(Counter) ^ (int)Key.charAt(Length));
          Length++;
          if (Length > (Key.length()-1))
             Length=0;
          }
        return rString;
 }

jeff

Posts: 7
Nickname: jeffcmd
Registered: Apr, 2003

Re: need help...I'm new. Posted: Apr 9, 2003 6:48 AM
Reply to this message Reply
I suppose perhaps i don't need the real encryption but just a little obfuscation as you mentioned. Thanks for the help.

jeff

Posts: 7
Nickname: jeffcmd
Registered: Apr, 2003

Re: need help...I'm new. Posted: Apr 9, 2003 7:07 AM
Reply to this message Reply
Hi Senthoorkumaran Punniamoorthy , thanks for the code, i would like to know if your code works for both encrypt and decrypt? If i want to encrypt the string, and later decrypt the string, can i use the same function?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: need help...I'm new. Posted: Apr 9, 2003 4:00 PM
Reply to this message Reply
Here is something similar, but it lets you specify the password and data. The same function is used for encryption and decryption. See the simpleTest() call to get an idea of how a string is encrypted and decrypted.

Keep in mind that this is a very simple scheme and is by no means secure.

public class SimpleCrypto
{
   static String transmogrify( String password, String data )
   {
      StringBuffer buffy = new StringBuffer();
 
      for( int i = 0; i < data.length(); i++ )
         buffy.append( (char)(password.charAt(i%password.length()) ^ data.charAt(i)) );
 
      return buffy.toString();
   }
   
   public static void main( String [] args )
   {
      simpleTest();
   }
 
   private static void simpleTest()
   {
      String password = "secret";
      String data = "This is a test of the emergency broad's caste system.";
 
      System.out.println( "Original data: " + data );
      
      String encrypted = transmogrify(password,data);
      System.out.println( "Encrypted data: " + encrypted );
      System.out.println( "Decrypted data: " + transmogrify(password,encrypted) );
   }
}

jeff

Posts: 7
Nickname: jeffcmd
Registered: Apr, 2003

Re: need help...I'm new. Posted: Apr 9, 2003 7:18 PM
Reply to this message Reply
Thanks matt.

I have another problem. Which is writing a code that can send a string/text using email. I have looked through some Java books and websites which provide samples on how to do it but they tend to import their own classes for their function calls. This results in many versions of the code. Can the code be made simple without complex class dependencies? I'm using java servlets by the way.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: need help...I'm new. Posted: Apr 9, 2003 8:47 PM
Reply to this message Reply
Yes you can use the same methog to encrypt and decrypt

Flat View: This topic has 9 replies on 1 page
Topic: Collection Previous Topic   Next Topic Topic: need help with some simple code.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use