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

java - 如何在java密碼學中將位插入塊中?

How insert bits into block in java cryptography?(java - 如何在java密碼學中將位插入塊中?)
本文介紹了java - 如何在java密碼學中將位插入塊中?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試制作一個涉及密碼學的簡單 Java 程序.

I am trying to make a simple Java program that involves cryptography.

首先,我從文件 clearmsg.txt 中讀取一個 32 字節的塊.然后我將此塊轉換為整數,并將其用于加密.不幸的是,密文的大小不是靜態的;有時它返回 30 個字節,有時返回 26 個字節.這似乎與添加操作的結果無關.

First I read a block of 32 bytes from file clearmsg.txt. Then I convert this block to integer number, and use it for encryption. Unfortunately the size of the ciphertext is not static; sometimes it returns 30 bytes and sometimes 26 bytes. This seems independent on result of add operation.

如何確保它變成 32 字節的密碼塊?如何向這個塊添加位/字節?因為當我嘗試解密這個塊時,我需要讀取 32 個密文字節.

How can I make sure it becomes a cipher block of 32 bytes? How add bits / bytes to this block? Because when I try decrypt this block I need to read 32 ciphertext bytes.

private void ENC_add() {

    final File clearmsg = new File("F:/java_projects/clearmsg.txt");
    final File ciphermsg = new File("F:/java_projects/ciphermsg.txt");
    final byte[] block = new byte[32];
    try {
        final FileInputStream fis = new FileInputStream(clearmsg);
        final FileOutputStream fcs = new FileOutputStream(ciphermsg);
        int i;
        while ((i = fis.read(block)) != -1) {
            // Is this process true
            // here M2 (Plain text) shuld be 32 byte
            M2 = new BigInteger(block);
            // here encrypt M2 by add k1 where k1 any number less than P
            CM2 = M2.add(K1).mod(P);
            // here my problem some time Cipher CM2 length 31 , some time CM2 length 32 ,some time CM2 length 30
            System.out.println("THE CM2=" + CM2.toByteArray().Length);
            fcs.write(CM2.toByteArray(), 0, i);
        }
        fcs.close();

    }

    catch (final IOException e) {
        e.printStackTrace();
    }
}

// Here problem for decrypt
private void DEC_ADD() {

    // DECREPT METHOD
    final File ciphermsg = new File("F:/java_projects/ciphermsg.txt");
    final File clearmsg = new File("F:/java_projects/rciphermsg.txt");
    final byte[] block = new byte[32];
    try {
        final FileInputStream fis = new FileInputStream(ciphermsg);
        final FileOutputStream fos = new FileOutputStream(clearmsg);
        int i;
        while ((i = fis.read(block)) != -1) {
            // CM2 NOT STATIC BITS NUMBER BECAUSE INDEPENDET ON RESULT ADDITIONAL AND PRIME NUMBER P through ENCRYPT
            // Process
            CM2 = new BigInteger(block);
            // here RM2 is decrypt cipher (CM2) NOTE When encrypt above M2 WAS 32 bytes and Cipher CM2 was 30 bytes
            // and When I read from file 32 bytes then this is my problem
            RM2 = CM2.subtract(K1).mod(P);

            fos.write(RM2.toByteArray(), 0, i);
        }
        fos.close();
        System.out.println("THE RM2=" + CM2.bitLength());
    } catch (final IOException e) {
        e.printStackTrace();
    }
}

推薦答案

對于加密,需要一個通常稱為 Integer to Octet String Primitive 或 I2OSP 的函數.對于解密,您需要一個 OS2IP 函數來轉換回整數.我在密碼學姊妹網站上的回答中解釋了兩者.它們是 RSA PKCS#1 規范的一部分,在此處指定了 2.2 版.

For the encrypt a function is required that is normally called Integer to Octet String Primitive or I2OSP. For decryption you need an OS2IP function to convert back to integers. Both are explained in my answer on the cryptography sister site. They are part of the RSA PKCS#1 specifications, for which version 2.2 is specified here.

I2OSP 和 OS2IP 函數也用于其他加密原語.例如,它們可用于橢圓曲線密碼學以創建平面 ECDSA 簽名或 EC 公鑰表示.

The I2OSP and OS2IP functions are also used for other cryptographic primitives. For instance, they can be used for Elliptic Curve Cryptography to create flat ECDSA signatures or EC public key representations.

這些函數用于對給定大小的八位字節字符串(字節數組)進行編碼/解碼.此大小通常與 RSA 加密的模數(在您的情況下為 P)的大小直接相關.

