pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

java - 如何在java密碼學(xué)中將位插入塊中?

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

問(wèn)題描述

我正在嘗試制作一個(gè)涉及密碼學(xué)的簡(jiǎn)單 Java 程序.

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

首先,我從文件 clearmsg.txt 中讀取一個(gè) 32 字節(jié)的塊.然后我將此塊轉(zhuǎn)換為整數(shù),并將其用于加密.不幸的是,密文的大小不是靜態(tài)的;有時(shí)它返回 30 個(gè)字節(jié),有時(shí)返回 26 個(gè)字節(jié).這似乎與添加操作的結(jié)果無(wú)關(guān).

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 字節(jié)的密碼塊?如何向這個(gè)塊添加位/字節(jié)?因?yàn)楫?dāng)我嘗試解密這個(gè)塊時(shí),我需要讀取 32 個(gè)密文字節(jié).

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();
    }
}

推薦答案

對(duì)于加密,需要一個(gè)通常稱為 Integer to Octet String Primitive 或 I2OSP 的函數(shù).對(duì)于解密,您需要一個(gè) OS2IP 函數(shù)來(lái)轉(zhuǎn)換回整數(shù).我在密碼學(xué)姊妹網(wǎng)站上的回答中解釋了兩者.它們是 RSA PKCS#1 規(guī)范的一部分,在此處指定了 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 函數(shù)也用于其他加密原語(yǔ).例如,它們可用于橢圓曲線密碼學(xué)以創(chuàng)建平面 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.

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

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 函數(shù)應(yīng)該這樣編碼:

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;
}

當(dāng)然,要使用正確的大小八位字節(jié)/字節(jié),您首先應(yīng)該知道密鑰大小(以字節(jié)為單位).對(duì)于 RSA 公鑰或私鑰,這可以很容易地從模數(shù)中計(jì)算出來(lái)(如果它不直接可用,如在 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;
}

請(qǐng)注意,RSAPublicKeyRSAPrivateKeyRSAPrivateCrtKey 都擴(kuò)展了 RSAKey,后者提供了對(duì)模數(shù)的訪問(wèn).因此,您可以直接使用這些類的實(shí)例作為此方法的參數(shù).當(dāng)然,Java 中的 RSA 提供程序在 CipherSignature 實(shí)現(xiàn)類中已經(jīng)包含了 I2OSP 和 OS2IP,但是從位大小到字節(jié)大小的轉(zhuǎn)換(沒(méi)有浮點(diǎn)計(jì)算)可以派上用場(chǎng).

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.

還好反轉(zhuǎn)功能沒(méi)那么復(fù)雜:

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); 
}

我保留了大小驗(yàn)證,因此可以使用 預(yù)期八位字節(jié)大小調(diào)用它,即使計(jì)算本身不需要它.

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.

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

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來(lái)自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯(cuò)誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測(cè)不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 氧化铁红厂家-淄博宗昂化工| 氧氮氢联合测定仪-联测仪-氧氮氢元素分析仪-江苏品彦光电 | 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 | 选矿设备,选矿生产线,选矿工艺,选矿技术-昆明昆重矿山机械 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | 密集柜_档案密集柜_智能密集架_密集柜厂家_密集架价格-智英伟业 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 选矿设备,选矿生产线,选矿工艺,选矿技术-昆明昆重矿山机械 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | MTK核心板|MTK开发板|MTK模块|4G核心板|4G模块|5G核心板|5G模块|安卓核心板|安卓模块|高通核心板-深圳市新移科技有限公司 | 北京西风东韵品牌与包装设计公司,创造视觉销售力! | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 江苏全风,高压风机,全风环保风机,全风环形高压风机,防爆高压风机厂家-江苏全风环保科技有限公司(官网) | 杰恒蠕动泵-蠕动泵专业厂家-19年专注蠕动泵 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 锂辉石检测仪器,水泥成分快速分析仪-湘潭宇科分析仪器有限公司 | 亿诺千企网-企业核心产品贸易| 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 开云(中国)Kaiyun·官方网站 - 登录入口 | 阳光模拟试验箱_高低温试验箱_高低温冲击试验箱_快速温变试验箱|东莞市赛思检测设备有限公司 | 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 电销卡_北京电销卡_包月电话卡-豪付网络 | 金联宇电缆|广东金联宇电缆厂家_广东金联宇电缆实业有限公司 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 复盛空压机配件-空气压缩机-复盛空压机(华北)总代理 | 潜水搅拌机-双曲面搅拌机-潜水推进器|奥伯尔环保 | 重庆中专|职高|技校招生-重庆中专招生网 | 优秀的临床医学知识库,临床知识库,医疗知识库,满足电子病历四级要求,免费试用 | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | 万濠影像仪(万濠投影仪)百科-苏州林泽仪器 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 瓶盖扭矩测试仪-瓶盖扭力仪-全自动扭矩仪-济南三泉中石单品站 | 喷播机厂家_二手喷播机租赁_水泥浆洒布机-河南青山绿水机电设备有限公司 | 电杆荷载挠度测试仪-电杆荷载位移-管桩测试仪-北京绿野创能机电设备有限公司 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | TPE塑胶原料-PPA|杜邦pom工程塑料、PPSU|PCTG材料、PC/PBT价格-悦诚塑胶 | 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 |