2014年6月19日 星期四
2014年2月21日 星期五
Java加解密Class使用 - 對稱加密
稍微記錄一下Java中加密類別的使用方法,要使用加解密會用到下列三個Class:KeyGenerator、SecretKey、Cipher。 KeyGenerator主要是負責產生加解密用的Key
例如
例如
KeyGenerator keygen = KeyGenerator.getInstance("DES");//參數代表使用的加解密演算法
SecretKey key = keygen.generateKey(); //產生key
在產生完Key後,利用Cipher來對資料加密。String str ="example";
byte[] src = str.getBytes();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);//決定是何種模式有 ENCRYPT_MODE, DECRYPT_MODE, WRAP_MODE, UNWRAP_MODE
byte [] result = cipher.doFinal(src);//依據設定的Mode來開始動作,result即為加密後之密文
如果要解密的話,則改用DECRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, key); byte [] src = cipher.doFinal(result);//取回明文,在轉換回對應之type即可 String str = new String(src);因為對稱加密,加解密都是用同一把Key所以會需要保存Key,最簡單的方法就是使用檔案來存放,例如
//存成檔案 byte [] bytekey = key.getEncoded(); int len = bytekey.length; FileOutputStream fos; fos = new FileOutputStream(FILEPATH) fos.write(bytekey); fos.close(); //讀檔 byte [] buf = new byte[len] FileInputStream fis = new FileInputStream(FILEPATH); int i = fis.read(buf); fis.close(); SecretKey resultKey = new SecretKeySpec(buf, "DES");//從byte array轉回key當然也可用ObjectInputStream、ObjectOutputStream拉,如此一來就可以少去byte轉換的動作。
2014年2月19日 星期三
LocalBroadcastManager 使用
LocalBroadcastManager 簡單的說就是intra-app的Broadcast 發送,用來避免Broadcast 發送到外部所造成的security issue(當然你也可以指定target或是用Permission拉...)。 下面是Android 上所提到的優點:
範例
- You know that the data you are broadcasting won't leave your app, so don't need to worry about leaking private data.
- It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
- It is more efficient than sending a global broadcast through the system.
範例
LocalBroadcastManager mLocalBroadcastManager;
public void onCreate(Bundle savedInstanceState) {
mLocalBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext()); //要先getInstance
}
protected void onPause() {
mLocalBroadcastManager.unregisterReceiver(TTLNotifyReceiver);
}
protected void onResume() {
IntentFilter filter = new IntentFilter("com.test.hello");
mLocalBroadcastManager.registerReceiver(receiver, filter);
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//rece the Intent
}
};
2013年7月4日 星期四
Ant Import 和 Include的差異
http://ant.apache.org/manual/Tasks/include.html
其實官方網頁寫得很清楚了,貼一下原文
例如官方的例子, 下面是要被導入的nested.xml
其實官方網頁寫得很清楚了,貼一下原文
The short version: Use import if you intend to override a target, otherwise use include.意思就是說如果被導入的target需要被 override的話就用import,不然用include即可。
When using import the imported targets are available by up to two names. Their "normal" name without any prefix and potentially with a prefixed name (the value of the as attribute or the imported project's name attribute, if any).
When using include the included targets are only available in the prefixed form.
When using import, the imported target's depends attribute remains unchanged, i.e. it uses "normal" names and allows you to override targets in the dependency list.
When using include, the included targets cannot be overridden and their depends attributes are rewritten so that prefixed names are used. This allows writers of the included file to control which target is invoked as part of the dependencies.
It is possible to include the same file more than once by using different prefixes, it is not possible to import the same file more than once.
例如官方的例子, 下面是要被導入的nested.xml
結論: 如果沒有要改動到導入的XML東西用include即可,反之則用import<project> <target name="setUp"> <property name="prop" value="in nested.xml"/> </target> <target name="echo" depends="setUp"> <echo>prop has the value ${prop}</echo> </target> </project>如果用import結果會是<project default="test"> <target name="setUp"> <property name="prop" value="in importing"/> </target> <import file="nested.xml" as="nested"/> <target name="test" depends="nested.echo"/> </project>setUp: nested.echo: [echo] prop has the value in importing test:如果用include結果會變成<project default="test"> <target name="setUp"> <property name="prop" value="in importing"/> </target> <include file="nested.xml" as="nested"/> <target name="test" depends="nested.echo"/> </project>nested.setUp: nested.echo: [echo] prop has the value in nested.xml test:
2013年6月12日 星期三
Java sleep(), yield()/join(), notify()/notifyAll()/wait() 用法差異
http://dylanxu.iteye.com/blog/1322066
太久沒有用到,最近要用到的時候又忘了...
乾脆把他整理一下。
1.sleep, yield, join 基本上都是Thread scheduling的method,sleep跟yield用法差不多,基本上就是Thread不需要執行的時候呼叫,只是差在sleep可以決定時間。
join則是等待某個Thread執行完,例如
thread.join() //等thread跑完 在繼續
2.notify()/notifyAll()/wait() 則是Data Lock的部分,這些method都要在synchronized block中呼叫,不然會丟exception。
如果wait()的Thread要離開可以呼叫Thread的interrupt(); 會丟InterruptedException,可以跳離wait()狀態。
太久沒有用到,最近要用到的時候又忘了...
乾脆把他整理一下。
1.sleep, yield, join 基本上都是Thread scheduling的method,sleep跟yield用法差不多,基本上就是Thread不需要執行的時候呼叫,只是差在sleep可以決定時間。
join則是等待某個Thread執行完,例如
thread.join() //等thread跑完 在繼續
2.notify()/notifyAll()/wait() 則是Data Lock的部分,這些method都要在synchronized block中呼叫,不然會丟exception。
如果wait()的Thread要離開可以呼叫Thread的interrupt(); 會丟InterruptedException,可以跳離wait()狀態。
try { while(true) { synchronized (this) { wait(); } } } catch (InterruptedException e) { //interrupt triger }
訂閱:
文章 (Atom)
[京都] 瑠璃光院 永觀堂 東寺
京都的紅葉大部分都是以寺廟為中心,來記錄一下這次跑的寺廟,第一間是 瑠璃光院,這沒甚麼好說的,就是熱門景點,由於剛好是不用預約的日期,所以最好要提早去排隊,交通也不是很方便會有巴士爆滿的情況,而且門票也不便宜,不過拍出來的照片層次豐富,要不要去被騙一次就看人抉擇了。 ...
-
今天的旅程要搭新幹線離開仙台繼續往北,到新青森之後改搭奧羽本線前往弘前,搭乘往新青森的新幹線需要注意的是,在盛岡之後全車皆為指定席,所以搭乘前請先到櫃台劃位,不然就只能找空位被趕來趕去了。 東北新幹線常常可以看到兩車一起行駛的畫面,上圖...
-
一直以來都有將RSU從ETRADE轉出,為了怕忘掉流程乾脆直接寫下來,比較不容易忘記。 這邊用的例子是從ETRADE將RSU轉到Charles Schwab,首先直接跟ETRADE的客服說要將股票轉出,客服會給你下面的表格,將表格填好,然後傳真到上面的電話即可...
-
集美學村雖然是廈門景點,但是卻不在廈門島上,所以在上一次的廈門行並沒有來到這個景點,這次剛好從廈門機場進入廈門,加上上次還沒開通廈門地鐵一號線也開通了。所以就來走一趟。 其實要到集美學村最快的方式應該是搭公車,不過因為想體驗地鐵一號線,所以這一次...