我需要获得两个函数。我想以 xml 格式将数据从我的网站传输到我的服务器。现在在我的服务器上,我想创建一个函数来加密数据并将其放入 xml 中,并使用 java 中的另一个函数来解密它。
请告诉我是否有任何预定义的功能,或者您能抽出 5 分钟时间吗?
最佳答案
嗯,您可以在 PHP 中使用任何加密 mcrypt
函数。 AES 128 加密示例:
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = "Put your secret key here";
$text = "<xml>This is your XML text</xml>";
//encrypting now with RIJNDAEL 128 encryption.
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, $iv);
//Display encrypted content
echo $crypttext;
要解密,请使用此代码(我不是 Java 专业人士,因此可能存在一些错误):
package org.kamal.crypto;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
public class SimpleProtector
{
private static final String ALGORITHM = "AES";
private static final byte[] keyValue =
new byte[] { 'P', 'u', 't', ' ', 'Y', 'o', 'u', 'r', ' ', 'S', 'e', 'c', 'r', 'e', 't', ' ', 'K', 'e', 'y', '', 'H', 'e', 'r', 'e'};
public static String decrypt(String encryptedValue) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGORITHM);
// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
// key = keyFactory.generateSecret(new DESKeySpec(keyValue));
return key;
}
}
关于java - 我需要两个函数来加密 php 中的一些数据并在 java "correctly"中解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3589303/