These functions are used to encode/decode to an octet string (byte array) of a given size. This size is normally directly related to the size of the modulus (P in your case) of RSA encryption.

I2OSP 函數應該這樣編碼:

The I2OSP function should be coded like this:

public static byte[] i2osp(final BigInteger i, final int size) {
    if (size < 1) {
        throw new IllegalArgumentException("Size of the octet string should be at least 1 but is " + size);
    }

    if (i == null || i.signum() == -1 || i.bitLength() > size * Byte.SIZE) {
        throw new IllegalArgumentException("Integer should be a positive number or 0, no larger than the given size");
    }

    final byte[] signed = i.toByteArray();
    if (signed.length == size) {
        // (we are lucky, already the right size)
        return signed;
    }

    final byte[] os = new byte[size];
    if (signed.length < size) {
        // (the dynamically sized array is too small, pad with 00 valued bytes at the left)
        System.arraycopy(signed, 0, os, size - signed.length, signed.length);
        return os;
    }

    // (signed representation too large, remove leading 00 valued byte)
    System.arraycopy(signed, 1, os, 0, size);
    return os;
}

當然,要使用正確的大小八位字節/字節,您首先應該知道密鑰大小(以字節為單位).對于 RSA 公鑰或私鑰,這可以很容易地從模數中計算出來(如果它不直接可用,如在 Java JCA 中):

Of course, to use this with the correct size in octets / bytes you should first know the key size in bytes first. For an RSA public- or private key this can be easily calculated from the modulus (in case it is not directly available, as in the Java JCA):

public static int keySizeInOctets(RSAKey key) {
    int keySizeBits = key.getModulus().bitLength();
    int keySizeBytes = (keySizeBits + Byte.SIZE - 1) / Byte.SIZE;
    return keySizeBytes;
}

請注意,RSAPublicKeyRSAPrivateKeyRSAPrivateCrtKey 都擴展了 RSAKey,后者提供了對模數的訪問.因此,您可以直接使用這些類的實例作為此方法的參數.當然,Java 中的 RSA 提供程序在 CipherSignature 實現類中已經包含了 I2OSP 和 OS2IP,但是從位大小到字節大小的轉換(沒有浮點計算)可以派上用場.

Note that RSAPublicKey, RSAPrivateKey and RSAPrivateCrtKey all extend RSAKey which provides access to the modulus. So you can use instances of these classes directly as argument for this method. Of course, the RSA providers in Java already contain I2OSP and OS2IP within the Cipher and Signature implementation classes, but the conversion from bit size to byte size (without floating point calculations) could come in handy.

還好反轉功能沒那么復雜:

Fortunately, the reverse function is not as complicated:

public static BigInteger os2ip(final byte[] data, final int size) {
    if (data.length != size) {
        throw new IllegalArgumentException("Size of the octet string should be precisely " + size);
    }

    return new BigInteger(1, data); 
}

我保留了大小驗證,因此可以使用 預期八位字節大小調用它,即使計算本身不需要它.

I've kept in the size validation so it can be called with the expected octet size, even though it is not required for the calculation itself.

這篇關于java - 如何在java密碼學中將位插入塊中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 天天操狠狠操 | 日韩中文字幕视频在线观看 | 少妇午夜一级艳片欧美精品 | 亚洲免费一区二区 | 精品国产91久久久久久 | 在线免费观看成人 | 国产精品视频在线播放 | 日韩在线播放一区 | 狠狠狠色丁香婷婷综合久久五月 | 成年人免费看 | 日韩精品视频中文字幕 | 一区二区三区视频 | 国产午夜精品一区二区三区嫩草 | 国产黄色av网站 | 日韩在线中文 | 亚洲成人中文字幕 | 欧美在线a | 麻豆视频在线免费看 | 欧美日韩成人影院 | 国产在线一区二区三区 | 久久亚洲国产精品 | 手机在线观看 | 欧美一区二区三区在线播放 | 一区中文字幕 | 欧美二区在线 | 九九热最新地址 | 精品一区二区免费视频 | 在线看av网址 | 天天看天天摸天天操 | 久久在看 | 黄色在线网站 | 五月天婷婷综合 | 狠狠操狠狠操 | 亚洲一区二区精品 | 欧美精品一区二区三区四区 在线 | 亚洲国产一区二区视频 | 国产精品久久久久久二区 | 国产精品一区二区三区在线 | 成人国产在线视频 | 在线小视频 | 性一交一乱一透一a级 |