密码学 13 数字签名算法
文章目录
简介:
对目标先进行消息摘要算法,得到的结果再进行RSA加密。
特点:
这种情况下的RSA往往是NoPadding 模式。保证每次的加密结果一致。
| 算法 | 秘钥长度 | 秘钥长度默认值 | 签名长度 | 备注 |
|---|---|---|---|---|
| MD2withRSA | 512~65535位(秘钥长度必须是64的倍数) | 1024 | 与秘钥长度相同 | java6实现 |
| MD5withRSA | 同上 | 同上 | 与秘钥长度相同 | 同上 |
| SHA1withRSA | 同上 | 同上 | 与秘钥长度相同 | 同上 |
| SHA224withRSA | 同上 | 2048 | 与秘钥长度相同 | Bouncy Castle实现 |
| SHA256withRSA | 同上 | 同上 | 与秘钥长度相同 | 同上 |
| SHA384withRSA | 同上 | 同上 | 与秘钥长度相同 | 同上 |
| SHA512withRSA | 同上 | 同上 | 与秘钥长度相同 | 同上 |
| RIPEMD128withRSA | 同上 | 同上 | 与秘钥长度相同 | 同上 |
| RIPEMD160withRSA | 同上 | 同上 | 与秘钥长度相同 | 同上 |
代码实现
public static String getSignature(String data) throws Exception {
PrivateKey privateKey = RSABase64.genPrivatekey();
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
byte[] res= signature.sign();
ByteString of = ByteString.of(res);
return of.base64();
};
public static boolean verifySignature (String data, String sign) throws Exception {
PublicKey publicKey = RSABase64.genPublickey();
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initVerify(publicKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
return signature.verify(ByteString.decodeBase64(sign).toByteArray());
};
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论