Java IO Stream Encryption decryption small example __android
Last Update:2018-07-27
Source: Internet
Author: User
Be sure to turn off the flow, it's over. 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);
}
}
}