Encryption of images and voice in Android, the Web

Source: Internet
Author: User

because of the project needs of a bank, both the Android client and the web side of the project app need to upload files to the server (voice, picture) to the client.
For encryption. Encryption is implemented using the classes provided in the Javax.crypto package, the most important of which is the cipher class.
The steps implemented in the Android project are as follows:
1. Generate a key for encryption and decryption according to the strkey we specify
2. Encrypt files, encrypt files according to key
3. Decrypt files, decrypt files according to key

The code is as follows:

        /** * Generate key */public string GetKey (String strkey) {try {byte[] Keybyte = Strkey.getbytes () based on parameters,//Create an empty eight-bit array, 0byte[by default) Bytetemp = new byte[8];//Converts the user-specified rule to a eight-bit array for (int i = 0; i < bytetemp.length && i < keybyte.length; i++) {b Ytetemp[i] = Keybyte[i];} return  new Secretkeyspec (bytetemp, "DES"),} catch (Exception e) {throw new RuntimeException ("Error initializing Sqlmap class. Cause: "+ E);}}
<span style= "White-space:pre" ></span>//Encrypting File <span style= "White-space:pre" ></span>public void Encrypt (string file, String destfile) throws Exception {Cipher Cipher = cipher.getinstance ("DES");//Cipher.init (CIP Her. Encrypt_mode, GetKey ()); Cipher.init (Cipher.encrypt_mode, This.key); InputStream is = new FileInputStream (file); O Utputstream out = new FileOutputStream (destfile); CipherInputStream cis = new CipherInputStream (IS, cipher); byte[] buffer = new Byte[1024];int R;while ((r = cis.read (buffer )) > 0) {out.write (buffer, 0, R);} Cis.close (); Is.close (); Out.close (); File Img2 = new file (file); Commutil.delete (IMG2);}
<span style= "White-space:pre" ></span>//decryption files, this is the Android side, web-side encryption means the same
<pre name= "code" class= "java" ><span style= "White-space:pre" ></span>public Bitmap Decrypt (String File, String dest) throws Exception {Bitmap bitmaporiginal = null; Cipher Cipher = cipher.getinstance ("DES"); Cipher.init (Cipher.decrypt_mode, This.key); InputStream is = new FileInputStream (file); OutputStream out = new FileOutputStream (dest); CipherOutputStream cos = new CipherOutputStream (out, cipher); byte[] buffer = new Byte[1024];int R;while ((r = is.read (buff ER)) >= 0) {cos.write (buffer, 0, R);} Cos.close (); Out.close (); Is.close (); InputStream Openis = new FileInputStream (dest); bitmaporiginal = Bitmapfactory.decodestream (Openis); Openis.close (); File Img2 = new file (dest); Commutil.delete (IMG2); return bitmaporiginal;}

Strkey is our own configuration, used to generate encryption and decryption of the Key,strkey for security and configuration flexibility, the project is placed in the server, whenever the Android client login,
The strkey is sent to the client. Some file operations do not need to be consistent with the server, and these strkey can be written in C code using JNI methods.




Encryption of images and voice in Android, the Web

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.