Servlet generation additional code (reproduced)

zhaozj2021-02-17  54

Generate additional code with Java

Now some websites will need to add additional code when they log in. This is to protect users, prevent hackers from obtaining user information (user name and password, before I have used Java to attack some websites to get users Password ^ _ ^). The following example is the method I implemented an additional code, I hope to start a role!

I just started to use Applet to implement additional code, this implementation is the implementation speed, the generation of the image does not have to access the server at the client; but the biggest shortcoming of this program is easy to disclose (users can download Applet, through anti-compilation You can know the additional code implementation algorithm, so that you can enter the system through a brute force attack), so it is not adopted.

Because logic is easy to disclose in client implementation, logical reality should be implemented on the server, I use a small service servlet. The following is the source code: Picture.java, CreateImage.java and CreateImage.jsp

// Read the image of the server side, draw the user needs to be entered in the image

Package lqh.rmb.picture;

Import java.io. *;

Public class picture {

Final Int key = 43;

Final string error_format_int = "format of color is not rgb.sample /" 212 | 232 | 0 / ""

Final string error_color_input = "format of color (num): Num in 0-255

/ ** Format Output Data ** /

Public string management (String Temp) {

String Returnstr = ""

Temp = Encrypt (TEMP);

Byte [] by = TEMP.GETBYTES ();

For (int i = 0; i

Returnstr = Returnstr (Byte) by [i] "|";

}

Return Returnstr;

}

/ ** Format Input Data ** /

Public Byte [] Dismanage (String Temp) {

INT LEN = 0, INDEX = 0, i = 0, first = 0;

While ((i = temp.indexof ("|", first))> - 1) {

Len ;

FigSt = i 1;

}

Byte [] by = new byte [len];

FigSt = 0;

While ((i = temp.indexof ("|", first))> - 1) {

By [index] = byte.Parsebyte (Temp.Substring (First, i));

INDEX ;

FigSt = i 1;

}

Return by;

}

/ ** randomly generate four additional code ** /

Public String getrandom () {

INT I1 = (int) (java.lang.math.random () * 10);

INT I2 = (int) (java.lang.math.random () * 10);

INT i3 = (int) (java.lang.math.random () * 10);

INT i4 = (int) (java.lang.math.random () * 10);

Return String.Valueof (i1) String.Valueof (i2) string.valueof (i3) string.valueof (i4);

/ ** Encryption 1: misalignment processing ** /

Public String Encrypt (String Randomstr) {

String para = random () randomstr.substring (0, 1) random () random () randomstr.substring (1, 2);

Para = Para Random () randomstr.substring (2);

Return Jiami (Para);

}

/ ** Get a random number 0-9 ** /

Private string random () {

String Temp = String.Valueof ((int) (java.lang.math.random () * 10));

Return Temp;

}

/ ** Encryption 2: Encryption processing, this method can modify ** /

Private string jiami (String str) {

Byte [] by = str.getbytes ();

BYTEARRAYINPUTSTREAM IN = New ByteArrayInputStream (by);

int CH;

INT INDEX = 0;

Byte [] temp = new byte [in.available ()];

While (chile (ch = in.read ())! = - 1) {

Temp [index] = (BYTE) (ch-key);

INDEX ;

}

BYTEARRAYINPUTSTREAM INS = New ByteArrayInputStream (TEMP);

DataInputStream INSS = New DataInputStream (INS);

Try {return INSS.READLINE ();} Catch (Exception E) {Return ";

}

/ ** Get the correct number from the number ** /

Public String Discrypt (String Temp) {

String Para = Jiemi (Dismanage (TEMP));

Return Para.Substring (1, 2) Para.Substring (4, 5) Para.Substring (6, 8);

}

/ ** Decryption handling ** /

Private string jiemi (byte [] by) {

BYTEARRAYINPUTSTREAM IN = New ByteArrayInputStream (by);

int CH;

INT INDEX = 0;

Byte [] temp = new byte [in.available ()];

While (chile (ch = in.read ())! = - 1) {

Temp [Index] = (Byte) (CH Key);

INDEX ;

}

BYTEARRAYINPUTSTREAM INS = New ByteArrayInputStream (TEMP);

DataInputStream INSS = New DataInputStream (INS);

Try {return INSS.READLINE ();} Catch (Exception E) {Return ";

}

/ ** Decompose color Num | NUM | NUM ** /

Public int [] masterdata (string temp) {

INDEX_LEN = 0, INDEX = 0, Next_index = 0; int [] return_arr = new int [3];

Boolean Break_ERROR = false;

IF (GetMax (Temp, "|") == 2) {

While ((Index_len = Temp.indexof ("|", next_index)> - 1) {

IF (getInt (Temp.substring (next_index, index_len) == 256) {

Break_ERROR = true;

} else {

Return_arr [index] = getInt (Temp.substring (next_index, index_len);

Next_index = INDEX_LEN 1;

INDEX ;

}

IF (BREAK_ERROR) BREAK;

}

IF (BREAK_ERROR) {

Return NULL;

} else {

Return_arr [index] = getInt (Temp.substring (next_index));

RETURN RETURN_ARR;

}

} else {

System.out.println (ERROR_FORMAT_INT ":" TEMP);

Return NULL;

}

}

Private int getmax (string temp, string temp2) {

INDEX = 0, INDEX_LEN = 0, INDEX_NEXT = 0;

While ((Index = Temp.indexof (Temp2, Index_Next)> - 1) {

INDEX_LEN ;

INDEX_NEXT = INDEX 1;

}

Return Index_Len;

}

Private int getint (String Temp) {

Try {

Return Integer.Parseint (TEMP);

} catch (exception e) {

System.out.println (ERROR_COLOR_INPUT ":" TEMP);

Return 256;

}

}

}

Below is a servlet to respond to customers, return to a customer a good picture:

Package lqh.rmb.picture;

Import java.io. *;

Import java.util. *;

Import javax.servlet. *;

Import javax.servlet.http. *;

Import java.awt. *;

Import java.awt.image. *;

Import java.awt.image.bufferedImage;

Import com.sun.Image.codec.jpeg. *;

Import com.sun.jpegcodec;

Public class createImage extends httpservlet {

Static Final Private string content_type = "text / html; charSet = GB2312";

Final String Input_back_color_error = "Input RGB Backcolor IS Error";

Final string infut_fore_color_error = "Input RGB Forecolor is Error";

Private Picture Pic = New Picture (); // Initialize Global Variables

Public void init () throws servletexception {

}

// Process the http get request

Public void doget (httpservletRequest request, httpservletResponse response) throws servletexception, ioException {

String querynum = Request.getParameter ("image");

String queryRGB = "";

IF (Request.GetParameter ("RGB")! = null) {

QueryRGB = Request.getParameter ("RGB");

}

Response.setHeader ("Cache-Control", "No-Store");

Response.setContentType ("Image / JPEG");

ServletOutputStream out = response.getOutputStream ();

// jpg format background color picture (same as the page style), width 3.6 mm, high 1.8 mm

InputStream Imagein = New FileInputStream (New File ("D: / Test.jpg"));

JPEGIMAGEDECODER DECODER = JPEGCODEC.CREATEJPEGDECODER (Imagein);

Bufferedimage image4 = decoder.decodeasbufferedImage ();

Querynum = pic.discrypt (querynum);

Graphics g = image4.getgraphics ();

Queryrgb.Length ()> 1) {

IF (pic.masterdata (queryrgb)! = null) {

Int [] arg = pic.masterdata (queryRGB);

G.SetColor (New Color (Arg [0], Arg [1], Arg [2]));

}

} else {

G.SetColor (New Color (255, 0, 0));

}

g.drawstring (querynum, 0, 13);

JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUT);

Encoder.Encode (image4);

Out.close ();

}

}

Below is a JSP page to verify the result:

<% @ page contenttype = "text / html; charset = GBK"%>

<% @ page import = "lqh.rmb.picture"%>

<%

Picture Pic = New Picture ();

String random = pic.getrandom ();

String encryrandom = pic.manage (random);

%>

<% - this code may need himself to modify, in order to call to servlet -%>

Welcome

Welcome contact: lqhboy@163.com

转载请注明原文地址:https://www.9cbs.com/read-30646.html

New Post(0)