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

2020年6月2日 星期二

使用Java開發OpenCV程式


因為最近實驗室最近要做影像辨識了,要加緊腳步研究OpenCV。
由於我長期使用Java進行開發的緣故,所以想用Java做影像辨識程式。

1. 下載OpenCV

到官方網站下載OpenCV,OpenCV官方連結



2018年11月8日 星期四

【Java】FileStream 底層資料



test.java
File file1=new File("C:\\Users\\517106\\Desktop\\testFile\\test.tst");
FileOutputStream fos=new FileOutputStream(file1);
fos.write(0x41);
fos.write(0x42);
fos.write(0x43);
fos.write(0x44);


fos.write(int);

使用Notepad++開啟




使用HxD開啟



參考資料




2018年10月14日 星期日

【Java】使用Cipher實現字串加密/解密



程式碼:CipherAdaptor.java
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherSpi;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class CipherAdaptor{
private Base64.Decoder decoder=Base64.getDecoder();
private Base64.Encoder encoder=Base64.getEncoder();
    private final SecretKey secretKey;
    private final String algorithm,charset;
    private Cipher cipher;
    CipherAdaptor(String algorithm,SecretKey secretKey,String charset) throws NoSuchPaddingException, NoSuchAlgorithmException {
    encoder=encoder.withoutPadding();//設定Base64的解碼規則:編碼器省略末段的'='字符(如果有的話)。
    this.secretKey=secretKey;
        this.algorithm=algorithm;
        this.charset=charset;
        cipher=Cipher.getInstance(algorithm);//定義Cipher所使用的演算法
     
    }
/*
 * 明文:訊息
 * 密文:明文經過加密所產生的信息被稱為密文
 *
 */
    String encrypt(String str) throws Exception {
//    將明文轉換成位元組陣列
        byte[] executed=encrypt(str.getBytes(charset));
     
//    將已加密的位元組陣列使用Base64表示,因為輸出的位元組值不一定在字碼表內,
//        Base64可以將原始資料轉換為字串,提供後續使用的方便性。
        return encoder.encodeToString(executed);
    }

    byte[] encrypt(byte[] raw) throws Exception {
//    將位元組陣列編碼成Base64格式
        return execute(raw,Cipher.ENCRYPT_MODE,secretKey);
    }

    String decrypt(String str) throws Exception {
//    將密文解碼成位元組陣列
        byte[] CryptedBytes=decoder.decode(str);
     
        return new String(decrypt(CryptedBytes),charset);
    }

    byte[] decrypt(byte[] raw) throws Exception {
//    將已加密的位元組解密成位元組
        return execute(raw,Cipher.DECRYPT_MODE,secretKey);
    }

    String getAlgorithm(){
        return cipher.getAlgorithm();
    }

    private byte[] execute(byte[] raw,int mode,SecretKey secretKey) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException{
        cipher.init(mode,secretKey);//初始化Cipher
        return cipher.doFinal(raw);//加密(Cipher.ENCRYPT_MODE)/解密(DECRYPT_MODE)
    }
 
    static SecretKey Str2Key(String str,String algorithm) throws UnsupportedEncodingException {
//    將字串轉換成位元組陣列
    byte[] byte_key=str.getBytes("UTF-8");
   
//    將位元組轉成SecretKey
        return new SecretKeySpec(byte_key,0,byte_key.length,algorithm);
    }
 

}

2018年10月10日 星期三

【Java】Base64編碼與解碼


程式碼:
package ciphertest;

import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.util.Scanner;

public class Base64_Test {

    public static void main(String[] args) {
        Scanner src = new Scanner(System.in);
        while (true) {
            System.out.print("mode:");
            String mode = src.nextLine();
            if (mode.equals("x")) {
                System.exit(0);
            }

            System.out.print("input:");
            String contain = src.nextLine();

            if (mode.equals("e")) {
                System.out.println(encode(contain));
            } else if (mode.equals("d")) {
                try {
                    System.out.println(decode(contain));
                } catch (Base64DecodingException e) {
                    System.err.println(e.getMessage());
                }
            }

        }
    }

    static String encode(String msg) {
        byte[] bytes_str = msg.getBytes();
        return Base64.encode(bytes_str);
    }

    static String decode(String msg) throws Base64DecodingException {
        return new String(Base64.decode(msg));
    }
}



執行


Console
mode:e
input:Hello!! I am Vincent Yeh.
SGVsbG8hISBJIGFtIFZpbmNlbnQgWWVoLg==
mode:d
input:SGVsbG8hISBJIGFtIFZpbmNlbnQgWWVoLg==
Hello!! I am Vincent Yeh. mode:x

參考資料