小编use*_*407的帖子

RSA解密错误 - IllegalBlockSizeException:数据不得超过128个字节

我现在正在制作RSA消息认证软件.过程如下:

  1. 使用A的私钥(1024位)对邮件进行签名
  2. 使用A的公钥验证邮件(1024位)

#1代码(如下)工作正常并生成以下结果:

5554c9a9f6838b6cf40d9dbfbab3d90ea27aa6434ed095d289c13c2624617993ad99161ac265276d150510c176341d8ab8600d08b7353286d465e6bd3370a6fd8dd3ffb82916f612fd6dcee5e654ed801cfca6b6d2d5d6dc99ff7921b615abdf62eb67db1f71e6a6ea70012fd35e7cefa1a8d3aab7614c47746cfe1fc2bc875b

但是#2代码显示以下错误:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes

我认为#1中的行Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 产生2048位(256字节)的结果.也许这就是问题......记住我使用1024位私钥.

那么#1代码如何生成128字节的结果呢?

1. SignMail.java

public class SignMail {

    static {
        Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider());
    }

    public static String sign(String userOriginalMessage) throws Exception {

        PEMReader userPrivateKey = new PEMReader(
          new InputStreamReader(
             new FileInputStream(Environment.getExternalStorageDirectory()+"/pkcs10priv.key")));

        KeyPair keyPair = (KeyPair)userPrivateKey.readObject();

        byte[] cipherText;
        //modified by JEON 20130817
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        //encrypt the message using private key
        cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate());
        cipherText = cipher.doFinal(userOriginalMessage.getBytes());
        return new String(Hex.encode(cipherText));

    }


} …
Run Code Online (Sandbox Code Playgroud)

java rsa key jce

8
推荐指数
1
解决办法
2万
查看次数

将Base64 byte []转换为java中的可读字符串

我想将Base62 Byte数组转换为人类可读的Stiring

在这段代码中,

我需要将"[B @ 913fe2"(结果)转换为"Hello Wold!".

我查看了几个以前的问题,但我不知道如何.

package chapter9;

import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.*;
import java.util.Arrays;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;

/**
 * Example of generating a detached signature.
 */
public class SignedDataExample
    extends SignedDataProcessor
{

    public static void main(String[] args)
        throws Exception
    {
        KeyStore        credentials = Utils.createCredentials();
        PrivateKey      key = (PrivateKey)credentials.getKey(Utils.END_ENTITY_ALIAS, Utils.KEY_PASSWD);
    Certificate[]   chain = credentials.getCertificateChain(Utils.END_ENTITY_ALIAS);
    CertStore       certsAndCRLs = CertStore.getInstance("Collection",
                        new CollectionCertStoreParameters(Arrays.asList(chain)), "BC");
    X509Certificate cert = (X509Certificate)chain[0];

    // set up the generator …
Run Code Online (Sandbox Code Playgroud)

java base64

4
推荐指数
1
解决办法
2万
查看次数

在文件中写入 PEM 编码的证书 - java

再会。

我最近使用充气城堡 API 创建了 X.509 证书。

我需要保存证书结果而不是显示结果。

我尝试使用 FileOutputStream,但它不起作用。

问候

结果如下

-----开始证书----- MIICeTCCAeKgAwIBAgIGATs8OWsXMA0GCSqGSIb3DQEBCwUAMBsxGTAXBgNVBAMT...

-----结束证书-----

代码如下

import java.io.FileOutputStream;
//example of a basic CA


public class PKCS10CertCreateExample
{
    public static X509Certificate[] buildChain() throws Exception
    {
        //create the certification request
        KeyPair pair = chapter7.Utils.generateRSAKeyPair();
        PKCS10CertificationRequest request =
PKCS10ExtensionExample.generateRequest(pair);

        //create a root certificate
        KeyPair rootPair=chapter7.Utils.generateRSAKeyPair();
        X509Certificate rootCert = X509V1CreateExample.generateV1Certificate
(rootPair);

        //validate the certification request
        if(!request.verify("BC"))
        {
            System.out.println("request failed to verify!");
            System.exit(1);
        }

        //create the certificate using the information in the request
        X509V3CertificateGenerator certGen = …
Run Code Online (Sandbox Code Playgroud)

java certificate x509

1
推荐指数
1
解决办法
5401
查看次数

通过充气城堡库将der转换为pem

我发现了许多将pem转换为der的答案。

但是,我找不到将der转换为pem的方法

例如,以下代码生成der编码文件pkcs10.cer

public static void main(String[] args) throws Exception
{
    X509Certificate[] chain = buildChain();
    PEMWriter pemWrt = new PEMWriter(new OutputStreamWriter(System.out));
    pemWrt.writeObject(chain[0]);

    FileWriter fwO = new FileWriter("pkcs10.cer");
    fwO.write((chain[0]).toString());

    fwO.close();
    pemWrt.close();

}
Run Code Online (Sandbox Code Playgroud)

像[0]版本:3序列号:1353995641265 IssuerDN:CN = Test证书开始日期:2012年11月26日星期一21:54:01最终日期:2012年11月26日星期一21:54:51

但是,我不知道如何从der文件中进行pem编码的认证。

java cryptography bouncycastle x509

0
推荐指数
1
解决办法
6050
查看次数

标签 统计

java ×4

x509 ×2

base64 ×1

bouncycastle ×1

certificate ×1

cryptography ×1

jce ×1

key ×1

rsa ×1