encrypt PBEWithMD5AndDES in j2me

旧街凉风 提交于 2019-12-22 01:43:34

问题


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.



来源:https://stackoverflow.com/questions/4060515/encrypt-pbewithmd5anddes-in-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!