IDEA加密算法在Android上运行出错,在windows上可以运行


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值