Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple encryption/decryption

Reply
Thread Tools

Simple encryption/decryption

 
 
angelochen960@gmail.com
Guest
Posts: n/a
 
      03-23-2009
I'm looking for a simple solution: a way to encrypt a string into a
alphanumeric string, and can be decrypted back to its original form,
using a string as the key, say 'password123', any idea how to achieve
this? Thanks.
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      03-23-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I'm looking for a simple solution: a way to encrypt a string into a
> alphanumeric string, and can be decrypted back to its original form,
> using a string as the key, say 'password123', any idea how to achieve
> this? Thanks.



Here's a little something to get you started. I'll see if I can muddle
through an example soonish.

<http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html>

 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-23-2009
(E-Mail Removed) wrote:
> I'm looking for a simple solution: a way to encrypt a string into a
> alphanumeric string, and can be decrypted back to its original form,
> using a string as the key, say 'password123', any idea how to achieve
> this? Thanks.


Well, there's always ROT-13. For extra security, you might want to
perform it twice.

If you're trying to store passwords, it's generally advisable to store a
hash of the password. For example, my Linux password is stored as the
following:
$1$exPy4JWs$Fp5p5vjY6b0P0m1HzSTxu.

The $1 indicates that this is an MD5-encrypted password
The $exPy4JWs$ indicates that exPy4JWs is the salt
The rest is the actual hash, in base64-form.

P.S., if you could tell me what the password is, please do! I've
forgotten what I made it...
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      03-23-2009
(E-Mail Removed) wrote:

> I'm looking for a simple solution: a way to encrypt a string into a
> alphanumeric string, and can be decrypted back to its original form,
> using a string as the key, say 'password123', any idea how to achieve
> this? Thanks.



I'm not seeing a good answer to this. In particular, the
SecretKeyFactory.getInstance( "ARCFOUR" ) method returns an error. I
don't see a simple way to use RC4 without this call succeeding. Anyone
got a solution?

Anyway, to the OP, check out the code below. It will at least get you
going. Sorry for the lack of Javadoc comments, I just had this laying
around on the hard drive. You can compare this code to the description
of RC4 at Wikipedia. It's the exact same algorithm. Note the test vectors.

<http://en.wikipedia.org/wiki/RC4>


package local;

/**

COPYRIGHT 2009 Brenden Towey

This code is donated to the public domain but is unsupported and has no
warranty whatsoever. Use at your own risk.

*
* @author Brenden
*/
public class Rc4
{
private byte[] state = new byte[256];
int x, y;

public Rc4( byte[] key )
{
// if( key.length<5||key.length>16 )
// {
// throw new IllegalArgumentException( "Key: "+key+
// " must be between 40 and 128 bits. (key length is "+
// key.length+".)" );
// }
for( int i = 0; i < state.length; i++ ) {
state[i] = (byte) i;
}
for( int i = 0; i < state.length; i++ ) {
x = (x + key[i % key.length] + state[i]) & 0xFF;
byte swap = state[i];
state[i] = state[x];
state[x] = swap;
}
x = 0;
}

public byte[] crypt( byte[] input )
{
byte[] output = new byte[input.length];
for( int i = 0; i < input.length; i++ ) {
x = (x + 1) % 256;
y = (state[x] + y) & 0xFF;
byte swap = state[x];
state[x] = state[y];
state[y] = swap;
byte r = state[(state[x] + state[y]) & 0xFF];
output[i] = (byte) (input[i] ^ r);
}
return output;
}

public static void main( final String... args )
{
String[][] testVectors = {{"Key", "Plaintext"},
{"Wiki", "pedia"}, {"Secret", "Attack at dawn"}
};
for( String[] s : testVectors ) {
System.out.printf( "RC4( \"%s\", \"%s\" ) == ", s[0], s[1] );
System.out.println( hexString( new Rc4( s[0].getBytes()
).crypt( s[1].
getBytes() ) ) );
}
}

private static String hexString( byte[] bytes )
{
StringBuilder sb = new StringBuilder( bytes.length *2 );
for( int i = 0; i < bytes.length; i++ ) {
sb.append( String.format( "%02X", bytes[i] ));
}
return sb.toString();
// return local.utils.ArrayUtils.toFormattedString( "%02X",
//bytes, null );
}
}
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-23-2009
On Mon, 23 Mar 2009 07:50:59 -0700 (PDT), "(E-Mail Removed)"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>I'm looking for a simple solution: a way to encrypt a string into a
>alphanumeric string, and can be decrypted back to its original form,
>using a string as the key, say 'password123', any idea how to achieve
>this? Thanks.


