Android 加密/解密音頻檔案(AES)

來源:互聯網
上載者:User

標籤:android   java   aes   語音加密   

加密過程:以byte[]形式讀取SD卡上準備好的測試音頻檔案,使用AES密碼編譯演算法加密byte[],再儲存覆蓋原音頻檔案,此時加密後的音頻檔案無法被播放。解密和加密過程原理一樣,解密儲存後的音頻檔案可以被播放。


代碼:

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";private static final String seed = "guess"; // 種子private MediaPlayer mPlayer;private Button mPlayButton;private Button mEncryptionButton;private Button mDecryptionButton;private File sdCard = Environment.getExternalStorageDirectory();private File oldFile = new File(sdCard, "recording_old.3gpp");// 音頻檔案的路徑,在res\raw\recording_old.3gpp中找到音頻檔案,再放到外部儲存的根目錄下。用於測試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(oldFile);mPlayer = new MediaPlayer();mPlayer.setDataSource(fis.getFD());mPlayer.prepare(); // 去掉會出錯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.printStackTrace();} finally {try {fis.close();} catch (IOException e) {e.printStackTrace();}}if (!isSuccess)Toast.makeText(this, "播放失敗", Toast.LENGTH_SHORT).show();break;case R.id.encryptionButton:// 加密儲存isSuccess = true;try {fis = new FileInputStream(oldFile);byte[] oldByte = new byte[(int) oldFile.length()];fis.read(oldByte); // 讀取byte[] newByte = AESUtils.encryptVoice(seed, oldByte);// 加密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 {fis.close();fos.close();} catch (IOException e) {e.printStackTrace();}}if (isSuccess)Toast.makeText(this, "加密成功", Toast.LENGTH_SHORT).show();elseToast.makeText(this, "加密失敗", Toast.LENGTH_SHORT).show();Log.i(TAG, "儲存成功");break;case R.id.decryptionButton:// 解密儲存isSuccess = true;byte[] oldByte = new byte[(int) oldFile.length()];try {fis = new FileInputStream(oldFile);fis.read(oldByte);byte[] newByte = AESUtils.decryptVoice(seed, oldByte);// 解密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 {fis.close();fos.close();} catch (IOException e) {e.printStackTrace();}if (isSuccess)Toast.makeText(this, "解密成功", Toast.LENGTH_SHORT).show();elseToast.makeText(this, "解密失敗", 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;}}

源碼:http://download.csdn.net/detail/u012964281/8233079


Android 加密/解密音頻檔案(AES)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.