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

RSA 塊的數據過多失敗.什么是 PKCS#7?

Too much data for RSA block fail. What is PKCS#7?(RSA 塊的數據過多失敗.什么是 PKCS#7?)
本文介紹了RSA 塊的數據過多失敗.什么是 PKCS#7?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

javax.crypto.Cipher

我嘗試使用 Cipher.getInstance("RSA/None/NoPadding", "BC") 加密數據,但出現異常:

I was trying to encrypt data using Cipher.getInstance("RSA/None/NoPadding", "BC") but I got the exception:

ArrayIndexOutOfBoundsException: too much data for RSA block

看起來與NoPadding"有關,因此,閱讀有關填充的內容,看起來 CBC 是在這里使用的最佳方法.

Looks like is something related to the "NoPadding", so, reading about padding, looks like CBC is the best approach to use here.

我在谷歌上找到了一些關于RSA/CBC/PKCS#7"的東西,這個PKCS#7"是什么?以及為什么它沒有列在 sun 的標準算法名稱?

I found at google something about "RSA/CBC/PKCS#7", what is this "PKCS#7"? And why its not listed on sun's standard algorithm names?

更新:

我想知道,如果是填充問題,為什么這個例子運行得很好?

I'm wondering, if is a padding problem, why this example run just fine?

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;

/**
 * Basic RSA example.
 */
public class BaseRSAExample
{
    public static void main(
        String[]    args)
        throws Exception
    {
        byte[]           input = new byte[] { (byte)0xbe, (byte)0xef };
        Cipher          cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");
        KeyFactory       keyFactory = KeyFactory.getInstance("RSA", "BC");

        // create the keys

        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
                new BigInteger("d46f473a2d746537de2056ae3092c451", 16),
                new BigInteger("11", 16));
        RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(
                new BigInteger("d46f473a2d746537de2056ae3092c451", 16),  
                new BigInteger("57791d5430d593164082036ad8b29fb1", 16));

        RSAPublicKey pubKey = (RSAPublicKey)keyFactory.generatePublic(pubKeySpec);
        RSAPrivateKey privKey = (RSAPrivateKey)keyFactory.generatePrivate(privKeySpec);

        // encryption step

        cipher.init(Cipher.ENCRYPT_MODE, pubKey);

        byte[] cipherText = cipher.doFinal(input);

        // decryption step

        cipher.init(Cipher.DECRYPT_MODE, privKey);

        byte[] plainText = cipher.doFinal(cipherText);

    }
}

更新 2:

我意識到即使我只使用 Cipher.getInstance("RSA", "BC") 也會引發相同的異常.

I realized that even if I use just Cipher.getInstance("RSA", "BC") it throws the same exception.

推薦答案

如果你使用分組密碼,你輸入的必須是分組比特長度的精確倍數.

If you use a block cipher, you input must be an exact multiple of the block bit length.

為了加密任意長度的數據,您首先需要將數據填充到塊長度的倍數.這可以用任何方法完成,但有許多標準.PKCS7 是一個很常見的,你可以在關于 padding 的維基百科文章中查看 概述.

In order to encipher arbitrary length data, you need first to pad you data to a multiple of the block length. This can be done with any method, but there are a number of standards. PKCS7 is one which is quite common, you can see an overview on the wikipedia article on padding.

由于塊密碼器對塊進行操作,因此您還需要想出一種連接加密塊的方法.這非常重要,因為幼稚的技術大大降低了加密的強度.還有一篇關于此的維基百科文章.

Since block cipers operate on blocks, you also need to come up with a way of concatenating the encrypted blocks. This is very important, since naive techniques greatly reduce the strength of the encryption. There is also a wikipedia article on this.

您所做的是嘗試加密(或解密)長度與密碼的塊長度不匹配的數據,并且您還明確要求不進行填充和鏈接操作模式.

What you did was to try to encrypt (or decrypt) data of a length which didn't match the block length of the cipher, and you also explicitly asked for no padding and also no chaining mode of operation.

因此,分組密碼無法應用于您的數據,并且您收到了報告的異常.

Consequently the block cipher could not be applied to your data, and you got the reported exception.

更新:

作為對您的更新和 GregS 評論的回應,我想承認 GregS 是對的(我不知道 RSA),并詳細說明一下:

As a response to your update and GregS's remark, I would like to acknowledge that GregS was right (I did not know this about RSA), and elaborate a bit:

RSA 不對位進行操作,而是對整數進行操作.因此,為了使用 RSA,您需要將您的字符串消息轉換為整數 m:0 <米<n,其中 n 是在生成過程中選擇的兩個不同素數的模數.RSA 算法中密鑰的大小通常是指n.有關這方面的更多詳細信息,請參閱關于 RSA 的維基百科文章.

