Encryption process: Read the prepared test audio file on SD card in byte[] form, encrypt byte[with AES encryption algorithm, and then save overwrite original audio file, the encrypted audio file cannot be played at this time. Decryption and encryption process the same principle, decrypt the saved 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 Android.widget.button;import Android.widget.toast;public class Voiceencryptionactivity extends Activity Implementsonclicklistener {private static final string TAG = "Voiceencryptionactivity";p rivate static final String seed = "Guess"; Seed Private MediaPlayer mplayer;private button mplaybutton;private button mencryptionbutton;private button Mdecryptionbutton;private file SDcard = environment.getexternalstoragedirectory ();p rivate file OldFile = new file ( SDcard, "RECORDING_OLD.3GPP");//The path to the audio file, locate the audio file in res\raw\recording_old.3gpp, and place it in the root directory of the external store. For testing private FileInputStream FIS = 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.setonclicklistener (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 {fis = new FileInputStream (oldfil e); mPlayer = new MediaPlayer (); Mplayer.setdatasource (FIS.GETFD ()); Mplayer.prepare (); Get rid of Errors Mplayer.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.print StackTrace ();} Finally {try {fis.close ();} catch (IOException e) {e.printstacktrace ()}} if (!issuccess) Toast.maketext (this, "Playback failed", Toast.length_short). Show (); Break;case r.id.encryptionbutton:// Encryption Save issuccess = true;try {fis = new FileInputStream (oldFile); byte[] Oldbyte = new byte[(int) oldfile.length ()];fis.read (o Ldbyte); Read Byte[] newbyte = Aesutils.encryptvoice (seed, oldbyte);//Encrypt FOS = new FileOutputStream (oldFile); Fos.write (newbyte);} catch (FileNotFoundException e) {issuccess = False;e.printstacktrace ();} catch (IOException e) {issuccess = False;e.print StackTrace ();} catch (Exception e) {issuccess = False;e.printstacktrace ();} finally {try {fis.close (); Fos.close ();} catch (IOException E ) {e.printstacktrace ();}} if (issuccess) Toast.maketext (this, "cryptographic success", Toast.length_short). Show (); Elsetoast.maketext (This, "Encryption failed", Toast.length _short). Show ();LOG.I (TAG, "Save succeeded"); Break;case r.id.decryptionbutton://decryption Save issuccess = true;byte[] Oldbyte = new byte[(int) Oldfile.length ()];try {fis = new FileInputStream (oldFile); Fis.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.print StackTrace ();} catch (Exception e) {issuccess = False;e.printstacktrace ();} try {fis.close (); Fos.close ();} catch (IOException e) {e.printstacktrace ();} if (issuccess) Toast.maketext (this, "decryption succeeds", 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 (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 (RA W, "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: http://download.csdn.net/detail/u012964281/8233079
Android encryption/decryption audio file (AES)