Current location - Quotes Website - Personality signature - Hello, I saw your previous article about JAVA's RSA algorithm on the Internet. I would like to ask how a BYTE array becomes an RSAPublicKey. Thank you.
Hello, I saw your previous article about JAVA's RSA algorithm on the Internet. I would like to ask how a BYTE array becomes an RSAPublicKey. Thank you.

//Convert the byte array into RSAPublicKey

public RSAPublicKey bytes2PK(byte[] buf) {

buf=Base64.decode(buf);

p>

byte size=buf[0];

byte size2=buf[1];

byte[] b1 = new byte[size];

System.arraycopy(buf,2,b1,0,b1.length);

byte[] b2 = new byte[size2];

System.arraycopy(buf ,b1.length+2,b2,0,b2.length);

BigInteger B1 = new BigInteger(b1);

BigInteger B2 = new BigInteger(b2);

p>

RSAPublicKeySpec spec = new RSAPublicKeySpec(B1, B2);//These two large integers are stored

KeyFactory keyFactory;

PublicKey pk = null;< /p>

try {

keyFactory = KeyFactory.getInstance("RSA");

pk = keyFactory.generatePublic(spec);

} catch (Exception e) {

e.printStackTrace();

}

return (RSAPublicKey)pk;

}

}

p>

The data contained in the public key is actually modulus and publicExponent. All can be expressed as byte arrays. For the convenience of network transmission, I spliced ??two byte arrays together. It's actually easier to understand storing them separately.