0
//MY PathParam code
package com.security.security;
import javax.websocket.server.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.codehaus.jettison.json.JSONException;
import com.security.security.AesUtilHelper;
import com.security.security.EncryptionUtil;;
@Path("Example")
public class encryptWithQuery {
@GET
@Produces("application/json")
@Path("/encrypt/{name}/")
public static String Encrypt(@PathParam("name") String PLAIN_TEXT){
      EncryptionUtil util=new EncryptionUtil();
      String encrypt = util.encode(PLAIN_TEXT);  
      return encrypt;
    }
 @GET
 @Produces("application/json")
 @Path("/decrypt/{name1:(.+)?}/")
 public static String Decrypt(@PathParam("name1") String encrypt){
        EncryptionUtil util=new EncryptionUtil();
        String decrypt = util.decode(encrypt);      
        System.out.println(decrypt);
        return decrypt;
    }
 }

//加密和解密 //加密显示相同的值和解密字节数组不转换为字符串

        import java.security.spec.AlgorithmParameterSpec;
        import java.security.spec.KeySpec;   
        import javax.crypto.Cipher;
        import javax.crypto.SecretKey;
        import javax.crypto.SecretKeyFactory;
        import javax.crypto.spec.PBEKeySpec;
        import javax.crypto.spec.PBEParameterSpec;
        import org.apache.commons.codec.binary.Base64;
        public class EncryptionUtil
        {
          // some random salt
          private static final byte[]SALT= { (byte) 0x21, (byte) 0x21, (byte) 0xF0, (byte) 0x55, (byte) 0xC3, (byte) 0x9F, (byte) 0x5A, (byte) 0x75                     };
          private final static int  ITERATION_COUNT = 31;
          EncryptionUtil()
          {
          }
          public static String encode(String input)
          {
                if (input == null)
                {
                    throw new IllegalArgumentException();
                }
                try
                {

                    KeySpec keySpec = new PBEKeySpec(null, SALT, ITERATION_COUNT);
                    AlgorithmParameterSpec paramSpec = new PBEParameterSpec(SALT, ITERATION_COUNT);

                    SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);

                    Cipher ecipher = Cipher.getInstance(key.getAlgorithm());
                    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

                    byte[] enc = ecipher.doFinal(input.getBytes());

                    String res = new String(Base64.encodeBase64(enc));
                    // escapes for url
                    res = res.replace('+', '-').replace('/', '_').replace("%", "%25").replace("\n", "%0A");
                    System.out.println("ecncoded value is " + new String(res ));

                    return res;

                }
                catch (Exception e)
                {
                }

                return "";

            }

            public static String decode(String token)
            {
                //System.out.println(token);

                if (token == null)
                {
                    return null;
                }
                try
                {

                    String input = token.replace("%0A", "\n").replace("%25", "%").replace('_', '/').replace('-', '+');
                //  System.out.println(input);

                    byte[] dec = Base64.decodeBase64(input.getBytes());
                    //System.out.println(dec);

                    KeySpec keySpec = new PBEKeySpec(null, SALT, ITERATION_COUNT);
                    AlgorithmParameterSpec paramSpec = new PBEParameterSpec(SALT, ITERATION_COUNT);
                    //System.out.println(keySpec);
                    //System.out.println(paramSpec);

                    SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
                    //System.out.println(key);

                    Cipher dcipher = Cipher.getInstance(key.getAlgorithm());
                    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

                    byte[] decoded = dcipher.doFinal(dec);
                    System.out.println(decoded);
                    String result = new String(decoded);
                    System.out.println("Decoded value is " + new String(decoded));
                    return result;

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                return null;
            }

        }

..... //这里是我的 EncryptionUtil.java
当我运行加密路径参数时,我没有得到正确的结果,但是当我在解码路径参数中运行加密字符串以显示空白屏幕时..这是字节数组未转换为细绳

4

1 回答 1

0
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.codehaus.jettison.json.JSONException;

import com.security.security.AesUtilHelper;
import com.security.security.EncryptionUtil;;

@Path("Example")

public class encryptWithQuery {

    @GET
    @Produces("application/json")

    @Path("/encrypt/{name:(.+)?}/")
    public static String Encrypt(@PathParam("name") String PLAIN_TEXT){
          EncryptionUtil util=new EncryptionUtil();
          String encrypt = util.encode(PLAIN_TEXT);  
          return encrypt;
        }
     @GET
     @Produces("application/json")
     @Path("/decrypt/{name1:(.+)?}/")
     public static String Decrypt(@PathParam("name1") String encrypt){
            EncryptionUtil util=new EncryptionUtil();
            String decrypt = util.decode(encrypt);      
            System.out.println(decrypt);
            return decrypt;
        }
    }
于 2015-10-29T04:04:30.060 回答