Wednesday, June 20, 2012

 

Complex Nested annotations

https://blogs.oracle.com/toddfast/entry/creating_nested_complex_java_annotations

Sunday, May 04, 2008

 

java troubleshooting tricks

http://java.sun.com/javase/6/webnotes/trouble/TSG-VM/html/gblfh.html

Monday, June 11, 2007

 

get Serial Version ID of a class

http://www.blogger.com/post-create.g?blogID=23162917

Tuesday, May 22, 2007

 

Tweaking eclipse

http://swem.wm.edu/blogs/waynegraham/index.cfm/2006/9/7/Tweaking-Eclipse

Thursday, March 22, 2007

 

Triple DES TripleDES


public class TripleDES {
public static final String DES_PROVIDER = "SunJCE";
public static final int DES_KEYSIZE = 112;
public static final byte SEED[] = "15454432999".getBytes();

public final static String ENCODED_PASSWORD_PREFIX = "enc";

private static TripleDES _me=new TripleDES();

private TripleDES() {};

public static TripleDES getInstance() {
return _me;
}



public String encrypt(String code) {
return encrypt(code, null);
}

private String encrypt(String code, SecureRandom sr){
SecureRandom rng;
KeyGenerator keyGen;
Key key;
Cipher cipher;
byte[] byteData = code.getBytes();

if(sr == null) rng = new SecureRandom(SEED);
else rng = sr;

try{
keyGen = KeyGenerator.getInstance("DESede", DES_PROVIDER);
keyGen.init(DES_KEYSIZE, rng);
key = keyGen.generateKey();

cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding", DES_PROVIDER);

cipher.init(Cipher.ENCRYPT_MODE, key);

return new sun.misc.BASE64Encoder().encode(cipher.doFinal(byteData));
}catch(NoSuchProviderException npro){
}catch(NoSuchAlgorithmException nalg){
}catch(NoSuchPaddingException npad){
}catch(InvalidKeyException inv){
}catch(BadPaddingException bad){
}catch(IllegalBlockSizeException ile){
}

return null;
}

public String decrypt(String code){
return decrypt(code, null);
}

private String decrypt(String code, SecureRandom sr){
SecureRandom rng;
KeyGenerator keyGen;
Key key;
Cipher cipher;

if(sr == null) rng = new SecureRandom(SEED);
else rng = sr;

try{
keyGen = KeyGenerator.getInstance("DESede", DES_PROVIDER);
keyGen.init(DES_KEYSIZE, rng);
key = keyGen.generateKey();

cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding", DES_PROVIDER);

cipher.init(Cipher.DECRYPT_MODE, key);

byte[] codeBytes = new sun.misc.BASE64Decoder().decodeBuffer(code);

byte[] text = cipher.doFinal(codeBytes);
return new String(text);
}catch(NoSuchProviderException npro){
npro.printStackTrace();
}catch(NoSuchAlgorithmException nalg){
nalg.printStackTrace();
}catch(NoSuchPaddingException npad){
npad.printStackTrace();
}catch(InvalidKeyException inv){
inv.printStackTrace();
}catch(BadPaddingException bad){
bad.printStackTrace();
}catch(IllegalBlockSizeException ile){
ile.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}

return null;
}

}


Wednesday, March 21, 2007

 

mysql jdbc

Statement stmt;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url,"root", "");
stmt = con.createStatement();

 

HttpClient code that logs in gets cookies and redirects


private void doLogin() throws Exception {
int curTestIndex = 0;
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
PostMethod authpost = new PostMethod("/login");
// Prepare login parameters
// user=tomer&pass=1234&view=login&op=dologin&site=hhhh
NameValuePair user = new NameValuePair("user", "tomer");
NameValuePair pass = new NameValuePair("pass", "1234");
NameValuePair view = new NameValuePair("view", "login");
NameValuePair op = new NameValuePair("op", "dologin");
NameValuePair site = new NameValuePair("site", "hhhhh");
authpost.setRequestBody(
new NameValuePair[] {user, pass, view, op, site});

authpost.setRequestHeader("User-Agent", "Nokia6230i");
client.executeMethod(authpost);
System.out.println("Login form post: " + authpost.getStatusLine().toString());
// release any connection resources used by the method
authpost.releaseConnection();
// See if we got any cookies
// The only way of telling whether logon succeeded is
// by finding a session cookie
Cookie[] logoncookies = cookiespec.match(
LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
System.out.println("Logon cookies:");
if (logoncookies.length == 0) {
System.out.println("None");
} else {
for (int i = 0; i < logoncookies.length; i++) {
System.out.println("- " + logoncookies[i].toString());
}
}
// Usually a successful form-based login results in a redicrect to
// another url
int statuscode = authpost.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header header = authpost.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
System.out.println("Redirect target: " + newuri);
GetMethod redirect = new GetMethod(newuri);
redirect.setFollowRedirects(true);
client.executeMethod(redirect);
System.out.println("Redirect: " + redirect.getStatusLine().toString());
// release any connection resources used by the method
System.out.println("redirect path: " + redirect.getPath());
System.out.println(redirect.getResponseBodyAsString());
redirect.releaseConnection();
} else {
System.out.println("Invalid redirect");
}
}
}

Sunday, December 24, 2006

 

Eclipse Memory

http://swem.wm.edu/blogs/waynegraham/index.cfm/2006/9/7/Tweaking-Eclipse

This page is powered by Blogger. Isn't yours?