Android encrypts/decrypts audio files (AES) and androidaes

Source: Internet
Author: User

Android encrypts/decrypts audio files (AES) and androidaes

Encryption process: Read the test audio file prepared on the SD card in byte [] format, encrypt byte [] using the AES encryption algorithm, and then save and overwrite the original audio file, the encrypted audio file cannot be played. The principle of decryption is the same as that of encryption. the decrypted audio file can be played.


Code:

VoiceEncryptionActivity. java

Package com. example. voiceencryption; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import android. app. activity; import android. media. mediaPlayer; import android. OS. bundle; import android. OS. environment; import android. util. log; import android. view. view; import android. view. view. onClickListener; import andro Id. widget. button; import android. widget. toast; public class VoiceEncryptionActivity extends Activity implementsOnClickListener {private static final String TAG = "VoiceEncryptionActivity"; private static final String seed = "guess"; // seed private MediaPlayer mPlayer; private Button mPlayButton; private Button mEncryptionButton; private Button mDecryptionButton; private File sdCard = Environment. getExterna LStorageDirectory (); private File oldFile = new File (sdCard, "recording_old.3gpp"); // path of the audio File, which is found in res \ raw \ recording_old.3gpp, put it in the root directory of external storage. Used to test private FileInputStream fiis = null; private FileOutputStream fos = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_voice_encryption); mPlayButton = (Button) findViewById (R. id. playButton); mPlayButton. setOnClickListener (this); mEncryptionButton = (Button) findViewById (R. id. encryptionButton); mEncryptionButton. setOnC LickListener (this); mDecryptionButton = (Button) findViewById (R. id. decryptionButton); mDecryptionButton. setOnClickListener (this) ;}@ SuppressWarnings ("static-access") @ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. playButton: if (mPlayer! = Null) {mPlayer. release (); mPlayer = null;} // mPlayer = MediaPlayer. create (this, R. raw. recording_old); boolean isSuccess = true; try {FD = new FileInputStream (oldFile); mPlayer = new MediaPlayer (); mPlayer. setDataSource (FCM. getFD (); mPlayer. prepare (); // an error occurs when mPlayer is removed. start ();} catch (FileNotFoundException e) {isSuccess = false; e. printStackTrace ();} catch (IllegalArgumentException e) {isSuccess = false; E. printStackTrace ();} catch (IllegalStateException e) {isSuccess = false; e. printStackTrace ();} catch (IOException e) {isSuccess = false; e. printStackTrace ();} finally {try {FCM. close ();} catch (IOException e) {e. printStackTrace () ;}} if (! IsSuccess) Toast. makeText (this, "playback failed", Toast. LENGTH_SHORT ). show (); break; case R. id. encryptionButton: // encrypt and save isSuccess = true; try {FD = new FileInputStream (oldFile); byte [] oldByte = new byte [(int) oldFile. length ()]; FCM. read (oldByte); // read byte [] newByte = AESUtils. encryptVoice (seed, oldByte); // encryption fos = new FileOutputStream (oldFile); fos. write (newByte);} catch (FileNotFoundException e) {isSuccess = false; e. printStackTrace ();} catch (IOException e) {isSuccess = false; e. printStackTrace ();} catch (Exception e) {isSuccess = false; e. printStackTrace ();} finally {try {FCM. close (); fos. close ();} catch (IOException e) {e. printStackTrace () ;}} if (isSuccess) Toast. makeText (this, "encrypted", Toast. LENGTH_SHORT ). show (); elseToast. makeText (this, "encryption failed", Toast. LENGTH_SHORT ). show (); Log. I (TAG, "saved successfully"); break; case R. id. decryptionButton: // decrypt and save isSuccess = true; byte [] oldByte = new byte [(int) oldFile. length ()]; try {fiis = new FileInputStream (oldFile); fiis. read (oldByte); byte [] newByte = AESUtils. decryptVoice (seed, oldByte); // decrypt fos = new FileOutputStream (oldFile); fos. write (newByte);} catch (FileNotFoundException e) {isSuccess = false; e. printStackTrace ();} catch (IOException e) {isSuccess = false; e. printStackTrace ();} catch (Exception e) {isSuccess = false; e. printStackTrace ();} try {FS. close (); fos. close ();} catch (IOException e) {e. printStackTrace ();} if (isSuccess) Toast. makeText (this, "decrypted", Toast. LENGTH_SHORT ). show (); elseToast. makeText (this, "decryption failed", Toast. LENGTH_SHORT ). show (); break; default: break ;}}}

AESUtils. java

package com.example.voiceencryption;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;public class AESUtils {public static byte[] encryptVoice(String seed, byte[] clearbyte)throws Exception {byte[] rawKey = getRawKey(seed.getBytes());byte[] result = encrypt(rawKey, clearbyte);return result;}public static byte[] decryptVoice(String seed, byte[] encrypted)throws Exception {byte[] rawKey = getRawKey(seed.getBytes());byte[] result = decrypt(rawKey, encrypted);return result;}private static byte[] getRawKey(byte[] seed) throws Exception {KeyGenerator kgen = KeyGenerator.getInstance("AES");SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");sr.setSeed(seed);kgen.init(128, sr);SecretKey skey = kgen.generateKey();byte[] raw = skey.getEncoded();return raw;}private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));byte[] encrypted = cipher.doFinal(clear);return encrypted;}private static byte[] decrypt(byte[] raw, byte[] encrypted)throws Exception {SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));byte[] decrypted = cipher.doFinal(encrypted);return decrypted;}}

Source code: http://download.csdn.net/detail/u012964281/8233079


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.