public byte[] getEncoded() { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); DEROutputStream dOut = new DEROutputStream(bOut); SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()), new RSAPublicKeyStructure(getModulus(), getPublicExponent()).getDERObject()); try { dOut.writeObject(info); dOut.close(); } catch (IOException e) { throw new RuntimeException("Error encoding RSA public key"); } return bOut.toByteArray(); }
/** * makes RSA public key from bin byte array. * * @param b byte array that contains the key * @return * @see JCERSAPublicKey */ public static RSAPublicKey extractBinaryRSAKey(final byte[] b) { RSAPublicKey theKey; try { final ASN1InputStream ais = new ASN1InputStream(b); final Object asnObject = ais.readObject(); final ASN1Sequence sequence = (ASN1Sequence) asnObject; final RSAPublicKeyStructure tempKey = new RSAPublicKeyStructure(sequence); theKey = getRSAPublicKey(tempKey.getModulus(), tempKey.getPublicExponent()); ais.close(); } catch (final IOException e) { logger.warn("Caught exception:" + e.getMessage()); theKey = null; } return theKey; }
/** * checks signature of PKCS1-padded SHA1 hash of the input * * Hint: A different implementation of this method can be found in the svn history revision<=229. * * @param signature * signature to check * @param signingKey * public key from signing * @param input * byte array, signature is made over * * @return true, if the signature is correct * */ public static boolean verifySignature(byte[] signature, RSAPublicKeyStructure signingKey, byte[] input) { byte[] hash = getDigest(input); try { RSAKeyParameters myRSAKeyParameters = new RSAKeyParameters(false, signingKey.getModulus(), signingKey.getPublicExponent()); PKCS1Encoding pkcsAlg = new PKCS1Encoding(new RSAEngine()); pkcsAlg.init(false, myRSAKeyParameters); byte[] decryptedSignature = pkcsAlg.processBlock(signature, 0, signature.length); return Encoding.arraysEqual(hash, decryptedSignature); } catch (Exception e) { log.log(Level.WARNING, "unexpected", e); return false; } }
/** * makes RSA public key from bin byte array * * @param s * string that contais the key * @return * @see JCERSAPublicKey */ public static RSAPublicKey extractBinaryRSAKey(byte[] b) { RSAPublicKey theKey; try { ASN1InputStream ais = new ASN1InputStream(b); Object asnObject = ais.readObject(); ASN1Sequence sequence = (ASN1Sequence) asnObject; RSAPublicKeyStructure tempKey = new RSAPublicKeyStructure(sequence); theKey = getRSAPublicKey(tempKey.getModulus(), tempKey.getPublicExponent()); } catch (IOException e) { log.warning("Caught exception:" + e.getMessage()); theKey = null; } return theKey; }
JCERSAPublicKey( SubjectPublicKeyInfo info) { try { RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.parsePublicKey()); this.modulus = pubKey.getModulus(); this.publicExponent = pubKey.getPublicExponent(); } catch (IOException e) { throw new IllegalArgumentException("invalid info structure in RSA public key"); } }
JCERSAPublicKey( SubjectPublicKeyInfo info) { try { RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.getPublicKey()); this.modulus = pubKey.getModulus(); this.publicExponent = pubKey.getPublicExponent(); } catch (IOException e) { throw new IllegalArgumentException("invalid info structure in RSA public key"); } }
private static PublicKey fixPubKey(byte[] orig) throws InvalidKeySpecException, NoSuchAlgorithmException { try { ASN1InputStream in = new ASN1InputStream(orig); ASN1Primitive obj = in.readObject(); RSAPublicKeyStructure keyStruct = RSAPublicKeyStructure.getInstance(obj); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(keyStruct.getModulus(), keyStruct.getPublicExponent()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(keySpec); } catch (IOException e) { throw new InvalidKeySpecException("Unable to load public key with stopgap ASN1 method", e); } }
/** * converts a RSAPublicKey into PKCS1-encoding (ASN.1). * * @param pubKeyStruct * @return PKCS1-encoded RSA PUBLIC KEY * @see JCERSAPublicKey */ public static byte[] getPKCS1EncodingFromRSAPublicKey(final RSAPublicKey pubKeyStruct) { try { final RSAPublicKeyStructure myKey = new RSAPublicKeyStructure(pubKeyStruct.getModulus(), pubKeyStruct.getPublicExponent()); final ByteArrayOutputStream bOut = new ByteArrayOutputStream(); final ASN1OutputStream aOut = new ASN1OutputStream(bOut); aOut.writeObject(myKey.toASN1Object()); aOut.close(); return bOut.toByteArray(); } catch (final Exception e) { return null; } }
TempJCERSAPublicKey( SubjectPublicKeyInfo info) { try { RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.parsePublicKey()); this.modulus = pubKey.getModulus(); this.publicExponent = pubKey.getPublicExponent(); } catch (IOException e) { throw new IllegalArgumentException("invalid info structure in RSA public key"); } }
/** * checks row signature * * @param signature * signature to check * @param signingKey * public key from signing * @param input * byte array, signature is made over * * @return true, if the signature is correct * */ public static boolean verifySignatureXXXX(byte[] signature, RSAPublicKeyStructure signingKey, byte[] input) { byte[] hash = getDigest(input); try { Signature sig = Signature.getInstance("SHA1withRSA"); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(signingKey .getModulus(), signingKey.getPublicExponent()); PublicKey pubKey = keyFactory.generatePublic(keySpec); sig.initVerify(pubKey); sig.update(input); log.info(""); log.info(" HERE -> " + sig.verify(signature)); RSAKeyParameters myRSAKeyParameters = new RSAKeyParameters(false, signingKey.getModulus(), signingKey.getPublicExponent()); RSAEngine rsaAlg = new RSAEngine(); rsaAlg.init(false, myRSAKeyParameters); byte[] decryptedSignature = rsaAlg.processBlock(signature, 0, signature.length); log.info(" inpu = " + Encoding.toHexString(input)); log.info(" hash = " + Encoding.toHexString(hash)); log.info(""); log.info(" sign = " + Encoding.toHexString(signature)); log.info(" decr = " + Encoding.toHexString(decryptedSignature)); return Encoding.arraysEqual(hash, decryptedSignature); } catch (Exception e) { log.log(Level.WARNING, "unexpected", e); return false; } }
/** * converts a RSAPublicKey into PKCS1-encoding (ASN.1) * * @param rsaPublicKey * @see JCERSAPublicKey * @return PKCS1-encoded RSA PUBLIC KEY */ public static byte[] getPKCS1EncodingFromRSAPublicKey(RSAPublicKey pubKeyStruct) { try { RSAPublicKeyStructure myKey = new RSAPublicKeyStructure(pubKeyStruct.getModulus(), pubKeyStruct.getPublicExponent()); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); ASN1OutputStream aOut = new ASN1OutputStream(bOut); aOut.writeObject(myKey.toASN1Object()); return bOut.toByteArray(); } catch (Exception e) { return null; } }
public byte[] getEncoded() { return KeyUtil.getEncodedSubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKeyStructure(getModulus(), getPublicExponent())); }