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 ********* _
這些加密問題的9/10倍是由JS的unicode字符串和其他人的非Unicode字符串造成的...... – dandavis
你能建議該怎麼做。我對這兩件事都是新的,所以在這裏需要一點幫助。 –
我想建議您不要解密密碼,而是使用相同的密鑰(或鹽)加密傳入的密碼並比較兩種加密。讓密碼成爲提供密碼的人的超級祕密。 :D – DevilsHnd