|
Has anybody used cryptography inside James? I am trying to
do so (the JCE bundled with J2SE 1.4.2) from a mailet. If I invoke THE SAME
class (please see attached sample) from the command line it works fine, but if
I run inside James I always get the exception: “Algorithm
PBEWithMD5AndDES not available”. I think this must do with the java permissions and policies,
but if have tried the following without any success: -
run phoenix without security manager -
edit the java.policy
file contained in phoenix-loader.jar in order to add
specific permissions -
edit James’ environment.xml file to add the following permissions: <server> ��� <policy> ����� <grant
code-base="file:${java.home}${/}jre${/}lib${/}ext${/}-"> ��������
<permission class="java.security.AllPermission"
/> ����� </grant> �����
<grant code-base="file:${java.home}${/}..${/}lib${/}-"> ��������
<permission class="java.security.AllPermission"
/> �����
</grant> �����
<grant code-base="file:${java.home}${/}lib${/}ext${/}-"> ��������
<permission class="java.security.AllPermission"
/> �����
</grant> �����
<grant code-base="file:${app.home}${/}-"> ��������
<permission class="java.security.AllPermission"
/> ����� </grant> ��� </policy> […] Any help & ideas are welcome, thank you in advance! Isaac. |
import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.util.*;
// This is for BASE64 encoding and decoding
import sun.misc.*;
public class CryptoHelper {
private static int ITERATIONS = 1000;
private static void usage() {
System.err.println("Usage: java CryptoHelper -e|-d password text");
System.exit(1);
}
public static void main(String[] args)
throws Exception {
// Security.addProvider(new sun.security.provider.Sun());
if (args.length != 3) usage();
char[] password = args[1].toCharArray();
String text = args[2];
String output = null;
// Check the first argument: are we encrypting or decrypting?
if ("-e".equals(args[0])) output = encrypt(password, text);
else if ("-d".equals(args[0])) output = decrypt(password, text);
else usage();
System.out.println(output);
}
public static String encrypt(char[] password, String plaintext)
throws Exception {
// Begin by creating a random salt of 64 bits (8 bytes)
byte[] salt = new byte[8];
Random random = new Random();
random.nextBytes(salt);
// Create the PBEKeySpec with the given password
PBEKeySpec keySpec = new PBEKeySpec(password);
// Get a SecretKeyFactory for PBEWithSHAAndTwofish
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
// Create our key
SecretKey key = keyFactory.generateSecret(keySpec);
// Now create a parameter spec for our salt and iterations
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
// Create a cipher and initialize it for encrypting
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
String saltString = encoder.encode(salt);
String ciphertextString = encoder.encode(ciphertext);
return saltString+ciphertextString;
}
public static String decrypt(char[] password, String text)
throws Exception {
// Below we split the text into salt and text strings.
String salt = text.substring(0,12);
String ciphertext = text.substring(12,text.length());
// BASE64Decode the bytes for the salt and the ciphertext
BASE64Decoder decoder = new BASE64Decoder();
byte[] saltArray = decoder.decodeBuffer(salt);
byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);
// Create the PBEKeySpec with the given password
PBEKeySpec keySpec = new PBEKeySpec(password);
// Get a SecretKeyFactory for PBEWithSHAAndTwofish
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
// Create our key
SecretKey key = keyFactory.generateSecret(keySpec);
// Now create a parameter spec for our salt and iterations
PBEParameterSpec paramSpec =
new PBEParameterSpec(saltArray, ITERATIONS);
// Create a cipher and initialize it for encrypting
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
// Perform the actual decryption
byte[] plaintextArray = cipher.doFinal(ciphertextArray);
return new String(plaintextArray);
}
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
