List of usage examples for java.security.interfaces RSAPrivateKey getModulus
public BigInteger getModulus();
From source file:de.pawlidi.openaletheia.generator.KeyGenerator.java
public static boolean flushPrivateKeySpec(final String directory, RSAPrivateKey privateKey) { if (StringUtils.isBlank(directory) || !new File(directory).isDirectory() || privateKey == null) { return false; }/*from www. j a va 2 s .c om*/ return writeKeySpec(new File(directory, PRIVATE_KEYSPEC_FILE), privateKey.getModulus(), privateKey.getPrivateExponent()); }
From source file:cn.util.RSAUtils.java
/** * ? /*from w ww .j a v a 2s .co m*/ * * @param data * @param privateKey * @return * @throws Exception */ public static String decryptByPrivateKey(String data) throws Exception { RSAPrivateKey privateKey = getPrivateKey(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); // int key_len = privateKey.getModulus().bitLength() / 8; // byte[] bytes = data.getBytes(); // byte[] bcd = ASCII_To_BCD(bytes, bytes.length); // System.err.println(bcd.length); // //? String ming = ""; byte[][] arrays = splitArray(Base64Util.decryptBASE64(data), key_len); for (byte[] arr : arrays) { ming += new String(cipher.doFinal(arr)); } return ming; }
From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java
/** * RSA???/* w w w.ja v a 2 s .co m*/ * <dl> * <dt>? * <dd>RSA??????2048?????? * </dl> * @return RSA? */ public static KeyPair createKeyPair() { try { final KeyPairGenerator generator = KeyPairGenerator.getInstance(ALGO_KEY); generator.initialize(2048); final KeyPair pair = generator.generateKeyPair(); if (LOG.isDebugEnabled()) { final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic(); final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate(); LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray())); LOG.debug("public-exponent={}", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray())); LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray())); LOG.debug("private-exponent={}", Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray())); } return pair; } catch (final NoSuchAlgorithmException e) { throw new StandardRuntimeException(e); } }
From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java
/** * RSA???/*from w w w. j a v a 2 s . c o m*/ * <dl> * <dt>? * <dd>RSA??????2048?????? * </dl> * @return RSA? */ public static KeyPair createKeyPair() { try { final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); final KeyPair pair = generator.generateKeyPair(); if (LOG.isDebugEnabled()) { final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic(); final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate(); LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray())); LOG.debug("public-exponent={}", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray())); LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray())); LOG.debug("private-exponent={}", Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray())); } return pair; } catch (final NoSuchAlgorithmException e) { throw new StandardRuntimeException(e); } }
From source file:mitm.common.security.certificate.GenerateKeyPairs.java
private void writeKeyPair(KeyPair keyPair) throws IOException { System.out.println("Keypair:"); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); System.out.println("Modulus:"); System.out.println(bigIntToString(privateKey.getModulus())); System.out.println();// w w w.j a va2s. c o m System.out.println("Private exponent:"); System.out.println(bigIntToString(privateKey.getPrivateExponent())); System.out.println(); System.out.println("Public exponent:"); System.out.println(bigIntToString(publicKey.getPublicExponent())); System.out.println(); System.out.println("Encoded public key:"); System.out.println(bytesToHex(keyPair.getPublic().getEncoded())); System.out.println(); System.out.println("Encoded private key:"); System.out.println(bytesToHex(keyPair.getPrivate().getEncoded())); System.out.println(); System.out.println("Serial number:"); System.out.println(bigIntToString(serialNumberGenerator.generate())); }
From source file:fr.cls.atoll.motu.library.misc.vfs.provider.gsiftp.ProxyTool.java
/** * Verify./* w w w.j a v a 2s .c o m*/ * * @throws GeneralSecurityException the general security exception */ private void verify() throws GeneralSecurityException { RSAPublicKey pkey = (RSAPublicKey) this.certificates[0].getPublicKey(); RSAPrivateKey prkey = (RSAPrivateKey) userKey; if (!pkey.getModulus().equals(prkey.getModulus())) { throw new GeneralSecurityException("Certificate and private key specified do not match"); } }
From source file:com.thoughtworks.go.server.util.HttpTestUtil.java
private KeyPair generateKeyPair() { try {// w w w.ja v a2 s .com KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair(); RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate(); RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic(); KeyFactory fact = KeyFactory.getInstance("RSA", "BC"); RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(), privateSeed.getPrivateExponent()); RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(), publicSeed.getPublicExponent()); return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec)); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.candlepin.util.X509CRLStreamWriter.java
public X509CRLStreamWriter(InputStream crlToChange, RSAPrivateKey key, AuthorityKeyIdentifierStructure akiStructure) throws CryptoException, IOException { this.deletedEntries = new HashSet<BigInteger>(); this.deletedEntriesLength = 0; this.newEntries = new LinkedList<DERSequence>(); this.crlIn = crlToChange; this.count = new AtomicInteger(); /* The length of an RSA signature is padded out to the length of the modulus * in bytes. See http://stackoverflow.com/questions/6658728/rsa-signature-size */*from w ww . j a va2 s. c o m*/ * If the original CRL was signed with a 2048 bit key and someone sends in a * 4096 bit key, we need to account for the discrepancy. */ int newSigBytes = key.getModulus().bitLength() / 8; /* Now we need a byte array to figure out how long the new signature will * be when encoded. */ byte[] dummySig = new byte[newSigBytes]; Arrays.fill(dummySig, (byte) 0x00); this.newSigLength = new DERBitString(dummySig).getDEREncoded().length; this.key = key; this.akiStructure = akiStructure; }
From source file:org.globus.gsi.gssapi.GlobusGSSContextImpl.java
protected void verifyDelegatedCert(X509Certificate certificate) throws GeneralSecurityException { RSAPublicKey pubKey = (RSAPublicKey) certificate.getPublicKey(); RSAPrivateKey privKey = (RSAPrivateKey) this.keyPair.getPrivate(); if (!pubKey.getModulus().equals(privKey.getModulus())) { throw new GeneralSecurityException(i18n.getMessage("keyMismatch")); }//from w w w .j av a 2 s .co m }
From source file:org.globus.myproxy.MyProxy.java
/** * Retrieves delegated credentials from the MyProxy server. * * @param credential/* w w w . j av a 2 s. c om*/ * The local GSI credentials to use for authentication. * Can be set to null if no local credentials. * @param params * The parameters for the get operation. * @return GSSCredential * The retrieved delegated credentials. * @exception MyProxyException * If an error occurred during the operation. */ public GSSCredential get(GSSCredential credential, GetParams params) throws MyProxyException { if (params == null) { throw new IllegalArgumentException("params == null"); } if (credential == null) { try { credential = getAnonymousCredential(); } catch (GSSException e) { throw new MyProxyException("Failed to create anonymous credentials", e); } } String msg = params.makeRequest(); Socket gsiSocket = null; OutputStream out = null; InputStream in = null; try { gsiSocket = getSocket(credential); if (credential.getName().isAnonymous()) { this.context.requestAnonymity(true); } out = gsiSocket.getOutputStream(); in = gsiSocket.getInputStream(); if (!((GssSocket) gsiSocket).getContext().getConfState()) throw new Exception("Confidentiality requested but not available"); // send message out.write(msg.getBytes()); out.flush(); if (logger.isDebugEnabled()) { logger.debug("Req sent:" + params); } // may require authz handshake handleReply(in, out, params.getAuthzCreds(), params.getWantTrustroots()); // start delegation - generate key pair KeyPair keyPair = CertificateUtil.generateKeyPair("RSA", DEFAULT_KEYBITS); // According to the MyProxy protocol, the MyProxy server // will ignore the subject in the client's certificate // signing request (CSR). However, in some cases it is // helpful to control the CSR subject (for example, when // the MyProxy server is using a CA back-end that can only // issue certificates with subjects matching the request). // So we construct the CSR subject using the given MyProxy // username (if possible). String CSRsubjectString = params.getUserName(); CSRsubjectString = CSRsubjectString.trim(); if (CSRsubjectString.contains("CN=") || CSRsubjectString.contains("cn=")) { // If the MyProxy username is a DN, use it. if (CSRsubjectString.charAt(0) == '/') { // "good enough" conversion of OpenSSL DN strings CSRsubjectString = CSRsubjectString.substring(1); CSRsubjectString = CSRsubjectString.replace('/', ','); } } else { CSRsubjectString = "CN=" + CSRsubjectString; } X509Name CSRsubjectName; try { CSRsubjectName = new X509Name(CSRsubjectString); } catch (Exception e) { // If our X509Name construction fails for any reason, // just use a default value (as in the past). CSRsubjectName = new X509Name("CN=ignore"); } if (logger.isDebugEnabled()) { logger.debug("CSR subject: " + CSRsubjectName.toString()); } BouncyCastleCertProcessingFactory certFactory = BouncyCastleCertProcessingFactory.getDefault(); byte[] req = null; req = certFactory.createCertificateRequest(CSRsubjectName, "SHA1WithRSAEncryption", keyPair); // send the request to server out.write(req); out.flush(); // read the number of certificates int size = in.read(); if (logger.isDebugEnabled()) { logger.debug("Reading " + size + " certs"); } X509Certificate[] chain = new X509Certificate[size]; for (int i = 0; i < size; i++) { chain[i] = certFactory.loadCertificate(in); // DEBUG: display the cert names if (logger.isDebugEnabled()) { logger.debug("Received cert: " + chain[i].getSubjectDN()); } } // get the response handleReply(in); // make sure the private key belongs to the right public key // currently only works with RSA keys RSAPublicKey pkey = (RSAPublicKey) chain[0].getPublicKey(); RSAPrivateKey prkey = (RSAPrivateKey) keyPair.getPrivate(); if (!pkey.getModulus().equals(prkey.getModulus())) { throw new MyProxyException("Private/Public key mismatch!"); } X509Credential newCredential = null; newCredential = new X509Credential(keyPair.getPrivate(), chain); return new GlobusGSSCredentialImpl(newCredential, GSSCredential.INITIATE_AND_ACCEPT); } catch (Exception e) { throw new MyProxyException("MyProxy get failed.", e); } finally { // close socket close(out, in, gsiSocket); } }