忘记密码时,通常的做法是向用户注册邮箱中发一个链接,用户打开这个链接来更改密码,这种做法的原理是什么呢,比如忘记谷歌账户,发送的连接是:
https://www.google.com/accounts/recovery/rp?a=AI_UWnnfbFQMfIZpmbjErd5vJYcCuqI2K9CCLBkEotmNnNUyl3gA36W1m7wVAmtGdDoCDjaD5ufe-fwEeCOxV3vxvvMug0nx3dpAwU0IWkzMzQ8X4b2NLiNxn62Bn4v41EtsSWtVPpxXngHO_mdGH6v3SyECFT3aArVCWd_1HNFbDl2o115w6GyJLo903NRSTjd33qv1is8dvWpNyS01emGUBVJqoEGo02u2f6Wfioan_AHS7gbr3qU
可以看出是向页面传入了一个参数a,这个参数是如何生成的?又该如何处理呢?请教!

解决方案 »

  1.   

    这个a里面应该包含了你的用户信息等一些东西,只是经过加密现在看不出来,但是服务器可以解析出里面的信息,然后就能确定是哪个账户忘记了密码需要修改,等等一些信息。
    亦或者这根本就是没什么具体作用的加密信息,服务器在发送给你的时候已经保存了什么账号需要修改密码,对应的a是多少,然后你点击链接就会传入a参数,服务器通过这个a去匹配找到对应的信息,然后让你修改密码等操作。
      

  2.   

    谢谢!我现在想用RSA来加密用户的邮箱地址,然后将密文作为链接的传入参数,可是我将密文转为字符得到的是乱码,怎样能像上面的链接那样都是可见字符呢?加密解密的程序如下:import java.security.InvalidKeyException;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;public class SecurityAlg { /**
     * encrypt with RSA algorithm
     * 
     * @param publickey
     * @param obj
     * @return
     */
    public byte[] encryptRSA(RSAPublicKey publickey, byte[] obj) {
    if (publickey != null) {
    Cipher cipher = null;
    try {
    cipher = Cipher.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (NoSuchPaddingException e) {
    e.printStackTrace();
    }
    try {
    cipher.init(Cipher.ENCRYPT_MODE, publickey);
    } catch (InvalidKeyException e) {
    e.printStackTrace();
    }
    try {
    return cipher.doFinal(obj);
    } catch (IllegalBlockSizeException e) {
    e.printStackTrace();
    } catch (BadPaddingException e) {
    e.printStackTrace();
    }
    }
    System.err.println("no given public key");
    return null;
    } /**
     * decrypt with RSA
     * 
     * @param privatekey
     * @param obj
     * @return
     */
    public byte[] decryptRSA(RSAPrivateKey privatekey, byte[] obj) {
    if (privatekey != null) {
    Cipher cipher = null;
    try {
    cipher = Cipher.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (NoSuchPaddingException e) {
    e.printStackTrace();
    }
    try {
    cipher.init(Cipher.DECRYPT_MODE, privatekey);
    } catch (InvalidKeyException e) {
    e.printStackTrace();
    }
    try {
    return cipher.doFinal(obj);
    } catch (IllegalBlockSizeException e) {
    e.printStackTrace();
    } catch (BadPaddingException e) {
    e.printStackTrace();
    }
    }
    System.err.println("no given private key");
    return null;
    } /**
     * translate byte encoded input to acill encoded String
     * 
     * @param input
     * @return
     */
    public String byte2AsciiString(byte[] b)
    {
    String stmp = "";
    char charArray[] = new char[b.length];

    for (int n = 0; n < b.length; n++) {
    charArray[n] = (char)(b[n]);
    }

    return new String(charArray);
    } /**
     * a demo for RSA algorithm
     * 
     * @param input
     *            : the text for encrypt
     */
    public void RSADemo(String input) {
    System.out.println("original code:\n" + input); KeyPairGenerator keyPairGen = null;
    try {
    keyPairGen = KeyPairGenerator.getInstance("RSA"); // set key size
    keyPairGen.initialize(512);
    KeyPair keypair = keyPairGen.generateKeyPair(); // generate key pairs
    RSAPrivateKey privatekey = (RSAPrivateKey) keypair.getPrivate();
    RSAPublicKey publickey = (RSAPublicKey) keypair.getPublic(); byte[] encryptResult = this.encryptRSA(publickey, input.getBytes());
    System.out.println("\nafter RSA encryption, the encrypted code:\n"
    + this.byte2AsciiString(encryptResult)); byte[] decryptResult = this.decryptRSA(privatekey, encryptResult);
    System.out.println("\nafter RSA decryption, the original code:\n"
    + this.byte2AsciiString(decryptResult)); } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    SecurityAlg algs = new SecurityAlg();
    algs.RSADemo("something to encrypt");
    }
    }