2017-01-16 73 views
0

的問題,我面對的是,當我把這個REST服務,我無法得到解密電子郵件:http://localhost:8080/RestWebService/MM/decryptEmail解密URL不工作

更多細節我也低於添加圖像。 我希望我已經解釋了我的問題以及

package client; 
import java.io.InputStream; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLEncoder; 
import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import my.web.services.KeysGeneration; 

public class TestClient { 
    private static String keypass = "KHU12345KHU12345"; 
    public static void callPostService() 
    { 
     try{ 
      URL url = new URL("http://localhost:8080/RestWebService/MM"); 
      HttpURLConnection conn =(HttpURLConnection)url.openConnection(); 
      // Allow Inputs 
      conn.setDoInput(true); 

      // Allow Outputs 
      conn.setDoOutput(true); 

      // Don't use a cached copy. 
      conn.setUseCaches(false); 
      conn.setRequestMethod("POST"); 
      String text; 
      OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); 
      String email="[email protected]"; 

//   //**********AES Encryption 


//   // Create key and cipher 
      SecretKey aesKey = KeysGeneration.getKeys(keypass); 

       Cipher cipher = Cipher.getInstance("AES"); 
//    // encrypt the text 
       cipher.init(Cipher.ENCRYPT_MODE, aesKey); 
       byte[] encrypted = cipher.doFinal(email.getBytes()); 
       System.out.println("checking encryption "+new String(encrypted)); 
       writer.write("&email="+(encrypted)); 

//    //**********AES Encryption 

////    //**********AES Decryption 
// 
////    // decrypt the text 
       cipher.init(Cipher.DECRYPT_MODE, aesKey); 
       String decrypted= new String(cipher.doFinal(encrypted)); 
       System.out.println("checking dencryption "+decrypted); 
//// 
//////     //**********AES Decryption 

      writer.flush(); 
      writer.close(); 
      InputStream stream = conn.getInputStream(); 
      byte[] b = new byte[1024]; 
      int len = 0; 
      while((len=stream.read(b, 0,1024))>0) 
      { 
       System.out.println(new String(b,0,len)); 
      } 
      stream.close(); 

     } 
     catch(Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 

    public static void callGETService() 
    { 
     try{ 
      URL url = new URL("http://localhost:8080/RestWebService/[email protected]&pwd=1985"); 
      HttpURLConnection conn =(HttpURLConnection)url.openConnection(); 
      // Allow Inputs 
      conn.setDoInput(true); 

      // Allow Outputs 
      conn.setDoOutput(false); 

      // Don't use a cached copy. 
      conn.setUseCaches(false); 
      conn.setRequestMethod("GET"); 
      InputStream stream = conn.getInputStream(); 
      byte[] b = new byte[1024]; 
      int len = 0; 
      while((len=stream.read(b, 0,1024))>0) 
      { 
       System.out.println(new String(b,0,len)); 
      } 

      stream.close(); 
     } 
     catch(Exception ex) 
     { 
      ex.printStackTrace(); 
     } 

    } 

    public static void main(String[] args) { 
     // open your connection 
     TestClient.callGETService(); 
     TestClient.callPostService(); 
    } 

} 

//webService 

package my.web.services; 
import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.ws.rs.FormParam; 
import javax.ws.rs.GET; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.QueryParam; 

@Path("/MM") 
public class WebService { 

    private String keypass = "KHU12345KHU12345"; 

    //********GET Method************* 

    @GET 
    //GET method provides read only access to resources 
    public String validateUserGETMethod(@QueryParam("email")String email, @QueryParam("pwd")String pwd) 

    { 
     return "Using GET method \n Email address: "+email+ " and password:" +pwd; 
    } 


    //********POST Method************* 

    //POST method is used to create or update a new resources 
    //To generate encryption of email parameter 

    @POST 
    @Path("/encryptEmail") 
    public String validateUserPOSTMethodEncryption(@FormParam("email")String email)throws Exception 

    { 
     // Create key and cipher 
     SecretKey aesKey = KeysGeneration.getKeys(keypass); 
     Cipher cipher = Cipher.getInstance("AES"); 
     // encrypt the text 
     cipher.init(Cipher.ENCRYPT_MODE, aesKey); 
     byte[] encrypted = cipher.doFinal(email.getBytes()); 
     System.err.println(new String(encrypted)); 

     return "Using POST method \n Email address (encrypted): "+new String(encrypted); 

    } 


    //POST method is used to create or update a new resources 
    //To generate decryption of email parameter 
    @POST 
    @Path("/decryptEmail") 
    public String validateUserPOSTMethodDecryption(@FormParam("email")String email)throws Exception 

    { 
     //SecretKey aesKey = KeysGeneration.getKeys(keypass); 
     SecretKey secKey = KeysGeneration.getKeys(keypass);//generator.generateKey(); 

     // Create key and cipher 
     Cipher cipher = Cipher.getInstance("AES"); 


     // decrypt the text 
     cipher.init(Cipher.DECRYPT_MODE, secKey); 
     String decrypted= new String(cipher.doFinal(email.getBytes())); 

     return "Using POST method \n Email address (decrypted): "+decrypted; 
    } 

} 

enter image description here

+1

你還沒有很好地解釋這個問題。 – shmosel

+0

我已經從TestClient發送加密的電子郵件,我想通過使用webService代碼解密它。但是,當我輸入REST(如圖所示)時,我得到錯誤500. – user3582228

+1

如果客戶端發生HTTP 500錯誤,則服務器端發生錯誤。請檢查服務器的日誌,並將完整的錯誤(包括stacktrace)添加到問題中。 – Codo

回答

0

喜謝謝你,我對於你們的盛情建議解決的問題。我想告訴你,如果我正在計算正確加密所需的時間:這是我的代碼

//   //**********AES Encryption 
      //checking time in msec 
      long startTime=System.currentTimeMillis(); 
      //System.out.println(startTime); 
      //   // Create key and cipher 
      SecretKey aesKey = KeysGeneration.getKeys(keypass); 

      Cipher cipher = Cipher.getInstance("AES"); 
      //    // encrypt the text 
      cipher.init(Cipher.ENCRYPT_MODE, aesKey); 
      byte[] encrypted = cipher.doFinal(email.getBytes()); 
      System.out.println("checking encryption "+new String(encrypted)); 

      BASE64Encoder encoder = new BASE64Encoder(); 
      email = encoder.encode(encrypted); 
      System.out.println("Encoded Encrypted Value="+email); 

      // text="&email="+URLEncoder.encode(new String(encrypted), "UTF-8"); 
      writer.write("&email="+email); 
      //Calculating Encryption Time 
      long stopTime = System.currentTimeMillis(); 
      Duration ms=Duration.ofMillis(stopTime); 

      //System.out.println(stopTime); 
      long elapsedTime = stopTime - startTime; 
      System.out.println("Time required for encryption (milliseconds)= "+elapsedTime);