List of usage examples for java.security.spec RSAPrivateKeySpec RSAPrivateKeySpec
public RSAPrivateKeySpec(BigInteger modulus, BigInteger privateExponent)
From source file:org.mitre.jwt.JwtTest.java
/** * @throws Exception//from ww w .j a v a2s . com */ @Test public void testGenerateRsaSignature() throws Exception { // Hard code the private/public key so as not to depend on it being in // the keystore... RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(new BigInteger( "AD6E684550542947AD95EF9BACDC0AC2C9168C6EB3212D378C23E5539266111DB2E5B4D42B1E47EB4F7A65DB63D9782C72BC365492FD1E5C7B4CD2174C611668C29013FEDE22619B3F58DA3531BB6C02B3266768B7895CBDAFB3F9AC7A7B2F3DB17EF4DCF03BD2575604BDE0A01BB1FB7B0E733AD63E464DB4D7D89626297A214D7CECCD0C50421A322A01E9DCEA23443F6A9339576B31DFA504A133076394562CB57F3FDEDB26F9A82BED2F6D52D6F6BF8286E2497EF0B5C8456F32B4668F5A9F5FCD3781345DDDB749792C37238A53D18FD976C0C9D1F1E211F1A4A9AAE679C45B92D1741EF0D3C3F373232CE7FB93E9BC461E1C508A20B74E7E3361B3C527", 16), new BigInteger( "627CDD67E75B33EA0990A8F64DEED389942A62EB867C23B274B9F9C440D2078C47089D6D136369D21E5B52B688F8797F3C54D7C1A58B6A8F7851C2C90A4DE42CEFB864328B31191ED19582AD4CA5B38BC0F2E12C9D75BB1DD946AA55A1648D0A4ADEDEED0CDBDBF24EDDF87A345225FBBB0114BCE7E78B831B5CAC197068837AB0B3F07157952A05F67A72B9852972C704B6B32A70C3BB3DEB186936B0F7D6ABE012DEB89BC2DBE1F88AE7A28C06C53D1FB2459E58D8ED266E3BFC28266981D2A5F624D36555DD64F410461ADA5D53F448BA5EEBBD4BCEC3AF53285FB394650D7B3BFB06712E081AAD160EED6E83A3EA2D092712C07A6331209F62D27184BFC9", 16)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(privateSpec); RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(new BigInteger( "AD6E684550542947AD95EF9BACDC0AC2C9168C6EB3212D378C23E5539266111DB2E5B4D42B1E47EB4F7A65DB63D9782C72BC365492FD1E5C7B4CD2174C611668C29013FEDE22619B3F58DA3531BB6C02B3266768B7895CBDAFB3F9AC7A7B2F3DB17EF4DCF03BD2575604BDE0A01BB1FB7B0E733AD63E464DB4D7D89626297A214D7CECCD0C50421A322A01E9DCEA23443F6A9339576B31DFA504A133076394562CB57F3FDEDB26F9A82BED2F6D52D6F6BF8286E2497EF0B5C8456F32B4668F5A9F5FCD3781345DDDB749792C37238A53D18FD976C0C9D1F1E211F1A4A9AAE679C45B92D1741EF0D3C3F373232CE7FB93E9BC461E1C508A20B74E7E3361B3C527", 16), new BigInteger("10001", 16)); PublicKey publicKey = keyFactory.generatePublic(publicSpec); Jwt jwt = new Jwt(); jwt.getHeader().setType("JWT"); jwt.getHeader().setAlgorithm("RS256"); jwt.getClaims().setExpiration(new Date(1300819380L * 1000L)); jwt.getClaims().setIssuer("joe"); jwt.getClaims().setClaim("http://example.com/is_root", Boolean.TRUE); JwtSigner signer = new RsaSigner(JwsAlgorithm.RS256.getJwaName(), publicKey, privateKey); ((RsaSigner) signer).afterPropertiesSet(); /* * Expected string based on the following structures, serialized exactly * as follows and base64 encoded: * * header: {"typ":"JWT","alg":"HS256"} claims: * {"exp":1300819380,"iss":"joe","http://example.com/is_root":true} * * Expected signature: dSRvtD-ExzGN- * fRXd1wRZOPo1JFPuqgwvaIKp8jgcyMXJegy6IUjssfUfUcICN5yvh0ggOMWMeWkwQ7 * -PlXMJWymdhXVI3BOpNt7ZOB2vMFYSOOHNBJUunQoe1lmNxuHQdhxqoHahn3u1cLDXz * -xx- * JELduuMmaDWqnTFPodVPl45WBKHaQhlOiFWj3ZClUV2k5p2yBT8TmxekL8gWwgVbQk5yPnYOs * -PcMjzODc9MZX4yI10ZSCSDciwf- * rgkQLT7wW4uZCoqTZ7187sCodHd6nw3nghqbtqN05fQ3Yq7ykwaR8pdQBFb2L9l7DhLLuXIREDKIFUHBSUs8OnvXFMg */ String signature = "dSRvtD-ExzGN-fRXd1wRZOPo1JFPuqgwvaIKp8jgcyMXJegy6IUjssfUfUcICN5yvh0ggOMWMeWkwQ7-PlXMJWymdhXVI3BOpNt7ZOB2vMFYSOOHNBJUunQoe1lmNxuHQdhxqoHahn3u1cLDXz-xx-JELduuMmaDWqnTFPodVPl45WBKHaQhlOiFWj3ZClUV2k5p2yBT8TmxekL8gWwgVbQk5yPnYOs-PcMjzODc9MZX4yI10ZSCSDciwf-rgkQLT7wW4uZCoqTZ7187sCodHd6nw3nghqbtqN05fQ3Yq7ykwaR8pdQBFb2L9l7DhLLuXIREDKIFUHBSUs8OnvXFMg"; String expected = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjEzMDA4MTkzODAsImlzcyI6ImpvZSIsImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ" + "." + signature; signer.sign(jwt); String actual = jwt.toString(); assertThat(signer.verify(actual), equalTo(true)); assertThat(actual, equalTo(expected)); assertThat(jwt.getSignature(), equalTo(signature)); }
From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java
/** * RSA???//from w w w . java2 s.co m * <dl> * <dt>? * <dd>RSA????????????? * </dl> * @param modulus * @param exponent ?? * @return RSA? */ public static RSAPrivateKey createPrivateKey(final BigInteger modulus, final BigInteger exponent) { try { final KeyFactory keyFactory = KeyFactory.getInstance(ALGO_KEY); return (RSAPrivateKey) keyFactory.generatePrivate(new RSAPrivateKeySpec(modulus, exponent)); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new StandardRuntimeException(e); } }
From source file:compiler.downloader.MegaHandler.java
private int login_process(JSONObject json, long[] password_aes) throws IOException { String master_key_b64 = null; try {//from w w w . ja va2 s . c om master_key_b64 = json.getString("k"); } catch (JSONException e) { e.printStackTrace(); } if (master_key_b64 == null || master_key_b64.isEmpty()) { return -1; } long[] encrypted_master_key = MegaCrypt.base64_to_a32(master_key_b64); master_key = MegaCrypt.decrypt_key(encrypted_master_key, password_aes); if (json.has("csid")) { String encrypted_rsa_private_key_b64 = null; try { encrypted_rsa_private_key_b64 = json.getString("privk"); } catch (JSONException e) { e.printStackTrace(); } long[] encrypted_rsa_private_key = MegaCrypt.base64_to_a32(encrypted_rsa_private_key_b64); long[] rsa_private_key = MegaCrypt.decrypt_key(encrypted_rsa_private_key, master_key); String private_key = MegaCrypt.a32_to_str(rsa_private_key); BigInteger[] rsa_private_key1 = new BigInteger[4]; for (int i = 0; i < 4; i++) { int l = ((((int) private_key.charAt(0)) * 256 + ((int) private_key.charAt(1)) + 7) / 8) + 2; rsa_private_key1[i] = MegaCrypt.mpi_to_int(private_key.substring(0, l)); private_key = private_key.substring(l); } BigInteger encrypted_sid = null; try { encrypted_sid = MegaCrypt.mpi_to_int(MegaCrypt.base64_url_decode(json.getString("csid"))); } catch (JSONException e) { e.printStackTrace(); } BigInteger modulus = rsa_private_key1[0].multiply(rsa_private_key1[1]); BigInteger privateExponent = rsa_private_key1[2]; BigInteger sid = null; try { PrivateKey privateKey = KeyFactory.getInstance("RSA") .generatePrivate(new RSAPrivateKeySpec(modulus, privateExponent)); Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); // PyCrypt can handle >256 bit length... what the fuck... sometimes i get 257 if (encrypted_sid.toByteArray().length > 256) { Random rg = new Random(); sequence_number = rg.nextInt(Integer.MAX_VALUE); return -2; // lets get a new seession } sid = new BigInteger(cipher.doFinal(encrypted_sid.toByteArray())); } catch (Exception e) { e.printStackTrace(); return -1; } String sidS = sid.toString(16); if (sidS.length() % 2 != 0) { sidS = "0" + sidS; } try { byte[] sidsnohex = MegaCrypt.decodeHexString(sidS); this.sid = MegaCrypt.base64_url_encode(new String(sidsnohex, "ISO-8859-1").substring(0, 43)); } catch (Exception e) { e.printStackTrace(); return -1; } } return 0; }
From source file:MegaHandler.java
private int login_process(JSONObject json, long[] password_aes) throws IOException { String master_key_b64 = null; try {// ww w. ja va 2 s.co m master_key_b64 = json.getString("k"); } catch (JSONException e) { e.printStackTrace(); } if (master_key_b64 == null || master_key_b64.isEmpty()) return -1; long[] encrypted_master_key = MegaCrypt.base64_to_a32(master_key_b64); master_key = MegaCrypt.decrypt_key(encrypted_master_key, password_aes); if (json.has("csid")) { String encrypted_rsa_private_key_b64 = null; try { encrypted_rsa_private_key_b64 = json.getString("privk"); } catch (JSONException e) { e.printStackTrace(); } long[] encrypted_rsa_private_key = MegaCrypt.base64_to_a32(encrypted_rsa_private_key_b64); long[] rsa_private_key = MegaCrypt.decrypt_key(encrypted_rsa_private_key, master_key); String private_key = MegaCrypt.a32_to_str(rsa_private_key); this.rsa_private_key = new BigInteger[4]; for (int i = 0; i < 4; i++) { int l = ((((int) private_key.charAt(0)) * 256 + ((int) private_key.charAt(1)) + 7) / 8) + 2; this.rsa_private_key[i] = MegaCrypt.mpi_to_int(private_key.substring(0, l)); private_key = private_key.substring(l); } BigInteger encrypted_sid = null; try { encrypted_sid = MegaCrypt.mpi_to_int(MegaCrypt.base64_url_decode(json.getString("csid"))); } catch (JSONException e) { e.printStackTrace(); } BigInteger modulus = this.rsa_private_key[0].multiply(this.rsa_private_key[1]); BigInteger privateExponent = this.rsa_private_key[2]; BigInteger sid = null; try { PrivateKey privateKey = KeyFactory.getInstance("RSA") .generatePrivate(new RSAPrivateKeySpec(modulus, privateExponent)); Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); // PyCrypt can handle >256 bit length... what the fuck... sometimes i get 257 if (encrypted_sid.toByteArray().length > 256) { Random rg = new Random(); sequence_number = rg.nextInt(Integer.MAX_VALUE); return -2; // lets get a new seession } sid = new BigInteger(cipher.doFinal(encrypted_sid.toByteArray())); } catch (Exception e) { e.printStackTrace(); return -1; } String sidS = sid.toString(16); if (sidS.length() % 2 != 0) sidS = "0" + sidS; try { byte[] sidsnohex = MegaCrypt.decodeHexString(sidS); this.sid = MegaCrypt.base64_url_encode(new String(sidsnohex, "ISO-8859-1").substring(0, 43)); } catch (Exception e) { e.printStackTrace(); return -1; } } return 0; }
From source file:XmldapCertsAndKeys.java
public static RSAPrivateKey getXmldapPrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException { String exponentB64 = "AKh/FZVHiKxcIPA8g2mN8TUdMXuX58I7z4jS+57vYta387MG3DGZtQ/XXfHdPx9WjdoW0KWE2Pl5" + "SbOZW7tVcwigF88FrSJ5i6XDwUktmXjFwJM/TvUZlxWAKUdoOX8MC3DrAYZxeT3kC1mzAiBMPdC4" + "W4zNe7Zo0YgbsMzQZowVxZTP4GWa/L8o3adXTvdobP1nKW5buPj9vkgaGCTxE0vQzbuiGj1HRJe9" + "MRtvcU/I2shiIVE0F35wk8gw0FATtkvMpTpR12YVeo1JGZsHFQoD7gTD/n/NmC9Rjk2baYGj97hV" + "9EpDRcPNsMll2pVRy4Z45j2+t/yl8WjaqK5lhkE="; String modulusB64 = "ANMnkVA4xfpG0bLos9FOpNBjHAdFahy2cJ7FUwuXd/IShnG+5qF/z1SdPWzRxTtpFFyodtXlBUEI" + "biT+IbYPZF1vCcBrcFa8Kz/4rBjrpPZgllgA/WSVKjnJvw8q4/tO6CQZSlRlj/ebNK9VyT1kN+Mr" + "KV1SGTqaIJ2l+7Rd05WHscwZMPdVWBbRrg76YTfy6H/NlQIArNLZanPvE0Vd5QfD4ZyG2hTh3y7Z" + "lJAUndGJ/kfZw8sKuL9QSrh4eOTc280NQUmPGz6LP5MXNmu0RxEcomod1+ToKll90yEKFAUKuPYF" + "gm9J+vYm4tzRequLy/njteRIkcfAdcAtt6PCYjU="; byte[] exponentBytes = Base64.decode(exponentB64); byte[] modulusBytes = Base64.decode(modulusB64); BigInteger exponent = new BigInteger(1, exponentBytes); BigInteger modulus = new BigInteger(1, modulusBytes); RSAPrivateKeySpec ks = new RSAPrivateKeySpec(modulus, exponent); KeyFactory kf = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) kf.generatePrivate(ks); }
From source file:com.cellngine.crypto.RSACipher.java
@Override public void loadPrivateKey(final byte[] fromBytes) { final BigInteger[] keyData = this.getKeyData(fromBytes); final RSAPrivateKeySpec spec = new RSAPrivateKeySpec(keyData[0], keyData[1]); this.privateKey = this.getPrivateKey(spec); }
From source file:license.TestWakeLicense.java
/** * ??//from w ww . j av a 2 s .c o m * @return * @throws Exception */ private static PrivateKey readPrivateKeyFromFile() throws Exception { //?? ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream( new FileInputStream(new File("E:\\workspace\\TestProject\\src\\license\\private.key")))); try { BigInteger m = (BigInteger) oin.readObject(); BigInteger e = (BigInteger) oin.readObject(); RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e); KeyFactory fact = KeyFactory.getInstance("RSA"); return fact.generatePrivate(keySpec); } finally { // oin.close(); } }
From source file:net.adamcin.httpsig.testutil.KeyTestUtil.java
public static KeyPair getKeyPairFromProperties(String parentName, String keyName) { InputStream is = null;// w w w. j a v a 2s . c o m try { is = KeyTestUtil.class.getResourceAsStream("/" + parentName + "/" + keyName + ".properties"); Properties props = new Properties(); props.load(is); if (TYPE_RSA.equals(props.getProperty(P_TYPE))) { RSAPrivateKeySpec privSpec = null; if (props.getProperty(RSA_P) != null && props.getProperty(RSA_Q) != null && props.getProperty(RSA_U) != null) { privSpec = new RSAPrivateCrtKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E)), new BigInteger(props.getProperty(RSA_D)), new BigInteger(props.getProperty(RSA_P)), new BigInteger(props.getProperty(RSA_Q)), new BigInteger(props.getProperty(RSA_PE)), new BigInteger(props.getProperty(RSA_QE)), new BigInteger(props.getProperty(RSA_U))); } else { privSpec = new RSAPrivateKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_D))); } RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E))); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } else if (TYPE_DSA.equals(props.getProperty(P_TYPE))) { DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(new BigInteger(props.getProperty(DSA_X)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(new BigInteger(props.getProperty(DSA_Y)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } } catch (Exception e) { LOGGER.error("Failed to read properties", e); } finally { IOUtils.closeQuietly(is); } return null; }
From source file:de.pawlidi.openaletheia.generator.KeyGenerator.java
public static RSAPrivateKeySpec readPrivateKeySpec(final String directory) { if (StringUtils.isBlank(directory) || !new File(directory).isDirectory()) { return null; }/*www . ja v a 2 s . c o m*/ RSAPrivateKeySpec privateKeySpec = null; ObjectInputStream inputStream = null; try { inputStream = new ObjectInputStream( new BufferedInputStream(new FileInputStream(new File(directory, PRIVATE_KEYSPEC_FILE)))); try { BigInteger m = (BigInteger) inputStream.readObject(); BigInteger e = (BigInteger) inputStream.readObject(); privateKeySpec = new RSAPrivateKeySpec(m, e); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (inputStream != null) { inputStream.close(); inputStream = null; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return privateKeySpec; }
From source file:sernet.verinice.encryption.test.CryptoTest.java
X509Certificate generateCertificate(String dn, KeyPair pair, int days) throws GeneralSecurityException, IOException { PublicKey publicKey = pair.getPublic(); PrivateKey privateKey = pair.getPrivate(); if (publicKey instanceof RSAPublicKey) { RSAPublicKey rsaPk = (RSAPublicKey) publicKey; RSAPublicKeySpec rsaPkSpec = new RSAPublicKeySpec(rsaPk.getModulus(), rsaPk.getPublicExponent()); try {/*from w w w . j ava 2 s. c om*/ publicKey = KeyFactory.getInstance("RSA").generatePublic(rsaPkSpec); } catch (InvalidKeySpecException e) { publicKey = pair.getPublic(); } } if (privateKey instanceof RSAPrivateKey) { RSAPrivateKey rsaPk = (RSAPrivateKey) privateKey; RSAPrivateKeySpec rsaPkSpec = new RSAPrivateKeySpec(rsaPk.getModulus(), rsaPk.getPrivateExponent()); try { privateKey = KeyFactory.getInstance("RSA").generatePrivate(rsaPkSpec); } catch (InvalidKeySpecException e) { privateKey = pair.getPrivate(); } } X509V3CertificateGenerator certGen = new X509V3CertificateGenerator(); String commonName = "CN=" + dn + ", OU=None, O=None L=None, C=None"; X500Principal dnName = new X500Principal(commonName); certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis())); certGen.setIssuerDN(dnName); certGen.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(true)); Calendar cal = Calendar.getInstance(); certGen.setNotBefore(cal.getTime()); cal.add(Calendar.YEAR, 5); certGen.setNotAfter(cal.getTime()); certGen.setSubjectDN(dnName); certGen.setPublicKey(publicKey); certGen.setSignatureAlgorithm("MD5WithRSA"); return certGen.generate(privateKey, BouncyCastleProvider.PROVIDER_NAME); }