encrypt PBEWithMD5AndDES in j2me

2019-04-17 06:46发布

问题:

i'm triing to get this code to work on j2me (it is working a java program) but not yet in j2me

    public static String generate(String plaintext, String passphase) throws Exception {
        try {
            PBEKeySpec pbeKeySpec = new PBEKeySpec(passphase.toCharArray());
            PBEParameterSpec pbeParamSpec;
            SecretKeyFactory keyFac;
            // Salt
            byte[] salt = {(byte) 0xc8, (byte) 0x73, (byte) 0x61, (byte) 0x1d, (byte) 0x1a, (byte) 0xf2, (byte) 0xa8, (byte) 0x99};
            // Iteration count
            int count = 20;
            // Create PBE parameter set
            pbeParamSpec = new PBEParameterSpec(salt, count);
            keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
            // Create PBE Cipher
            Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
            // Initialize PBE Cipher with key and parameters
            pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
            // Our cleartext
            byte[] cleartext = plaintext.getBytes();
            // Encrypt the cleartext
            byte[] ciphertext = pbeCipher.doFinal(cleartext);
            return ciphertext;
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

i found this lib http://www.bouncycastle.org/java.html

the important thing is that i find a method for j2me that can encrypt using PBEWithMD5AndDES

anyone know the solution?

edit adding extra info

when i try to add the above code to a mobile project following classes are not recognized (not included in j2me)

    PBEKeySpec
    PBEParameterSpec
    SecretKeyFactory

so i need a package that allows me to encode plain text using PBEWithMD5AndDES anyone know such a package compatible with j2me?

thx for the replies so far

回答1:

A lot can go wrong when applying a primitive, you should use Jasypt.