Encrypt a hex string in java.

Posted by twintwins on Stack Overflow See other posts from Stack Overflow or by twintwins
Published on 2010-02-03T05:09:59Z Indexed on 2010/06/18 12:43 UTC
Read the original article Hit count: 367

Filed under:
|
|
|

I would like to ask for any suggestions about my problem. I need to encrypt a hexadecimal string. I must not to use the built-in functions of java because it doesn't work in my server. In short, I have to hard code an algorithm or any means of encrypting the message. Anyone who could help me with this? thanks a lot!

here is the code.

public Encrypt(SecretKey key, String algorithm) {

 try {
     ecipher = Cipher.getInstance(algorithm);
     dcipher = Cipher.getInstance(algorithm);
     ecipher.init(Cipher.ENCRYPT_MODE, key);
     dcipher.init(Cipher.DECRYPT_MODE, key);
 } catch (NoSuchPaddingException e) {
     System.out.println("EXCEPTION: NoSuchPaddingException");
 } catch (NoSuchAlgorithmException e) {
     System.out.println("EXCEPTION: NoSuchAlgorithmException");
 } catch (InvalidKeyException e) {
     System.out.println("EXCEPTION: InvalidKeyException");
 }
}

public void useSecretKey(String secretString) {


 try {
     SecretKey desKey       = KeyGenerator.getInstance("DES").generateKey();
     SecretKey blowfishKey  = KeyGenerator.getInstance("Blowfish").generateKey();
     SecretKey desedeKey    = KeyGenerator.getInstance("DESede").generateKey();

     Encrypt desEncrypter = new Encrypt(desKey, desKey.getAlgorithm());
     Encrypt blowfishEncrypter = new Encrypt(blowfishKey, blowfishKey.getAlgorithm());
     Encrypt desedeEncrypter = new Encrypt(desedeKey, desedeKey.getAlgorithm());

     desEncrypted       = desEncrypter.encrypt(secretString);
     blowfishEncrypted  = blowfishEncrypter.encrypt(secretString);
     desedeEncrypted    = desedeEncrypter.encrypt(secretString);
 } catch (NoSuchAlgorithmException e) {}
}

those are the methods i used. no problem if it is run as an application but then when i put it to my server which is the glassfish server an exception occured and it says no such algorithm.

© Stack Overflow or respective owner

Related posts about java

Related posts about encryption