How to getting the RSA Key from byte[]
Hello! in my project,the hardware security chips generate pair of RSA keys and i got them in byte[]
for example
he give me
/*PublicKey*/
pkd[ ]={
0x35,0xDA,0x57,0x4F,0x7B,0x6E,0x77,0x61,0x09,0xEE,0x9A,0xEE,0x65,0x42,0xC7,0x92,
0x5B,0x84,0x5D,0x53,0xE6,0xEB,0x90,0x32,0xBA,0x78,0x30,0x1B,0x8C,0x5C,0x65,0x9F,
0x9B,0x18,0xED,0x8D,0x28,0x48,0xDC,0x5A,0x97,0x2C,0xAC,0x29,0x15,0x8D,0xB7,0xE7,
0xD3,0xF4,0x52,0x63,0xC1,0xE1,0x8A,0xA0,0x6C,0x07,0x9E,0x53,0x25,0x3F,0xEC,0x38,
0x16,0x3A,0xFC,0xD2,0xC5,0x1E,0xFA,0x3C,0x6D,0xEE,0x10,0xA9,0x1B,0x6E,0x8F,0x4F,
0xFE,0xD1,0x7F,0x0E,0x97,0x04,0xA2,0x36,0x16,0x24,0x0C,0x01,0x82,0xF6,0x81,0x7F,
0x9A,0x14,0x92,0x8A,0xF6,0x0C,0x83,0xB1,0x12,0x23,0xCF,0x67,0x5B,0x66,0x75,0x4D,
0x99,0xE0,0x38,0xEC,0xF0,0x82,0xE3,0xCD,0x99,0x2C,0xF7,0x61,0x58,0xB7,0x32,0x76,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
}
/*modulus*/
pkn[ ]={
0x55,0x31,0xCD,0xC3,0x00,0xF2,0x7E,0xF7,0x7D,0x68,0xCC,0x9F,0x63,0x1C,0x9B,0xA6,
0x4C,0xEA,0xD5,0xBD,0x63,0x02,0x66,0xD6,0x5C,0x9B,0x85,0x6F,0x18,0xEC,0x43,0xD8,
0x23,0x20,0xFD,0x43,0x3E,0xDA,0xCB,0x4F,0x9D,0xA9,0x02,0x97,0xE5,0xA4,0x13,0x55,
0x93,0xFC,0x3F,0x2D,0xD3,0x9D,0xF5,0x5F,0xF2,0xC8,0xC2,0xFE,0x3D,0x9E,0xDF,0xB3,
0x66,0xF4,0xDD,0x1A,0xE7,0x53,0xF5,0x2A,0xAA,0xED,0x26,0x8A,0xA9,0xA2,0x63,0x6F,
0xE7,0xD1,0x00,0x11,0x73,0x48,0x0D,0x07,0xA3,0x69,0x32,0xB2,0x8C,0x17,0x66,0x11,
0x84,0xF6,0xFF,0x1E,0x1D,0xD1,0xDE,0x0C,0x11,0x8B,0x30,0x34,0xC5,0xCC,0xA4,0x19,
0xEE,0x86,0x78,0x2F,0x63,0xB3,0x4F,0xE1,0xD5,0x6F,0x4E,0x02,0x7D,0xCF,0x29,0x9D,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
}
/*input*/
input[ ]={
0x6e,0xd2,0x3b,0xdf,0xdc,0x5e,0x14,0x63,0x9f,0x9f,0xcc,0x67,0x71,0x83,0x4f,0x0d,
0xba,0xd6,0x54,0x1d,0x95,0x18,0xd5,0x09,0x22,0x66,0x0e,0x34,0x7c,0x9c,0x09,0xbd,
0x33,0x00,0xdc,0x38,0x57,0x1e,0x44,0xbd,0xf2,0x2b,0x66,0xb4,0x04,0x4c,0xbb,0x9c,
0x8a,0xff,0xc5,0x00,0x19,0xbd,0x3f,0xfd,0x3d,0x6a,0x14,0xd7,0x38,0x5a,0x05,0xe0,
0xb7,0xb4,0xc1,0xda,0x53,0x09,0xa0,0xe5,0x25,0x6e,0x1e,0x31,0xe2,0x39,0x0a,0xa5,
0xdc,0x98,0x2c,0x6a,0xd8,0x52,0xbf,0x22,0x9c,0x09,0x38,0x62,0x57,0x58,0x18,0xe6,
0x63,0xc1,0xb1,0x50,0x71,0x4b,0x0c,0x84,0x7f,0x17,0xad,0xc4,0x12,0xa4,0x85,0xe1,
0x70,0x5f,0x02,0x47,0xd2,0xcb,0x0c,0x9a,0x49,0xc3,0xf8,0x67,0x11,0xd2,0xc1,0x09,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
}
i must encrypt the input[ ] bytes use RSA
i read this topic
http://forum.java.sun.com/thread.jspa?threadID=5156375&tstart=120
it gives the method:
byte _key[];
RSAPublicKey rsaKey;
KeyFactory kfac = KeyFactory.getInstance("RSA");
BigInteger modulus =new BigInteger(_key);
RSAPublicKeySpec kspec1 =new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
rsaKey = (RSAPublicKey)kfac.generatePublic(kspec1);
but i dont found /*PublicKey*/ byte[] where to input
please help me ! thx very much.
# 2
> Three problems -
> 1) You don't specify the public exponent.
> 2) The modulus has 16 trailing zeros.
> 3) The magnitude of the input is greater than the
> modulus.
>
> Message was edited by:
> sabre150
yup,i asked the hardware engineer,he said there is not 16 trailing zeros in modulus and input byte,and the exponent he used is 65537.
i deleted 16 trailing zeros from pkd[ ] and pkn[ ] ,but i also reveive this error
javax.crypto.BadPaddingException: Message is larger than modulus
code
byte[]input = new byte[] {
0x6e,(byte)0xd2,0x3b,(byte)0xdf,(byte)0xdc,0x5e,0x14,0x63,(byte)0x9f,(byte)0x9f,(byte)0xcc,0x67,0x71,(byte)0x83,0x4f,0x0d,
(byte)0xba,(byte)0xd6,0x54,0x1d,(byte)0x95,0x18,(byte)0xd5,0x09,0x22,0x66,0x0e,0x34,0x7c,(byte)0x9c,0x09,(byte)0xbd,
0x33,0x00,(byte)0xdc,0x38,0x57,0x1e,0x44,(byte)0xbd,(byte)0xf2,0x2b,0x66,(byte)0xb4,0x04,0x4c,(byte)0xbb,(byte)0x9c,
(byte)0x8a,(byte)0xff,(byte)0xc5,0x00,0x19,(byte)0xbd,0x3f,(byte)0xfd,0x3d,0x6a,0x14,(byte)0xd7,0x38,0x5a,0x05,(byte)0xe0,
(byte)0xb7,(byte)0xb4,(byte)0xc1,(byte)0xda,0x53,0x09,(byte)0xa0,(byte)0xe5,0x25,0x6e,0x1e,0x31,(byte)0xe2,0x39,0x0a,(byte)0xa5,
(byte)0xdc,(byte)0x98,0x2c,0x6a,(byte)0xd8,0x52,(byte)0xbf,0x22,(byte)0x9c,0x09,0x38,0x62,0x57,0x58,0x18,(byte)0xe6,
0x63,(byte)0xc1,(byte)0xb1,0x50,0x71,0x4b,0x0c,(byte)0x84,0x7f,0x17,(byte)0xad,(byte)0xc4,0x12,(byte)0xa4,(byte)0x85,(byte)0xe1,
0x70,0x5f,0x02,0x47,(byte)0xd2,(byte)0xcb,0x0c,(byte)0x9a,0x49,(byte)0xc3,(byte)0xf8,0x67,0x11,(byte)0xd2,(byte)0xc1,0x09
};
byte[] pkm = new byte[]{(byte)0xB0,(byte)0x87,(byte)0xC3,0x4F,(byte)0xAF,(byte)0x82,(byte)0xAE,0x46,
(byte)0xFD,(byte)0x82,0x2E,(byte)0x87,0x66,0x65,0x2B,0x79,
0x54,0x7F,0x78,0x0D,0x02,0x23,(byte)0xDD,(byte)0xA0,0x0E,(byte)0x99,(byte)0x84,0x1D,0x3C,(byte)0x87,(byte)0x99,0x11,
(byte)0xA6,(byte)0xBC,(byte)0xC8,(byte)0xDC,0x1A,0x7A,(byte)0x93,(byte)0xDC,(byte)0x91,0x15,(byte)0x87,(byte)0x8C,0x63,0x01,0x3A,(byte)0xDA,
(byte)0xA4,0x4E,(byte)0x97,0x0C,(byte)0x85,0x34,(byte)0xE5,0x77,(byte)0xE6,0x55,(byte)0xE4,0x7C,0x24,0x2A,(byte)0xDD,(byte)0xFD,
0x3F,(byte)0xBD,0x19,(byte)0xE8,(byte)0xD0,0x2F,(byte)0xCF,(byte)0xD5,(byte)0x95,0x21,(byte)0x8B,(byte)0xF6,0x30,0x2D,(byte)0xC5,0x4B,
0x2F,0x7C,0x03,(byte)0xDC,0x7C,0x67,(byte)0xEC,0x2E,(byte)0xC7,0x51,(byte)0xEC,(byte)0xCF,(byte)0xA5,0x03,0x5E,0x33,
0x54,(byte)0xEE,0x4D,0x0D,0x1E,0x56,0x48,0x25,0x0F,(byte)0xDC,0x2A,(byte)0xC8,0x36,0x53,(byte)0xD7,(byte)0xAD,
0x5A,(byte)0xE1,0x29,0x14,0x70,(byte)0xBF,0x14,(byte)0xF3,0x1E,(byte)0xF1,(byte)0xEC,0x1B,(byte)0xAE,(byte)0xF4,(byte)0x91,(byte)0xB6
};
Ciphercipher = Cipher.getInstance("RSA/ECB/NoPadding");
// create the keys
KeyFactorykeyFactory = KeyFactory.getInstance("RSA");
BigInteger modulus = new BigInteger(pkm).abs();
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, RSAKeyGenParameterSpec.F4);
RSAPublicKey pubKey = (RSAPublicKey)keyFactory.generatePublic(pubKeySpec);
//System.out.println("input : " + ByteTransHexStr.byteArr2HexStr(input));
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherText = cipher.doFinal(input);
System.out.println("cipher length: " +cipherText.length);
System.out.println("cipher: " + ByteTransHexStr.byteArr2HexStr(cipherText));
when i set the padding to PKCS1Padding
i got javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
how was it?
may be i dont need publicKey bytes ?
thx!
Message was edited by:
Caixiaopig
Message was edited by:
Caixiaopig
# 3
and i change pkm[] to add 16 zeros
byte[] pkm = new byte[]{(byte)0xB0,(byte)0x87,(byte)0xC3,0x4F,(byte)0xAF,(byte)0x82,(byte)0xAE,0x46,
(byte)0xFD,(byte)0x82,0x2E,(byte)0x87,0x66,0x65,0x2B,0x79,
0x54,0x7F,0x78,0x0D,0x02,0x23,(byte)0xDD,(byte)0xA0,0x0E,(byte)0x99,(byte)0x84,0x1D,0x3C,(byte)0x87,(byte)0x99,0x11,
(byte)0xA6,(byte)0xBC,(byte)0xC8,(byte)0xDC,0x1A,0x7A,(byte)0x93,(byte)0xDC,(byte)0x91,0x15,(byte)0x87,(byte)0x8C,0x63,0x01,0x3A,(byte)0xDA,
(byte)0xA4,0x4E,(byte)0x97,0x0C,(byte)0x85,0x34,(byte)0xE5,0x77,(byte)0xE6,0x55,(byte)0xE4,0x7C,0x24,0x2A,(byte)0xDD,(byte)0xFD,
0x3F,(byte)0xBD,0x19,(byte)0xE8,(byte)0xD0,0x2F,(byte)0xCF,(byte)0xD5,(byte)0x95,0x21,(byte)0x8B,(byte)0xF6,0x30,0x2D,(byte)0xC5,0x4B,
0x2F,0x7C,0x03,(byte)0xDC,0x7C,0x67,(byte)0xEC,0x2E,(byte)0xC7,0x51,(byte)0xEC,(byte)0xCF,(byte)0xA5,0x03,0x5E,0x33,
0x54,(byte)0xEE,0x4D,0x0D,0x1E,0x56,0x48,0x25,0x0F,(byte)0xDC,0x2A,(byte)0xC8,0x36,0x53,(byte)0xD7,(byte)0xAD,
0x5A,(byte)0xE1,0x29,0x14,0x70,(byte)0xBF,0x14,(byte)0xF3,0x1E,(byte)0xF1,(byte)0xEC,0x1B,(byte)0xAE,(byte)0xF4,(byte)0x91,(byte)0xB6,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
it is successful and result is
cipher length: 144
cipher: 49af78f9095235841458f904c4e8d9d511cc560c87dfbb9a32b3068541f7a6bb9ef9d5ce97fea3364269cfaf0672281fa6912f4ee207d671981bc52e45f32c42e02eaf9d3bd7a2ff436b782a5bbbbf4da25ec1a1134a085000de39dd2791b3b154fde43ebe0f196fe0f198be60c6e41c984e02c84acae2f36100717f24887aefe58bf705279927e6f092f49a9bfac109