ASi

暗号APIの使い方

コード断片だけ

ByteArrayInputStream is;
ByteArrayOutputStream os;
CipherInputStream is_enc;
CipherOutputStream os_dec;
        	    
KeyGenerator keygen = KeyGenerator.getInstance("AES");
Key key = keygen.generateKey();

Cipher cipher_enc = Cipher.getInstance("AES");
cipher_enc.init(Cipher.ENCRYPT_MODE, key);
Cipher cipher_dec = Cipher.getInstance("AES");
cipher_dec.init(Cipher.DECRYPT_MODE, key);

is = new ByteArrayInputStream("1234567890".getBytes());
is_enc = new CipherInputStream(is, cipher_enc);

os = new ByteArrayOutputStream();
os_dec = new CipherOutputStream(os, cipher_dec);

byte[] b = new byte[8];
int i;
while (-1 != (i = is_enc.read(b))) {
	String str = "";
    for (int j = 0 ; j < i ; j++){
    	 str = str + ' ' + b[j];
    }
    Log.d(TAG, "encrypted:" + str);
	os_dec.write(b, 0, i);
}
os_dec.close();

Log.d(TAG, "finished:" + new String(os.toByteArray()));