java IO 流加密解密小例子__Android

來源:互聯網
上載者:User
一定要關流,完了。 import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class T {

static String fileA = "D:/a.txt";
static String fileB = "D:/b.txt";
private static final byte[] KEY = { 37, -82, 88, -42, 1, -36, -104, -125,
4, 81, -75, -94, -33, -75, 110, -3, -32, 64, -48, -105, -43, -113,
104, 32 };

public static void main(String[] args) throws IOException,
InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
String sourceString = doReadNoDes(fileB);
doWrite(sourceString, fileA);
System.out.println(doRead(fileA));

}

private static String doRead(String file) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException, IOException {
FileInputStream input = null;
try {
input = new FileInputStream(file);
return read(input);
} finally {
input.close();
}
}

private static void doWrite(String sourceString, String file)
throws IOException, InvalidKeyException, NoSuchAlgorithmException,
NoSuchPaddingException {
FileOutputStream output = null;
try {
output = new FileOutputStream(file);
write(output, sourceString);
} finally {
output.close();
}
}

private static String doReadNoDes(String file) throws IOException {
FileInputStream input = null;
try {
input = new FileInputStream(file);
return getStringFromStream(input);
} finally {
input.close();
}

}

private static String read(InputStream input)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IOException {
CipherInputStream cin = null;
try {
SecretKey key = getKey();
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.DECRYPT_MODE, key);
cin = new CipherInputStream(input, cp);
return getStringFromStream(cin);
} finally {
cin.close();
}
}

private static void write(OutputStream output, String sourceString)
throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
CipherOutputStream cin = null;
try {
SecretKey key = getKey();
Cipher cp = Cipher.getInstance("DESede");
cp.init(Cipher.ENCRYPT_MODE, key);
cin = new CipherOutputStream(output, cp);
cin.write(sourceString.getBytes());
} finally {
cin.close();
}
}

private static SecretKey getKey() {
return new SecretKeySpec(KEY, "DESede");
}

private static String getStringFromStream(InputStream stream) {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = null;
while ((line = in.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.