RSA does not operate on bits, it operates on integer numbers. In order to use RSA you therefore need to convert your string message into an integer m: 0 < m < n, where n is the modulus of the two distinct primes chosen in the generation process. The size of a key in the RSA algorithm typically refers to n. More details on this can be found on the wikipedia article on RSA.

將字符串消息轉換為整數而不丟失(例如截斷初始零)的過程,通常遵循 PKCS#1 標準.此過程還為消息完整性(哈希摘要)、語義安全(IV)等添加了一些其他信息.有了這個額外的數據,可以提供給 RSA/None/PKCS1Padding 的最大字節數是 (keylength - 11).我不知道 PKCS#1 如何將輸入數據映射到輸出整數范圍,但是我的印象是它可以輸入小于或等于 keylength - 11 的任何長度,并為 RSA 加密生成一個有效的整數.

The process of converting a string message to an integer, without loss (for instance truncating initial zeroes), the PKCS#1 standard is usually followed. This process also adds some other information for message integrity (a hash digest), semantical security (an IV) ed cetera. With this extra data, the maximum number of bytes which can be supplied to the RSA/None/PKCS1Padding is (keylength - 11). I do not know how PKCS#1 maps the input data to the output integer range, but my impression is that it can take any length input less than or equal to keylength - 11 and produce a valid integer for the RSA encryption.

如果您不使用填充,您的輸入將被簡單地解釋為一個數字.您的示例輸入 {0xbe, 0xef} 很可能會被解釋為 {10111110 +o 11101111} = 1011111011101111_2 = 48879_10 = beef_16(原文如此!).由于 0 <牛肉_16

If you use no padding, your input will simply be interpreted as a number. Your example input, {0xbe, 0xef} will most probably be interpreted as {10111110 +o 11101111} = 1011111011101111_2 = 48879_10 = beef_16 (sic!). Since 0 < beef_16 < d46f473a2d746537de2056ae3092c451_16, your encryption will succeed. It should succeed with any number less than d46f473a2d746537de2056ae3092c451_16.

bouncycastle 常見問題解答中提到了這一點.他們還聲明了以下內容:

This is mentioned in the bouncycastle FAQ. They also state the following:

附帶的 RSA 實現充氣城堡只允許加密單個數據塊.RSA算法不適合流數據,不應使用那樣.在這種情況下你應該使用加密數據隨機生成的密鑰和一個對稱的密碼,之后你應該加密使用 RSA 隨機生成的密鑰,然后發送加密數據和對方的加密隨機密鑰結束他們可以逆轉過程的地方(即使用解密隨機密鑰他們的 RSA 私鑰,然后解密數據).

The RSA implementation that ships with Bouncy Castle only allows the encrypting of a single block of data. The RSA algorithm is not suited to streaming data and should not be used that way. In a situation like this you should encrypt the data using a randomly generated key and a symmetric cipher, after that you should encrypt the randomly generated key using RSA, and then send the encrypted data and the encrypted random key to the other end where they can reverse the process (ie. decrypt the random key using their RSA private key and then decrypt the data).

這篇關于RSA 塊的數據過多失敗.什么是 PKCS#7?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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(使用線程逐塊處理文件)
主站蜘蛛池模板: 2019精品手机国产品在线 | 成人精品鲁一区一区二区 | aaaa网站| 免费久久网站 | 日韩在线观看网站 | 99精品免费久久久久久久久日本 | 中文字幕二区 | 激情毛片 | 中文字幕精品一区二区三区精品 | av在线免费观看不卡 | 久草网站| 日本激情一区二区 | 久久精品手机视频 | 天天插天天搞 | av在线免费看网址 | 国产精品不卡 | av大片在线观看 | 国产精品一区二区在线免费观看 | 亚洲精品一区二区 | 日韩精品中文字幕在线 | 性欧美精品一区二区三区在线播放 | 亚洲欧美日韩精品久久亚洲区 | 久久中文字幕视频 | av在线视| 国产精品九九视频 | 欧美亚洲激情 | 日韩欧美视频在线 | 麻豆av电影网 | 亚洲高清视频一区二区 | 一区二区三区在线 | 国产免费一区 | 亚洲精品成人av | 91精品在线看 | 国产精品久久一区二区三区 | 久久亚洲综合 | 欧美天堂 | 欧美999 | 午夜爽爽爽男女免费观看影院 | 午夜99| 逼逼网 | 久久亚洲国产精品 |