package com.fyn.algorithm.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class IDEAUtils {
/**
* 密钥算法
*/
public static final String KEY_ALGORITHM = "IDEA";
/**
* 加密/解密算法 / 工作模式 / 填充方式
*/
public static final String CIPHER_ALGORITHM = "IDEA/ECB/ISO10126Padding";
/**
* 转换密钥
* @param key 二进制密钥
* @return Key 密钥
*/
public static Key toKey(byte[] key){
SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
return secretKey;
}
/**
* 生成密钥
* @return
*/
public static byte[] initKey(){
//加入BouncyCastleProvider支持
Security.addProvider(new BouncyCastleProvider());
try {
//实例化密钥生成器
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
//初始化密钥生成器
kg.init(128);
//生成私密密钥
SecretKey secretKey = kg.generateKey();
//获得密钥的二进制编码形式
return secretKey.getEncoded();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* IDEA加密文件
* @param inFile
* @param outFilePath
* @param outFileName
* @param key
*/
public static File encrypt(File inFile, String outFilePath, String outFileName, byte[] key){
//加入BouncyCastleProvider支持
Security.addProvider(new BouncyCastleProvider());
//还原密钥
Key k = toKey(key);
FileInputStream fis = null;
FileOutputStream fos = null;
CipherInputStream cis = null;
File outFile = new File(outFilePath, outFileName);
byte[] buffer = new byte[1024*10];
int length;
try {
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
//实例化Cipher
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,设置为加密模式
cipher.init(Cipher.ENCRYPT_MODE, k);
//用加密流包装文件流
cis = new CipherInputStream(fis, cipher);
//开始加密,以加密流读取文件,以文件流写文件
while((length = cis.read(buffer)) != -1){
fos.write(buffer, 0, length);
}
return outFile;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(cis != null){
try {
cis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
public static File decrypt(File inFile, String outFilePath, String outFileName, byte[] key){
//加入BouncyCastleProvider支持
Security.addProvider(new BouncyCastleProvider());
//还原密钥
Key k = toKey(key);
FileInputStream fis = null;
FileOutputStream fos = null;
CipherOutputStream cos = null;
File outFile = new File(outFilePath, outFileName);
byte[] buffer = new byte[1024*10];
int length;
try {
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
//实例化Cipher
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, k);
//用加密流包装文件流
cos = new CipherOutputStream(fos, cipher);
//开始解密,以文件读取文件,以解密流写文件
while((length = fis.read(buffer)) != -1){
cos.write(buffer, 0, length);
}
return outFile;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(cos != null){
try {
cos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
}
已经在工程里导入了bcprov-jdk15on-152.jar
上述代码作为一个java程序可以正常运行,可以初始化密钥,但是放在Android里初始化密钥失败。
在Android应用里的mainActivity中执行IDEAUtils.initKey()方法时,出现nosuchalgorithmexception: keygenerator IDEA implementation not found
![](http://img.bbs.csdn.net/upload/201505/09/1431147949_455889.png)