4
我想,因为这很有用页面上的方法在Java中一些二进制数据使用公钥加密: http://www.junkheap.net/content/public_key_encryption_java我CipherOutputStream静静地失败
所指示的页面,我创建使用命令公钥和私钥:
openssl genrsa -aes256 -out private.pem 2048
openssl rsa -in private.pem -pubout -outform DER -out public.der
现在我保存了一个小程序加密一些数据:
public class Rsa {
public static void main(String[] args) throws Exception, IOException {
File keyFile = new File("public.der");
byte[] encodedKey = new byte[(int) keyFile.length()];
new FileInputStream(keyFile).read(encodedKey);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pk = kf.generatePublic(publicKeySpec);
Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, pk);
FileOutputStream fileOutputStream = new FileOutputStream(
"encrypted.rsa");
OutputStream os = new CipherOutputStream(fileOutputStream, rsa);
byte[] raw = new byte[245];
raw[0] = 4;
os.write(raw);
os.flush();
os.close();
}
}
上面的代码工作,但WH我将字节数组的大小更改为246,它会生成一个零长度的文件!
我在做什么错?
感谢格雷格 - 我希望我知道,4小时前! – Mark 2010-02-08 13:28:41