import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
// CHANGE THIS VALUE DEPENDING ON WHAT YOU WANT
public static final int MESSAGE_LENGTH = 1024;
/**
* @param text
* Message to encrypt
* @param secret_key
* Secret Key
* @param crypt_spec
* Crypt algorithm like "AES" or "DES"
* @return Encrypted Byte Strings
*/
public byte[] encrypt(String text, byte[] secret_key, String crypt_spec)
throws InvalidKeyException, IllegalBlockSizeException, IOException,
BadPaddingException {
SecretKeySpec sKey = new SecretKeySpec(secret_key, crypt_spec);
byte[] secret = new byte[MESSAGE_LENGTH];
try {
Cipher cipher = Cipher.getInstance(crypt_spec);
cipher.init(Cipher.ENCRYPT_MODE, sKey);
secret = cipher.doFinal(text.getBytes());
} catch (Exception e) {
System.out.println(this.getClass().getName()
+ ".encrypt: Exception:");
e.printStackTrace();
}
return secret;
}
/**
* @param b
* Encypted Byte Message
* @param crypt_spec
* Crypt Algorithm like "AES" or "DES"
* @return Decrypted String
*/
public String decrypt(byte[] b, byte[] secret_key, String crypt_spec)
throws InvalidKeyException, IllegalBlockSizeException, IOException,
BadPaddingException {
SecretKeySpec sKey = new SecretKeySpec(secret_key, crypt_spec);
byte[] secret = new byte[MESSAGE_LENGTH];
try {
Cipher cipher = Cipher.getInstance(crypt_spec);
cipher.init(Cipher.DECRYPT_MODE, sKey);
secret = cipher.doFinal(b);
} catch (Exception e) {
System.out.println(this.getClass().getName()
+ ".decrypt: Exception:");
e.printStackTrace();
}
return new String(secret);
}
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
// CHANGE THIS VALUE DEPENDING ON WHAT YOU WANT
public static final int MESSAGE_LENGTH = 1024;
/**
* @param text
* Message to encrypt
* @param secret_key
* Secret Key
* @param crypt_spec
* Crypt algorithm like "AES" or "DES"
* @return Encrypted Byte Strings
*/
public byte[] encrypt(String text, byte[] secret_key, String crypt_spec)
throws InvalidKeyException, IllegalBlockSizeException, IOException,
BadPaddingException {
SecretKeySpec sKey = new SecretKeySpec(secret_key, crypt_spec);
byte[] secret = new byte[MESSAGE_LENGTH];
try {
Cipher cipher = Cipher.getInstance(crypt_spec);
cipher.init(Cipher.ENCRYPT_MODE, sKey);
secret = cipher.doFinal(text.getBytes());
} catch (Exception e) {
System.out.println(this.getClass().getName()
+ ".encrypt: Exception:");
e.printStackTrace();
}
return secret;
}
/**
* @param b
* Encypted Byte Message
* @param crypt_spec
* Crypt Algorithm like "AES" or "DES"
* @return Decrypted String
*/
public String decrypt(byte[] b, byte[] secret_key, String crypt_spec)
throws InvalidKeyException, IllegalBlockSizeException, IOException,
BadPaddingException {
SecretKeySpec sKey = new SecretKeySpec(secret_key, crypt_spec);
byte[] secret = new byte[MESSAGE_LENGTH];
try {
Cipher cipher = Cipher.getInstance(crypt_spec);
cipher.init(Cipher.DECRYPT_MODE, sKey);
secret = cipher.doFinal(b);
} catch (Exception e) {
System.out.println(this.getClass().getName()
+ ".decrypt: Exception:");
e.printStackTrace();
}
return new String(secret);
}
String protocol = "http";
String hostname = "yourdomain.com";
int port = 80;
String file = "/";
try {
URL url = new URL(protocol, hostname, port, file);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Input OK
conn.setDoOutput(true); // Output OK
conn.setUseCaches(false); // Cache NG
conn.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
conn.connect(); // Connect
boolean isRedirect = HttpURLConnection.getFollowRedirects();
String cookieValue = null;
try {
Map<String, List<String>> headers = conn.getHeaderFields();
List<String> values = headers.get("Set-Cookie");
for (Iterator<String> iter = values.iterator(); iter.hasNext();) {
String v = iter.next();
if (cookieValue == null)
cookieValue = v;
else
cookieValue = cookieValue + ";" + v;
}
} catch (Exception e) {
}
System.out.println(this.getClass().getName()
+ ": cookieValue: " + cookieValue);
conn.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
String protocol = "http";
String hostname = "yourdomain.com";
int port = 80;
String file = "/";
try {
URL url = new URL(protocol, hostname, port, file);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Input OK
conn.setDoOutput(true); // Output OK
conn.setUseCaches(false); // Cache NG
conn.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
conn.connect(); // Connect
boolean isRedirect = HttpURLConnection.getFollowRedirects();
String cookieValue = null;
try {
Map<String, List<String>> headers = conn.getHeaderFields();
List<String> values = headers.get("Set-Cookie");
for (Iterator<String> iter = values.iterator(); iter.hasNext();) {
String v = iter.next();
if (cookieValue == null)
cookieValue = v;
else
cookieValue = cookieValue + ";" + v;
}
} catch (Exception e) {
}
System.out.println(this.getClass().getName()
+ ": cookieValue: " + cookieValue);
conn.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
0x01c0 = 0x0100 + 0xc0 = 256 + 192 = 448を表すとする。
byte[] b = {0x01, 0xc0}; // 例えば、この場合、256 + 192 = 448 となる。
int length = ((b[0] & 0xff) << 8 ) + (b[1] & 0xff);
0x01c0 = 0x0100 + 0xc0 = 256 + 192 = 448を表すとする。
byte[] b = {0x01, 0xc0}; // 例えば、この場合、256 + 192 = 448 となる。
int length = ((b[0] & 0xff) << 8 ) + (b[1] & 0xff);
public String byteToString(byte[] b) {
StringBuffer s = new StringBuffer();
for (int i = 0; i < b.length; i++) {
int d = b[i];
d += d < 0 ? 256 : 0; // byte 128-255
if (d < 16) { //0-15 16
s.append("0");
}
s.append(Integer.toString(d, 16));
}
return s.toString();
}
public String byteToString(byte[] b) {
if (b == null || b.length <= 0) return null;
byte ch = 0x00;
String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
StringBuffer s = new StringBuffer(b.length * 2);
for(int i = 0; i < b.length; i++) {
ch = (byte) (b[i] & 0xf0);
ch = (byte) (ch >>> 4);
ch = (byte) (ch & 0x0f);
s.append(pseudo[(int) ch]);
ch = (byte) (b[i] & 0x0f);
s.append(pseudo[(int) ch]);
}
return s.toString();
}
public String byteToString(byte[] b) {
StringBuffer s = new StringBuffer();
for (int i = 0; i < b.length; i++) {
int d = b[i];
d += d < 0 ? 256 : 0; // byte 128-255
if (d < 16) { //0-15 16
s.append("0");
}
s.append(Integer.toString(d, 16));
}
return s.toString();
}
public String byteToString(byte[] b) {
if (b == null || b.length <= 0) return null;
byte ch = 0x00;
String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
StringBuffer s = new StringBuffer(b.length * 2);
for(int i = 0; i < b.length; i++) {
ch = (byte) (b[i] & 0xf0);
ch = (byte) (ch >>> 4);
ch = (byte) (ch & 0x0f);
s.append(pseudo[(int) ch]);
ch = (byte) (b[i] & 0x0f);
s.append(pseudo[(int) ch]);
}
return s.toString();
}