2017-05-08 38 views
0

我是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

回答

1

您的問題是您使用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側對稱加密密鑰/密碼。

相關問題