简体   繁体   中英

how to send form data to server in encrypted form in php?

I want to send input data of form in encrypted data at another page. How to do this in PHP or JavaScript. eg we have username and password as sunps and 1234, and I want to send it on the server like

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

If you want to send the data encrypted to a website (localhost is harder to set up), simply use HTTPS on your website and let the browser and server handle everything for you. You will seemingly send the data unencrypted, the php script will get it unencrypted, but on the network the data will be encrypted using SSL.

For more info, see https://en.wikipedia.org/wiki/HTTPS#Security or https://blog.nexcess.net/2014/09/03/the-pros-and-cons-of-implementing-ssl-https/

You can use this Cipher Class to encrypt strings

Use this exemple with my class to encrypt and decrypt a string:

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

The BaseCrypt Class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM