资源简介
BouncyCastleProvider jar包,如果有引入该类包报错的,可以下载,亲测可用,测试代码附上,有问题请reply to me
代码片段和文件信息
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.objectInputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyPair;
import javax.crypto.Cipher;
public class Test {
public static void main(String[] args) throws Exception {
/* 1
SecureRandom sr = new SecureRandom();
KeyPairGenerator kg = KeyPairGenerator.getInstance(“RSA“new org.bouncycastle.jce.provider.BouncyCastleProvider());
//注意密钥大小最好为1024否则解密会有乱码情况.
kg.initialize(1024 sr);
FileOutputStream fos = new FileOutputStream(“C:/RSAKey.xml“);
objectOutputStream oos = new objectOutputStream(fos);
//生成密钥
oos.writeobject(kg.generateKeyPair().getPrivate());
oos.close();
*/
/* 2
Key kp;
String fileName = “RSAKey.xml“;
InputStream is = Test.class.getClassLoader().getResourceAsStream(fileName);
objectInputStream oos = new objectInputStream(is);
kp = (Key) oos.readobject();
oos.close();
encryptFile(“C:/bsmain_runtime.log““C:/encry.log“kp);
*/
}
public static void encryptFile(String srcFileName String destFileNameKey publicKkey) throws Exception {
OutputStream outputWriter = null;
InputStream inputReader = null;
try {
Cipher cipher = Cipher.getInstance(“RSA“ new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] buf = new byte[100];
int bufl;
cipher.init(Cipher.ENCRYPT_MODE publicKkey);
System.out.println(cipher.getBlockSize()+“================“);
outputWriter = new FileOutputStream(destFileName);
inputReader = new FileInputStream(srcFileName);
while ((bufl = inputReader.read(buf)) != -1) {
byte[] encText = null;
byte[] newArr = null;
if (buf.length == bufl) {
newArr = buf;
} else {
newArr = new byte[bufl];
for (int i = 0; i < bufl; i++) {
newArr[i] = (byte) buf[i];
}
}
encText = cipher.doFinal(newArr);
outputWriter.write(encText);
}
outputWriter.flush();
} catch (Exception e) {
throw e;
} finally {
try {
if (outputWriter != null) {
outputWriter.close();
}
if (inputReader != null) {
inputReader.close();
}
} catch (Exception e) {
}
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3011 2009-06-29 22:13 Test.java
文件 1632822 2009-06-29 21:50 bcprov-jdk15-143.jar
----------- --------- ---------- ----- ----
1635833 2
- 上一篇:文件预览方案
- 下一篇:JSP学生成绩管理系统源代码
相关资源
- 齐全的Bouncycastle--jar文件
- Android 基于ContentProvider 的音乐播放器
- android系统 ContactsProvider模块
- Android SM2、SM3、SM4 算法支持 Service P
- JAVA数据加密jar包之sunjce_provider.rar
- jar.zip sunjce_provider.jar包
- android content provider程序简单记账
- bouncycastle.jar
- 内涵FileProvider的android-support-v4包,版
- bcprov-jdk15-1.43.jar Bouncy Castle加密包
- bcprov-jdk15on-156.jar
- 加密解密字符串找不到 com.sun.crypto.
- sunjce_provider.jar
- android 从外部程序获得ContentProvider提供
- sunjce_provider JAVA数据加密
- 安卓日历小部件源码AppWidgetProvider
评论
共有 条评论