Android 오디 오 파일 암호 화 / 복호화 (AES)
코드:
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 implements
OnClickListener {
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;
@Override
protected 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")
@Override
public 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();
else
Toast.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();
else
Toast.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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.