SecurityTokenUtils { public static byte[] createRSAPrivKeyTemplate(RSAPrivateCrtKey secretKey, KeyType slot, RSAKeyFormat format) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(), template = new ByteArrayOutputStream(), data = new ByteArrayOutputStream(), res = new ByteArrayOutputStream(); int expLengthBytes = (format.getExponentLength() + 7) / 8; template.write(new byte[]{(byte) 0x91, (byte) expLengthBytes}); writeBits(data, secretKey.getPublicExponent(), expLengthBytes); final int modLengthBytes = format.getModulusLength() / 8; template.write(Hex.decode("928180")); writeBits(data, secretKey.getPrimeP(), modLengthBytes / 2); template.write(Hex.decode("938180")); writeBits(data, secretKey.getPrimeQ(), modLengthBytes / 2); if (format.getAlgorithmFormat().isIncludeCrt()) { template.write(Hex.decode("948180")); writeBits(data, secretKey.getCrtCoefficient(), modLengthBytes / 2); template.write(Hex.decode("958180")); writeBits(data, secretKey.getPrimeExponentP(), modLengthBytes / 2); template.write(Hex.decode("968180")); writeBits(data, secretKey.getPrimeExponentQ(), modLengthBytes / 2); } if (format.getAlgorithmFormat().isIncludeModulus()) { template.write(Hex.decode("97820100")); writeBits(data, secretKey.getModulus(), modLengthBytes); } stream.write(slot.getSlot()); stream.write(0); stream.write(Hex.decode("7F48")); stream.write(encodeLength(template.size())); stream.write(template.toByteArray()); stream.write(Hex.decode("5F48")); stream.write(encodeLength(data.size())); stream.write(data.toByteArray()); res.write(Hex.decode("4D")); res.write(encodeLength(stream.size())); res.write(stream.toByteArray()); return res.toByteArray(); }  static byte[] attributesFromSecretKey(KeyType slot, CanonicalizedSecretKey secretKey,
            KeyFormat formatForKeyType); static byte[] createRSAPrivKeyTemplate(RSAPrivateCrtKey secretKey, KeyType slot,
            RSAKeyFormat format); static byte[] createECPrivKeyTemplate(ECPrivateKey secretKey, ECPublicKey publicKey, KeyType slot,
            ECKeyFormat format);  }
@Test public void testPrivateKeyTemplateSimple2048() throws Exception { RSAPrivateCrtKey key2048 = mock(RSAPrivateCrtKey.class); byte[] tmp = new byte[128]; Arrays.fill(tmp, (byte) 0x11); when(key2048.getPrimeP()).thenReturn(new BigInteger(tmp)); Arrays.fill(tmp, (byte) 0x12); when(key2048.getPrimeQ()).thenReturn(new BigInteger(tmp)); BigInteger exp = new BigInteger("65537"); when(key2048.getPublicExponent()).thenReturn(exp); Assert.assertArrayEquals( Hex.decode("4d820115" + "a400" + "7f4808" + "9103" + "928180" + "938180" + "5f48820103" + "010001" + "1111111111111111111111111111111111111111111111111111111111111111" + "1111111111111111111111111111111111111111111111111111111111111111" + "1111111111111111111111111111111111111111111111111111111111111111" + "1111111111111111111111111111111111111111111111111111111111111111" + "1212121212121212121212121212121212121212121212121212121212121212" + "1212121212121212121212121212121212121212121212121212121212121212" + "1212121212121212121212121212121212121212121212121212121212121212" + "1212121212121212121212121212121212121212121212121212121212121212" ), SecurityTokenUtils.createRSAPrivKeyTemplate(key2048, KeyType.AUTH, new RSAKeyFormat(2048, exp.bitLength(), RSAKeyFormat.RSAAlgorithmFormat.STANDARD))); }