Sample code for a JCE solution is posted at
http://mindprod.com/jgloss/aes.html
Follow the links for others.


Sample code for a non-JCE solution, using public encryption is posted
at http://mindprod.com/jgloss/products1.html#TRANSPORTER
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Nature provides a free lunch, but only if we control our appetites."
~ William Ruckelshaus, America’s first head of the EPA
 
Reply With Quote
 
Arne VajhĂžj
Guest
Posts: n/a
 
      03-23-2009
rossum wrote:
> IIRC, Base-64 is not part of standard Java, though there is an
> undocumented version in sun.misc. There are a lot of implementations
> floating about on the internet; I use Bouncy Castle, which has Base-64
> as part of their general crypto package.


Base64 is part of the Java EE spec. It is under the mail stuff.

Arne
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-23-2009
rossum wrote:
>> IIRC,


You do not.

>> Base-64 is not part of standard Java, though there is an
>> undocumented version in sun.misc. There are a lot of implementations


Sun warns us not to use the sun.misc packages.

>> floating about on the internet; I use Bouncy Castle, which has Base-64
>> as part of their general crypto package.


Apache commons-codec has it also. Besides, as
Arne VajhĂžj wrote:
> Base64 is part of the Java EE spec. It is under the mail stuff.


<http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeUtility.html#decode(java.io.InputStream,%20jav a.lang.String)>
<http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeUtility.html#encode(java.io.OutputStream,%20ja va.lang.String)>

--
Lew
 
Reply With Quote
 
Arne VajhĂžj
Guest
Posts: n/a
 
      03-23-2009
Lew wrote:
> rossum wrote:
>>> Base-64 is not part of standard Java, though there is an
>>> undocumented version in sun.misc. There are a lot of implementations


>>> floating about on the internet; I use Bouncy Castle, which has Base-64
>>> as part of their general crypto package.

>
> Apache commons-codec has it also. Besides, as


> Arne VajhĂžj wrote:
>> Base64 is part of the Java EE spec. It is under the mail stuff.

>
> <http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeUtility.html#decode(java.io.InputStream,%20jav a.lang.String)>
> <http://java.sun.com/javaee/5/docs/api/javax/mail/internet/MimeUtility.html#encode(java.io.OutputStream,%20ja va.lang.String)>


The code is simple:

public static String b64encode(byte[] b) throws MessagingException,
IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = MimeUtility.encode(baos, "base64");
b64os.write(b);
b64os.close();
return new String(baos.toByteArray());
}
public static byte[] b64decode(String s) throws
MessagingException, IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
InputStream b64is = MimeUtility.decode(bais, "Base64");
byte[] tmp = new byte[s.length()];
int n = b64is.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return res;
}

and if it is in Java EE context the classes are there - if it is in
pure Java SE context then getting JavaMail and put that jar in classpath
is enough.

Arne
 
Reply With Quote
 
Arne Vajhűj
Guest
Posts: n/a
 
      03-24-2009
Mark Space wrote:
> (E-Mail Removed) wrote:
>> I'm looking for a simple solution: a way to encrypt a string into a
>> alphanumeric string, and can be decrypted back to its original form,
>> using a string as the key, say 'password123', any idea how to achieve
>> this? Thanks.

>
> I'm not seeing a good answer to this. In particular, the
> SecretKeyFactory.getInstance( "ARCFOUR" ) method returns an error. I
> don't see a simple way to use RC4 without this call succeeding. Anyone
> got a solution?


private Cipher rc4;
private SecretKey rc4key;
public RC4(String key) {
try {
rc4 = Cipher.getInstance("RC4");
rc4key = new SecretKeySpec(key.getBytes(), "RC4");
} catch(Exception e) {
e.printStackTrace();
}
}

works fine for me.

Arne
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-24-2009
On Mon, 23 Mar 2009 21:17:41 +0000, rossum <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>Others have talked about encryption - RC4 etc. Using a cypher will
>produce an array, or stream, of bytes. In order to get those bytes
>into alphanumeric form you will need to use Base-64 (or similar) to
>code those bytes as alphanumeric characters.


see http://mindprod.com/jgloss/base64.html
and
http://mindprod.com/jgloss/armouring.html

The Transporter code I pointed you to does armouring using Base64
..
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Nature provides a free lunch, but only if we control our appetites."
~ William Ruckelshaus, America’s first head of the EPA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple VB.Net / webservice requirement (but not simple for me....) Dave E ASP .Net 7 01-11-2006 02:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM
A few simple problems in a simple program. jmac@berkeley.edu C Programming 7 07-23-2003 09:51 PM



Advertisments