2015-10-27 58 views
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
當我運行加密路徑參數時,我沒有得到正確的結果,但是當我在解碼路徑參數中運行加密字符串時,顯示空白屏幕..即沒有轉換爲字符串的字節數組

+0

我得到了結果... –

+0

因此,張貼它作爲一個答案,以幫助任何未來的讀者。 –

回答

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; 
     } 
    } 
+0

answer ...添加正則表達式 –