50,523
社区成员
发帖
与我相关
我的任务
分享
KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key=keyGen.generateKeyPair();
PublicKey pub_key = key.getPublic();
PrivateKey pri_key = key.getPrivate();
// 读入需加密文件
File file = new File(infoPath);
FileInputStream is = new FileInputStream(file);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] tmpbuf = new byte[1024];
int count = 0;
while ((count = is.read(tmpbuf)) != -1){
bout.write(tmpbuf, 0, count);
tmpbuf = new byte[1024];
}
is.close();
// 加密
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE,pubkey);
byte[] cipherText=cipher.doFinal(bout.toString().getBytes());
// 生成加密后文件
if(retPath==null || retPath.equals("")){
retPath = infoPath + ".enc";
}
file = new File(retPath);
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(cipherText);
out.close();
System.out.println(bout.toString().getBytes().length);
打印出来的结果是68845,丢失了1部分数据,这个是怎么回事呢?