求助java关于音频加密的问题

 我来答
育知同创教育
2018-01-02 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;

public class RSAEncrypt {

static KeyPairGenerator keyPairGen;

static KeyPair keyPair;

static RSAPrivateKey privateKey;

static RSAPublicKey publicKey;

static{
try {
keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(512);
keyPair = keyPairGen.generateKeyPair();
// Generate keys
privateKey = (RSAPrivateKey) keyPair.getPrivate();
publicKey = (RSAPublicKey) keyPair.getPublic();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
RSAEncrypt encrypt = new RSAEncrypt();
File file = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf.txt");
File newFile = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf1.txt");
encrypt.encryptFile(encrypt, file, newFile);
File file1 = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf1.txt");
File newFile1 = new File(
"C:\\Documents and Settings\\Administrator.DCB5E0D91E0D436\\桌面\\sdf2.txt");
encrypt.decryptFile(encrypt, file1, newFile1);
}

public void encryptFile(RSAEncrypt encrypt, File file, File newFile) {
try {
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(newFile);

byte[] bytes = new byte[53];
while (is.read(bytes) > 0) {
byte[] e = encrypt.encrypt(RSAEncrypt.publicKey, bytes);
bytes = new byte[53];
os.write(e, 0, e.length);
}
os.close();
is.close();
System.out.println("write success");
} catch (Exception e) {
e.printStackTrace();
}
}

public void decryptFile(RSAEncrypt encrypt, File file, File newFile) {
try {
InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(newFile);
byte[] bytes1 = new byte[64];
while (is.read(bytes1) > 0) {
byte[] de = encrypt.decrypt(RSAEncrypt.privateKey, bytes1);
bytes1 = new byte[64];
os.write(de, 0, de.length);
}
os.close();
is.close();
System.out.println("write success");

} catch (Exception e) {
e.printStackTrace();
}
}

/** */
/**
* * Encrypt String. *
*
* @return byte[]
*/
protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj) {
if (publicKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}

/** */
/**
* * Basic decrypt method *
*
* @return byte[]
*/
protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) {
if (privateKey != null) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}

加解密需要依靠以下四个属性,你可以把它存文件,存数据库都无所谓:
static KeyPairGenerator keyPairGen;

static KeyPair keyPair;

static RSAPrivateKey privateKey;

static RSAPublicKey publicKey;

以上只是示例,怎么存放根据自己情况
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式