Re: Simple encryption/decryption
Arne Vajh?j wrote:
Mark Space wrote:
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.
Thanks for that, I'll play around with it. I read the docs and thought
I was supposed to use a SecretKeyFactory. The SecretKeySpec class
looked like something else (a "specification" for the key).
Just for reference, the following fails on line 34 (marked):
public class Encryption {
Cipher c;
public Encryption( String password )
throws NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeySpecException,
InvalidKeyException
{
c = Cipher.getInstance( "RC4" );
char[] passChars = new char[password.length()];
password.getChars( 0, passChars.length, passChars, 0 );
// line 34 below
SecretKeyFactory skf = SecretKeyFactory.getInstance( "ARCFOUR" );
SecretKey sk = skf.generateSecret( new PBEKeySpec( passChars ) );
c.init( Cipher.ENCRYPT_MODE, sk );
}
public static void main ( String... args )
throws Exception
{
System.out.println( new Encryption("password1234") );
}
}