Javascript 如何在php中以加密的形式将表单数据发送到服务器?
我想在另一个页面上以加密数据的形式发送表单的输入数据。 如何在PHP或JavaScript中实现这一点。 e、 g我们的用户名和密码为sunps和1234, 我想把它发送到服务器上,就像Javascript 如何在php中以加密的形式将表单数据发送到服务器?,javascript,php,Javascript,Php,我想在另一个页面上以加密数据的形式发送表单的输入数据。 如何在PHP或JavaScript中实现这一点。 e、 g我们的用户名和密码为sunps和1234, 我想把它发送到服务器上,就像 http://localhost/suraj/auth.php?user=xjhgwdb&pass=hjfgdsjg 如果您想将加密的数据发送到网站(localhost更难设置),只需在您的网站上使用HTTPS,让浏览器和服务器为您处理一切。您似乎将发送未加密的数据,php脚本将使其未加密,但在网络上
http://localhost/suraj/auth.php?user=xjhgwdb&pass=hjfgdsjg
如果您想将加密的数据发送到网站(localhost更难设置),只需在您的网站上使用HTTPS,让浏览器和服务器为您处理一切。您似乎将发送未加密的数据,php脚本将使其未加密,但在网络上,数据将使用SSL加密
有关详细信息,请参阅或您可以使用此密码类加密字符串 将此示例用于my类来加密和解密字符串:
public static void main(String[] args){
// Generate a key-pair
KeyPair keyPair = BaseCrypt.generateKeyPair();
byte[] publicKey = BaseCrypt.generatePublicKey(keyPair);
byte[] privateKey = BaseCrypt.generatePrivateKey(keyPair);
byte[] dataBytes =
"J2EE Security for Servlets, EJBs and Web Services".getBytes();
byte[] encBytes = null;
byte[] decBytes = null;
try {
encBytes = BaseCrypt.encrypt(dataBytes, publicKey);
decBytes = BaseCrypt.decrypt(encBytes, privateKey);
} catch (Exception ex) {
Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
}
boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!")+": ");
System.out.println(new String(decBytes));
}
BaseCrypt类:
public class BaseCrypt {
public static byte[] encrypt(byte[] inpBytes, byte[] key) throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA"); // or "EC" or whatever
//PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
PublicKey publicKey = kf.generatePublic(new X509EncodedKeySpec(key));
String xForm = "RSA/ECB/PKCS1Padding";
Cipher cipher = Cipher.getInstance(xForm);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(inpBytes);
}
public static byte[] decrypt(byte[] inpBytes, byte[] key) throws Exception{
KeyFactory kf = KeyFactory.getInstance("RSA"); // or "EC" or whatever
PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(key));
String xForm = "RSA/ECB/PKCS1Padding";
Cipher cipher = Cipher.getInstance(xForm);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(inpBytes);
}
public static KeyPair generateKeyPair(){
KeyPairGenerator kpg = null;
try {
kpg = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(MainClass.class.getName()).log(Level.SEVERE, null, ex);
}
kpg.initialize(1000); // The size of the key
KeyPair kp = kpg.generateKeyPair();
return kp;
}
public static byte[] generatePublicKey(KeyPair keyPair){
PublicKey key = keyPair.getPublic();
return key.getEncoded();
}
public static byte[] generatePrivateKey(KeyPair keyPair){
PrivateKey key = keyPair.getPrivate();
return key.getEncoded();
}
}
最佳做法是永远不要在url中发送密码,对于用户名,您可以使用base64\u编码,您可以将表单作为post提交,这样它就不会显示在url上。请在
post
请求中发送此数据。在GET
中发送此类敏感数据非常危险。@TarangP base64\u Ecode不会加密数据。它几乎不编码它们(从它的名字可以明显看出),并且没有添加任何安全性。请参阅将加密字符串发送到php您可以使用POST方法,如果您需要如何将数据发送到php服务器,请阅读本文