顯示具有 Java 標籤的文章。 顯示所有文章
顯示具有 Java 標籤的文章。 顯示所有文章

2011年10月15日 星期六

在 Ubuntu 中使用 Java 連接 MySQL

首先必須要有 java 對 mysql 的連接器,並設定環境變數,安裝方法:

sudo apt-get install libmysql-java

#取得 mysql java 連接器



CLASSPATH=$CLASSPATH:/user/share/java/mysql.jar

#設定自訂變數 CLASSPATH



export CLASSPATH

#將自定變數  CLASSPATH 設為環境變數



mysql connector 都安裝好了之後,就可以寫點小程式來測試一下啦!

JDBCMySQL.java


import java.sql.*;

public class JDBCMySQL {
    public static void main(String[] args) {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/phpbb";
        String user = "root";
        String password = "5.ru8ck6";
        try {
            Class.forName(driver);
            Connection conn =
               DriverManager.getConnection(url,
                                  user, password);

            if(conn != null && !conn.isClosed()) {
                System.out.println("資料庫連線測試成功!");
                conn.close();
            }

        }
        catch(ClassNotFoundException e) {
            System.out.println("找不到驅動程式類別");
            e.printStackTrace();
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
    }
}


參考資料:
https://help.ubuntu.com/community/JDBCAndMySQL
http://caterpillar.onlyfun.net/Gossip/JavaGossip-V2/ConnectDB.htm


2011年10月13日 星期四

使用 Java 進行 AES 加密

前天投了一家處理資安的公司,沒想到很快就回覆我了,而且還夾帶了一個小小的程式撰寫測驗!

----

程式寫作測驗:
請寫一個檔案加密的程式,輸入檔名,讀取內容,加密後輸出成另一加密檔。
請交原始碼、編譯環境相關檔案,可參考或使用現成的加密函式RSA, AES, DES…或者基本的XOR。
評分標準:
基本項目
1. coding style (Source Code)
2. 完成度(exe檔 或 JAR檔)


加分項目
1. 可以的話用不同語言(C++, Java)撰寫
2. 不同作業系統程式 (project or make file)
3. 視窗介面(console, Windows 或 手機)

----

加密的東西其實還蠻常用到的,像是 PHP 的使用者密碼都是會經過加密在儲存到資料庫,只是用 Java 到還是頭一遭!稍微做了點功課,發現大家比較推薦的加密演算法是 AES,而且在 Java 的官網就可以找到教學文章 ,所以就寫了一個簡單的 AES 加密、解密器。

核心程式碼如下:

AESEncrypter.java

package joujiahe.encrypt.AES;

import java.security.NoSuchAlgorithmException;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;

// AES Encrypt core
public class AESEncrypter {

 private SecretKeySpec skeySpec;
 private Cipher cipher;
 
 public AESEncrypter() throws NoSuchAlgorithmException, NoSuchPaddingException{
    // Get the KeyGenerator
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128); // 192 and 256 bits may not be available
 
 
    // Generate the secret key specs.
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    this.skeySpec = new SecretKeySpec(raw, "AES");
 
 
    // Instantiate the cipher
    this.cipher = Cipher.getInstance("AES");
 }
 
 // output encrypted bytes
 public  byte[] encrypt( byte[] original ) throws Exception{
   
    this.getCipher().init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(original);
    
    return encrypted;
 }

 // output decrypted bytes
 public  byte[] decrypt( byte[] original ) throws Exception{
    
    this.getCipher().init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(original);
    
    return decrypted;
 }

 // getter and setter
 public SecretKeySpec getSkeySpec() {
  return skeySpec;
 }

 public void setSkeySpec(SecretKeySpec skeySpec) {
  this.skeySpec = skeySpec;
 }

 public Cipher getCipher() {
  return cipher;
 }

 public void setCipher(Cipher cipher) {
  this.cipher = cipher;
 }
 
}


參考資料:Using AES with Java Technology