久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SHA1 和 RSA 與 java.security.Signature 對比 MessageDigest

Using SHA1 and RSA with java.security.Signature vs. MessageDigest and Cipher(SHA1 和 RSA 與 java.security.Signature 對比 MessageDigest 和 Cipher)
本文介紹了SHA1 和 RSA 與 java.security.Signature 對比 MessageDigest 和 Cipher的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我試圖了解 Java java.security.Signature 類的作用.如果我計算一個 SHA1 消息摘要,然后使用 RSA 加密該摘要,我會得到與要求 Signature 類簽署相同內容不同的結果:

I'm trying to understand what the Java java.security.Signature class does. If I compute an SHA1 message digest, and then encrypt that digest using RSA, I get a different result to asking the Signature class to sign the same thing:

// Generate new key
KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
String plaintext = "This is the message being signed";

// Compute signature
Signature instance = Signature.getInstance("SHA1withRSA");
instance.initSign(privateKey);
instance.update((plaintext).getBytes());
byte[] signature = instance.sign();

// Compute digest
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
byte[] digest = sha1.digest((plaintext).getBytes());

// Encrypt digest
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] cipherText = cipher.doFinal(digest);

// Display results
System.out.println("Input data: " + plaintext);
System.out.println("Digest: " + bytes2String(digest));
System.out.println("Cipher text: " + bytes2String(cipherText));
System.out.println("Signature: " + bytes2String(signature));

結果(例如):

輸入數據:這是正在簽名的消息
摘要:62b0a9ef15461c82766fb5bdaae9edbe4ac2e067
密文:057dc0d2f7f54acc95d3cf5cba9f944619394711003bdd12...
簽名:7177c74bbbb871cc0af92e30d2808ebae146f25d3fd8ba1622...

Input data: This is the message being signed
Digest: 62b0a9ef15461c82766fb5bdaae9edbe4ac2e067
Cipher text: 057dc0d2f7f54acc95d3cf5cba9f944619394711003bdd12...
Signature: 7177c74bbbb871cc0af92e30d2808ebae146f25d3fd8ba1622...

我一定對 Signature 正在做的事情有一個根本性的誤解 - 我已經對其進行了跟蹤,它似乎正在對 MessageDigest 對象調用更新,其中如我所料,算法設置為 SHA1,然后獲取摘要,然后進行加密.是什么導致結果不同?

I must have a fundamental misunderstanding of what Signature is doing - I've traced through it, and it appears to be calling update on a MessageDigest object, with the algorithm set to SHA1 as I would expect, then getting the digest, then doing the encryption. What's making the results differ?

Leonidas 讓我檢查簽名方案是否應該按照我的想法去做.RFC 中定義了兩種類型的簽名:

Leonidas made me check whether the signature scheme is supposed to do what I think it does. There are two types of signature defined in the RFC:

  • RSSA-PKCS1-v1_5
  • RSSA-PSS

第一個 (PKCS1) 是我在上面描述.它使用哈希函數創建摘要,然后使用私鑰對結果進行加密.

The first of these (PKCS1) is the one I describe above. It uses a hash function to create a digest, and then encrypts the result with a private key.

第二種算法使用隨機鹽值,并且更安全但不確定.如果重復使用相同的密鑰,上面代碼產生的簽名不會改變,所以我認為它不可能是PSS.

The second algorithm uses a random salt value, and is more secure but non-deterministic. The signature produced from the code above does not change if the same key is used repeatedly, so I don't think it can be PSS.

這是我使用的 bytes2string 方法:

Here's the bytes2string method I was using:

private static String bytes2String(byte[] bytes) {
    StringBuilder string = new StringBuilder();
    for (byte b : bytes) {
        String hexString = Integer.toHexString(0x00FF & b);
        string.append(hexString.length() == 1 ? "0" + hexString : hexString);
    }
    return string.toString();
}

推薦答案

好的,我已經弄清楚發生了什么.Leonidas 是對的,它不僅僅是被加密的哈希(在 Cipher 類方法的情況下),它是與摘要連接的哈希算法的 ID:

OK, I've worked out what's going on. Leonidas is right, it's not just the hash that gets encrypted (in the case of the Cipher class method), it's the ID of the hash algorithm concatenated with the digest:

  DigestInfo ::= SEQUENCE {
      digestAlgorithm AlgorithmIdentifier,
      digest OCTET STRING
  }

這就是 Cipher 和 Signature 的加密方式不同的原因.

Which is why the encryption by the Cipher and Signature are different.

這篇關于SHA1 和 RSA 與 java.security.Signature 對比 MessageDigest 和 Cipher的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 久久亚洲国产精品 | 毛片免费看 | a视频在线观看 | 国产在线精品一区二区三区 | 久久久国产一区二区三区四区小说 | 亚洲欧美国产一区二区三区 | 国产精品免费在线 | 午夜爽爽爽男女免费观看影院 | 亚洲综合久久精品 | 久久精品国产免费高清 | 区一区二在线观看 | 91在线免费视频 | 亚洲欧美激情精品一区二区 | 99re6在线视频精品免费 | 91色视频在线观看 | 小早川怜子xxxxaⅴ在线 | 亚洲国产日韩一区 | 亚洲欧洲视频 | 精品久久久久久亚洲综合网 | 亚洲电影第三页 | 欧美一区二区三区四区五区无卡码 | 亚洲一区二区国产 | 中文字幕第90页 | 一区二区中文 | 午夜在线观看视频 | 国产精品久久久久久久久久久久午夜片 | 免费人成在线观看网站 | 超碰网址 | 午夜大片 | 日本福利视频免费观看 | 国产精品一区二区三区在线 | 九色视频网站 | 亚洲欧美久久 | 久久综合伊人 | 欧美亚洲国产日韩 | 黄色毛片在线看 | 欧美电影在线观看网站 | 日韩在线小视频 | 国产区视频在线观看 | 久久黄视频 | 中文字幕av一区二区三区 |