使用Crypto JS在Java和Dycrypt中加密使用Crypto JS无法正常工作

时间:2017-05-08 04:06:55

标签: javascript java encryption cryptojs

我是Java和JS的新手,并试图在java中加密密码,这应该由我现有的JS代码解密。 (不想改变我的JS!)

我认为它与KEY和IV有关,我完全不知道。

** JAVA PROGRAM **

public class Helper {

public Cipher dcipher, ecipher;

// Responsible for setting, initializing this object's encrypter and
// decrypter Chipher instances
public Helper(String passPhrase) {

    // 8-bytes Salt
    byte[] salt = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x34, (byte) 0xE3, (byte) 0x03};

    // Iteration count
    int iterationCount = 19;

    try {
        // Generate a temporary key. In practice, you would save this key
        // Encrypting with DES Using a Pass Phrase
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);

        ecipher = Cipher.getInstance(key.getAlgorithm());

        // Prepare the parameters to the cipthers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);            

    } catch (InvalidAlgorithmParameterException e) {
        System.out.println("EXCEPTION: InvalidAlgorithmParameterException");
    } catch (InvalidKeySpecException e) {
        System.out.println("EXCEPTION: InvalidKeySpecException");
    } catch (NoSuchPaddingException e) {
        System.out.println("EXCEPTION: NoSuchPaddingException");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("EXCEPTION: NoSuchAlgorithmException");
    } catch (InvalidKeyException e) {
        System.out.println("EXCEPTION: InvalidKeyException");
    }
}

// Encrpt Password
@SuppressWarnings("unused")
public String encrypt(String str) {
    try {
        // Encode the string into bytes using utf-8
        byte[] utf8 = str.getBytes("UTF8");
        System.out.println("\n UTF8 : " + utf8);
        // Encrypt
        byte[] enc = ecipher.doFinal(utf8);
        System.out.println("\n enc: " + enc);
        // Encode bytes to base64 to get a string
        return new sun.misc.BASE64Encoder().encode(enc);

    } catch (BadPaddingException e) {
    } catch (IllegalBlockSizeException e) {
    } catch (UnsupportedEncodingException e) {
    }
    return null;
}


public static void main(String[] args) {
    try {

        Helper encrypter = new Helper("");

        System.out.print("Enter a password : ");
        String password = input.nextLine();

        String encrypted = encrypter.encrypt(password);
        System.out.println("encrypted String:" + encrypted);
    } catch (Exception e) {
    }

}

}

上面的程序应该加密密钥 - 将通过以下JS解密:

var encryptedpassword=this.bodyParams.password;

        var bytes  = CryptoJS.AES.decrypt(encryptedpassword.toString(), accKey);
        var newpassword = bytes.toString(CryptoJS.enc.Utf8);

WHERE accKey =“ Nqnzu3RhCJ1h8ql5fdKOaKUAbsuURze ********* _

0 个答案:

没有答案