1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

JSP to .Net

Discussion in 'C#' started by AstarothSolutions, Apr 7, 2011.

  1. #1
    I have a relatively simple piece of JSP code that I need to convert to .Net (don't mind if it is VB or C#).

    Anyone have any ideas?

    
    <%@ page import="java.util.*, 
    	java.text.*, 
    	java.io.*, 
    	javax.servlet.*, 
    	javax.crypto.Cipher,
    	javax.crypto.SecretKey,
    	javax.crypto.spec.SecretKeySpec, 
    	java.net.URLEncoder, 
    	java.security.spec.AlgorithmParameterSpec,
    	java.security.GeneralSecurityException,
    	java.security.NoSuchAlgorithmException,
    	org.apache.commons.codec.binary.Base64,
    	org.apache.commons.codec.binary.Hex,
    	org.apache.commons.codec.BinaryEncoder"%>
    
    <%
    // remove the domain name from the user string
    String s = request.getParameter("id");
    String username = s.substring(s.indexOf("\\")+1, s.length()); 
    
    // decode the base64 encoded secret key
    //String b64EncodedSecretKey = "y2K0SPm1WO0iOm/DMNnJcpF1XRL5LknC6i9y5R0J8HM=";
    String b64EncodedSecretKey = "+od47ejGF/G94ultfN8QYg==";
    byte[] secretKeyBytes = b64EncodedSecretKey.getBytes("UTF-8");
    byte[] keyData = Base64.decodeBase64(secretKeyBytes);
    
    // create a secret key instance
    String encryptionAlgorithm = "AES";
    SecretKey secretKey = new SecretKeySpec(keyData, encryptionAlgorithm);
    
    // create cipher. note that because no mode/padding is specified in the
    // encryption transformation below, the default mode and padding of "ECB"
    // and "PKCS5Padding", respectively, will be used.
    String encryptionTransformation = "AES";
    Cipher cipher = Cipher.getInstance(encryptionTransformation);
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, (AlgorithmParameterSpec)null);
    
    // get ticket expiration date
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.add(Calendar.MINUTE, 5); // expire 5 minutes from now
    Date expireDate = cal.getTime();
    
    // format date
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    String iso8601Date = df.format(expireDate);
    
    // create ticket payload.
    StringBuffer buff = new StringBuffer();
    buff.append(username); // login key
    buff.append('|');
    buff.append(iso8601Date); // ISO-8601 formatted UTC date
    String ticketPayload = buff.toString();
    
    // encrypt ticket
    byte[] encryptedTicket = cipher.doFinal(ticketPayload.getBytes());
    
    // base64 encode the encrypted ticket
    byte[] rawBase64Bytes = Base64.encodeBase64(encryptedTicket , false);
    String b64EncodedTicket = new String(rawBase64Bytes, "UTF-8");
    
    // url encode the base64 encoded ticket. the resulting url encoded ticket
    // should be used as the value for the 'Ticket' query parameter
    String urlEncodedTicket = URLEncoder.encode(b64EncodedTicket, "UTF-8"); 
    
    response.sendRedirect("https://TargetDomain.com/security/security_check?orgKey=H15S2AC40X&Ticket=" + urlEncodedTicket );
    %>
    
    
    Code (markup):

     
    AstarothSolutions, Apr 7, 2011 IP