如何在PHP中以加密形式将表单数据发送到服务器?

时间:2018-02-08 06:44:32

标签: javascript php

我想在另一页的加密数据中发送表单的输入数据。 如何在PHP或JavaScript中执行此操作。 例如,我们有用户名和密码作为sunps和1234, 我想在服务器上发送它,如

http://localhost/suraj/auth.php?user=xjhgwdb&pass=hjfgdsjg

2 个答案:

答案 0 :(得分:0)

如果您想将加密的数据发送到网站(localhost更难设置),只需在您的网站上使用HTTPS,让浏览器和服务器为您处理所有内容。你似乎会发送未加密的数据,php脚本将不加密,但在网络上数据将使用SSL加密。

有关详细信息,请参阅https://en.wikipedia.org/wiki/HTTPS#Securityhttps://blog.nexcess.net/2014/09/03/the-pros-and-cons-of-implementing-ssl-https/

答案 1 :(得分:0)

您可以使用此密码类来加密字符串

将此示例与我的类一起使用来加密和解密字符串:

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();
}
}