-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_action.jsp
28 lines (25 loc) · 989 Bytes
/
crypto_action.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<%@ page import = "java.security.*, javax.crypto.*, javax.crypto.spec.*, java.util.*, java.io.*" %><%
String etype = request.getParameter( "etype" );
String keyStr = request.getParameter( "key" );
String algo = request.getParameter( "algo" );
String indata = request.getParameter( "indata" );
String encoding = request.getParameter( "encoding" );
byte[] k = keyStr.getBytes();
byte[] pt = indata.getBytes();
if ( encoding.equalsIgnoreCase("Base64") ) { //Base64 encoded
pt = Base64.getDecoder().decode(indata);
}
//Do encryption: RC4
Cipher rc4 = Cipher.getInstance("RC4");
Key key = new SecretKeySpec(k,"RC4");
if ( etype.equalsIgnoreCase("encrypt") ) { //Encryption
rc4.init(Cipher.ENCRYPT_MODE ,key);
byte[] ct = rc4.doFinal(pt);
out.println( "Output: " + Arrays.toString(ct) + "<br>" );
out.println( new String( Base64.getEncoder().encode(ct) ) );
} else { //Decryption
rc4.init(Cipher.DECRYPT_MODE ,key);
byte[] pt2 = rc4.doFinal(pt);
out.println( new String( pt2 ) );
}
%>