The exclusive promotional activities on JCOP J3H145 card and Feitian R301 Smartcard Reader are in full swing. Please check this page for details.

## RSA Public Key Encryption

### RSA Public Key Encryption

Hello

i want to encrypt some bytes for example (11 11 11 11 11) by using RSA Public Key with 2048 bit length , but the result changes ever

byte[] list = new byte[256];

private static Cipher asymCipher;

private static RSAPrivateKey rsaPriKey;

private static RSAPublicKey rsaPubKey;

private static KeyPair keyPair;

public testSignApplet()

{

rsaPriKey = (RSAPrivateKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE,KeyBuilder.LENGTH_RSA_2048,false);

rsaPubKey = (RSAPublicKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC,KeyBuilder.LENGTH_RSA_2048,false);

keyPair = new KeyPair(rsaPubKey,rsaPriKey);

asymCipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1,false);

}

switch (buf[ISO7816.OFFSET_INS])

{

case (byte)0x0C:

keyPair.genKeyPair();;

case (byte)0x3C:

asymCipher.init(rsaPubKey,Cipher.MODE_ENCRYPT);

break;

case (byte)0x4C:

apdu.setIncomingAndReceive();

asymCipher.doFinal(buf,(short)ISO7816.OFFSET_CDATA,lc,list,(short)0);

apdu.setOutgoing();

apdu.setOutgoingLength((short)256);

apdu.sendBytesLong(list,(short)0,(short)list.length);

break;

}

i want to encrypt some bytes for example (11 11 11 11 11) by using RSA Public Key with 2048 bit length , but the result changes ever

byte[] list = new byte[256];

private static Cipher asymCipher;

private static RSAPrivateKey rsaPriKey;

private static RSAPublicKey rsaPubKey;

private static KeyPair keyPair;

public testSignApplet()

{

rsaPriKey = (RSAPrivateKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE,KeyBuilder.LENGTH_RSA_2048,false);

rsaPubKey = (RSAPublicKey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC,KeyBuilder.LENGTH_RSA_2048,false);

keyPair = new KeyPair(rsaPubKey,rsaPriKey);

asymCipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1,false);

}

switch (buf[ISO7816.OFFSET_INS])

{

case (byte)0x0C:

keyPair.genKeyPair();;

case (byte)0x3C:

asymCipher.init(rsaPubKey,Cipher.MODE_ENCRYPT);

break;

case (byte)0x4C:

apdu.setIncomingAndReceive();

asymCipher.doFinal(buf,(short)ISO7816.OFFSET_CDATA,lc,list,(short)0);

apdu.setOutgoing();

apdu.setOutgoingLength((short)256);

apdu.sendBytesLong(list,(short)0,(short)list.length);

break;

}

- UNKNwYSHSA
**Posts:**630**Joined:**Thu May 21, 2015 4:05 am**Points**:3035-
**Contact:**

### Re: RSA Public Key Encryption

ALG_RSA_PKCS1

public static final byte ALG_RSA_PKCS1Cipher algorithm ALG_RSA_PKCS1 provides a cipher using RSA, and pads input data according to the PKCS#1 (v1.5) scheme.

Note:

This algorithm is only suitable for messages of limited length. The total number of input bytes processed during encryption may not be more than k-11, where k is the RSA key's modulus size in bytes.

The encryption block(EB) during encryption with a Public key is built as follows:

EB = 00 || 02 || PS || 00 || M

:: M (input bytes) is the plaintext message

:: PS is an octet string of length k-3-||M|| of pseudo random nonzero octets. The length of PS must be at least 8 octets.

:: k is the RSA modulus size.

The encryption block(EB) during encryption with a Private key (used to compute signatures when the message digest is computed off-card) is built as follows:

EB = 00 || 01 || PS || 00 || D

:: D (input bytes) is the DER encoding of the hash computed elsewhere with an algorithm ID prepended if appropriate

:: PS is an octet string of length k-3-||D|| with value FF. The length of PS must be at least 8 octets.

:: k is the RSA modulus size.

This is the description of ALG_RSA_PKCS1 in the specification JavaCard API.

The padding bytes contains random bytes. This leads different result in each encryption.

sense and simplicity

### Re: RSA Public Key Encryption

tanks so much.

what is your solution for this problem?

i dont no what should i do

what is your solution for this problem?

i dont no what should i do

- UNKNwYSHSA
**Posts:**630**Joined:**Thu May 21, 2015 4:05 am**Points**:3035-
**Contact:**

### Re: RSA Public Key Encryption

Decrypt the encrypted data with the private key. The output is your plain text.

Here is the whole process of PKCS1 encryption and decryption (Encrypt with public key):

1 Encryption:

case 0xXX:

// Init cipher with the private key and mode decrypt;

asymCipher.init(rsaPriKey,Cipher.MODE_DECRYPT);

// Input encrypted data and output the plain data;

apdu.setIncomingAndReceive();

short plainLen = asymCipher.doFinal(buf,(short)ISO7816.OFFSET_CDATA,lc,list,(short)0);

apdu.setOutgoing();

apdu.setOutgoingLength((short)plainLen);

apdu.sendBytesLong(list,(short)0,plainLen);

break;

Here is the whole process of PKCS1 encryption and decryption (Encrypt with public key):

1 Encryption:

- A Input plain text;

B Padding plain text: (00 || 02 || PS || 00 || Plain text); (PS is pseudo random nonzero octets);

C Encrypt padded data;

D Output encrypted data;

- A Input encrypted data;

B Decrypt encrypted data => Output padded plain data;

C Unpadding the (padded plain data) with PKCS1: (00 || 02 || PS || 00 || Plain text) => Plain text; (PS is pseudo random nonzero octets);

D OUtput plain text;

sense and simplicity

### Re: RSA Public Key Encryption

deer UNKNwYSHSA thanks.

- UNKNwYSHSA
**Posts:**630**Joined:**Thu May 21, 2015 4:05 am**Points**:3035-
**Contact:**

### Re: RSA Public Key Encryption

sense and simplicity

### Who is online

Users browsing this forum: No registered users and 10 guests