java程序如何使用openssl
答案:4 悬赏:80 手机版
解决时间 2021-01-03 07:32
- 提问者网友:niaiwoma
- 2021-01-03 03:46
java程序如何使用openssl
最佳答案
- 五星知识达人网友:鱼芗
- 2021-01-06 23:24
您好,这样的:
public static byte[] decryptbyprivatekey(byte[] encrypteddata, string privatekey)
throws exception {
//获取私钥
byte[] keybytes = base64utils.decode(privatekey);
pkcs8encodedkeyspec pkcs8keyspec = new pkcs8encodedkeyspec(keybytes);
keyfactory keyfactory = keyfactory.getinstance(key_algorithm);
key privatek = keyfactory.generateprivate(pkcs8keyspec);
cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding");
cipher.init(cipher.decrypt_mode, privatek);
int inputlen = encrypteddata.length;
bytearrayoutputstream out = new bytearrayoutputstream();
int offset = 0;
byte[] cache = null;
int i = 0;
// 对数据分段解密
while (inputlen - offset > 0) {
if (inputlen - offset > 128) {
cache = cipher.dofinal(encrypteddata, offset, 128);
} else {
cache = cipher.dofinal(encrypteddata, offset, inputlen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * max_decrypt_block;
}
byte[] decrypteddata = out.tobytearray();
out.close();
return decrypteddata;
}
public static byte[] hexstringtobytes(string hexstring) {
if (hexstring == null || hexstring.equals("")) {
return null;
}
hexstring = hexstring.touppercase();
int length = hexstring.length() / 2;
char[] hexchars = hexstring.tochararray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (chartobyte(hexchars[pos]) << 4 | chartobyte(hexchars[pos + 1]));
}
return d;
}
private static byte chartobyte(char c) {
return (byte) "0123456789abcdef".indexof(c);
}
public static byte[] decryptbyprivatekey(byte[] encrypteddata, string privatekey)
throws exception {
//获取私钥
byte[] keybytes = base64utils.decode(privatekey);
pkcs8encodedkeyspec pkcs8keyspec = new pkcs8encodedkeyspec(keybytes);
keyfactory keyfactory = keyfactory.getinstance(key_algorithm);
key privatek = keyfactory.generateprivate(pkcs8keyspec);
cipher cipher = cipher.getinstance("rsa/ecb/pkcs1padding");
cipher.init(cipher.decrypt_mode, privatek);
int inputlen = encrypteddata.length;
bytearrayoutputstream out = new bytearrayoutputstream();
int offset = 0;
byte[] cache = null;
int i = 0;
// 对数据分段解密
while (inputlen - offset > 0) {
if (inputlen - offset > 128) {
cache = cipher.dofinal(encrypteddata, offset, 128);
} else {
cache = cipher.dofinal(encrypteddata, offset, inputlen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * max_decrypt_block;
}
byte[] decrypteddata = out.tobytearray();
out.close();
return decrypteddata;
}
public static byte[] hexstringtobytes(string hexstring) {
if (hexstring == null || hexstring.equals("")) {
return null;
}
hexstring = hexstring.touppercase();
int length = hexstring.length() / 2;
char[] hexchars = hexstring.tochararray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (chartobyte(hexchars[pos]) << 4 | chartobyte(hexchars[pos + 1]));
}
return d;
}
private static byte chartobyte(char c) {
return (byte) "0123456789abcdef".indexof(c);
}
全部回答
- 1楼网友:骨子里都是戏
- 2021-01-07 01:35
同求,也遇见这个问题
- 2楼网友:長槍戰八方
- 2021-01-07 00:46
打酱油的,忽略!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