RSA數字簽名

加密算法

1、簡介

數字簽名用於驗證消息發送者的身份,確保其餘人沒法僞造身份。java

2、原理

數字簽名基於非對稱加密算法,利用只有擁有者纔有私鑰的特性(這能夠標識身份)進行的。git

一、數字簽名的生成

對發送內容先生成有限長度的摘要,再使用私鑰進行加密,進而生成數字簽名。算法

二、數字簽名驗證

用公鑰對數字簽名進行解密獲取加密內容(其實也就是摘要),再用與發送方相同的摘要算法對發送內空生成摘要,api

再將這二者進行比較,若相等,則驗證成功,不然失敗。編碼

3、代碼實例

在此使用java自帶的數字簽名api進行演示,包括MD5withRSA和SHA1withRSA兩種方式,簽名使用base64編碼加密

public class DigitalSignatureMain {
    public static void main(String[] args)  throws Exception {
        String content = "study hard and make progress everyday";
        System.out.println("content :"+content);

        KeyPair keyPair = getKeyPair();
        PublicKey publicKey =  keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        String md5Sign  = getMd5Sign(content,privateKey);
        System.out.println("sign with md5 and rsa :"+ md5Sign);
        boolean md5Verifty = verifyWhenMd5Sign(content,md5Sign,publicKey);
        System.out.println("verify sign with md5 and rsa :"+ md5Verifty);

        String sha1Sign  = getSha1Sign(content,privateKey);
        System.out.println("sign with sha1 and rsa :"+ sha1Sign);
        boolean sha1Verifty = verifyWhenSha1Sign(content,sha1Sign,publicKey);
        System.out.println("verify sign with sha1 and rsa :"+ sha1Verifty);

    }

    //生成密鑰對
    static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(512); //能夠理解爲:加密後的密文長度,實際原文要小些 越大 加密解密越慢
        KeyPair keyPair = keyGen.generateKeyPair();
        return keyPair;
    }

    //用md5生成內容摘要,再用RSA的私鑰加密,進而生成數字簽名
    static String getMd5Sign(String content , PrivateKey privateKey) throws Exception {
        byte[] contentBytes = content.getBytes("utf-8");
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initSign(privateKey);
        signature.update(contentBytes);
        byte[] signs = signature.sign();
        return Base64.encodeBase64String(signs);
    }

    //對用md5和RSA私鑰生成的數字簽名進行驗證
    static boolean verifyWhenMd5Sign(String content, String sign, PublicKey publicKey) throws Exception {
        byte[] contentBytes = content.getBytes("utf-8");
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initVerify(publicKey);
        signature.update(contentBytes);
        return signature.verify(Base64.decodeBase64(sign));
    }

    //用sha1生成內容摘要,再用RSA的私鑰加密,進而生成數字簽名
    static String getSha1Sign(String content , PrivateKey privateKey) throws Exception {
        byte[] contentBytes = content.getBytes("utf-8");
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initSign(privateKey);
        signature.update(contentBytes);
        byte[] signs = signature.sign();
        return Base64.encodeBase64String(signs);
    }

    //對用md5和RSA私鑰生成的數字簽名進行驗證
    static boolean verifyWhenSha1Sign(String content, String sign, PublicKey publicKey) throws Exception {
        byte[] contentBytes = content.getBytes("utf-8");
        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initVerify(publicKey);
        signature.update(contentBytes);
        return signature.verify(Base64.decodeBase64(sign));
    }
}
相關文章
相關標籤/搜索