Java IO Stream Encryption decryption small example __android

Source: Internet
Author: User
Tags stringbuffer
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);
}
}

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.