使用Crypto JS加密Java和使用Crypto JS加密Crycryo不使用

问题描述:

我是Java和JS中的新手,并尝试在Java中加密密码,这应该由我现有的JS代码进行解密。 (不想改变我的JS!)使用Crypto JS加密Java和使用Crypto JS加密Crycryo不使用

我认为它与KEY和IV有关,我完全没有意识到。

** Java程序**

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 ********* _

+1

这些加密问题的9/10倍是由JS的unicode字符串和其他人的非Unicode字符串造成的...... – dandavis

+0

你能建议该怎么做。我对这两件事都是新的,所以在这里需要一点帮助。 –

+1

我想建议您不要解密密码,而是使用相同的密钥(或盐)加密传入的密码并比较两种加密。让密码成为提供密码的人的超级秘密。 :D – DevilsHnd

您的问题是您使用DES进行加密并使用AES进行解密。

此外,您正在从Java代码的密码生成密钥,但直接在您的JavaScript代码上使用它。

您在Java端使用salt,但似乎并没有在消息中加入salt。使用salt +密码短语可以恢复密钥和iv。

您将需要查找另一组使用AES两端的示例,它们以相同的方式生成密钥,并使用相同的填充。沿此线

东西:

// Generate a temporary key. In practice, you would save this key 
// Encrypting with AES Using a Pass Phrase 
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, 100, 128); 
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); 
SecretKey aesKey = keyFactory.generateSecret(keySpec); 
ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 

// Prepare the parameters to the cipthers 
IvParameterSpec ivParameterSpec = new IvParameterSpec(aesKey.getEncoded()); 
ecipher.init(Cipher.ENCRYPT_MODE, aesKey, ivParameterSpec); 

您还需要考虑TLS通信刚刚在评论中提到一个人,因为它是相当困难的,以确保在JS侧对称加密密钥/密码。