pemreader.java
来自「内容:基于jdk1.4的加密算法的具体实现」· Java 代码 · 共 576 行 · 第 1/2 页
JAVA
576 行
return (X509Certificate)certFact.generateCertificate(bIn); } catch (Exception e) { throw new IOException("problem parsing cert: " + e.toString()); } } /** * Reads in a PKCS10 certification request. * * @return the certificate request. * @throws IOException if an I/O error occured */ private PKCS10CertificationRequest readCertificateRequest( String endMarker) throws IOException { String line; StringBuffer buf = new StringBuffer(); while ((line = readLine()) != null) { if (line.indexOf(endMarker) != -1) { break; } buf.append(line.trim()); } if (line == null) { throw new IOException(endMarker + " not found"); } try { return new PKCS10CertificationRequest(Base64.decode(buf.toString())); } catch (Exception e) { throw new IOException("problem parsing cert: " + e.toString()); } } /** * Reads in a X509 Attribute Certificate. * * @return the X509 Attribute Certificate * @throws IOException if an I/O error occured */ private X509AttributeCertificate readAttributeCertificate( String endMarker) throws IOException { String line; StringBuffer buf = new StringBuffer(); while ((line = readLine()) != null) { if (line.indexOf(endMarker) != -1) { break; } buf.append(line.trim()); } if (line == null) { throw new IOException(endMarker + " not found"); } return new X509V2AttributeCertificate(Base64.decode(buf.toString())); } /** * Reads in a PKCS7 object. This returns a ContentInfo object suitable for use with the CMS * API. * * @return the X509Certificate * @throws IOException if an I/O error occured */ private ContentInfo readPKCS7( String endMarker) throws IOException { String line; StringBuffer buf = new StringBuffer(); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); while ((line = readLine()) != null) { if (line.indexOf(endMarker) != -1) { break; } line = line.trim(); buf.append(line.trim()); Base64.decode(buf.substring(0, (buf.length() / 4) * 4), bOut); buf.delete(0, (buf.length() / 4) * 4); } if (buf.length() != 0) { throw new RuntimeException("base64 data appears to be truncated"); } if (line == null) { throw new IOException(endMarker + " not found"); } ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray()); try { ASN1InputStream aIn = new ASN1InputStream(bIn); return ContentInfo.getInstance(aIn.readObject()); } catch (Exception e) { throw new IOException("problem parsing PKCS7 object: " + e.toString()); } } /** * create the secret key needed for this object, fetching the password */ private SecretKey getKey( String algorithm, int keyLength, byte[] salt) throws IOException { byte[] key = new byte[keyLength]; int offset = 0; int bytesNeeded = keyLength; if (pFinder == null) { throw new IOException("No password finder specified, but a password is required"); } char[] password = pFinder.getPassword(); if (password == null) { throw new IOException("Password is null, but a password is required"); } OpenSSLPBEParametersGenerator pGen = new OpenSSLPBEParametersGenerator(); pGen.init(PBEParametersGenerator.PKCS5PasswordToBytes(password), salt); return new javax.crypto.spec.SecretKeySpec(((KeyParameter)pGen.generateDerivedParameters(keyLength * 8)).getKey(), algorithm); } /** * Read a Key Pair */ private KeyPair readKeyPair( String type, String endMarker) throws Exception { boolean isEncrypted = false; String line = null; String dekInfo = null; StringBuffer buf = new StringBuffer(); while ((line = readLine()) != null) { if (line.startsWith("Proc-Type: 4,ENCRYPTED")) { isEncrypted = true; } else if (line.startsWith("DEK-Info:")) { dekInfo = line.substring(10); } else if (line.indexOf(endMarker) != -1) { break; } else { buf.append(line.trim()); } } // // extract the key // byte[] keyBytes = null; if (isEncrypted) { StringTokenizer tknz = new StringTokenizer(dekInfo, ","); String encoding = tknz.nextToken(); if (encoding.equals("DES-EDE3-CBC")) { String alg = "DESede"; byte[] iv = Hex.decode(tknz.nextToken()); Key sKey = getKey(alg, 24, iv); Cipher c = Cipher.getInstance( "DESede/CBC/PKCS5Padding", provider); c.init(Cipher.DECRYPT_MODE, sKey, new IvParameterSpec(iv)); keyBytes = c.doFinal(Base64.decode(buf.toString())); } else if (encoding.equals("DES-CBC")) { String alg = "DES"; byte[] iv = Hex.decode(tknz.nextToken()); Key sKey = getKey(alg, 8, iv); Cipher c = Cipher.getInstance( "DES/CBC/PKCS5Padding", provider); c.init(Cipher.DECRYPT_MODE, sKey, new IvParameterSpec(iv)); keyBytes = c.doFinal(Base64.decode(buf.toString())); } else { throw new IOException("unknown encryption with private key"); } } else { keyBytes = Base64.decode(buf.toString()); } KeySpec pubSpec, privSpec; ByteArrayInputStream bIn = new ByteArrayInputStream(keyBytes); ASN1InputStream aIn = new ASN1InputStream(bIn); ASN1Sequence seq = (ASN1Sequence)aIn.readObject(); if (type.equals("RSA")) { DERInteger v = (DERInteger)seq.getObjectAt(0); DERInteger mod = (DERInteger)seq.getObjectAt(1); DERInteger pubExp = (DERInteger)seq.getObjectAt(2); DERInteger privExp = (DERInteger)seq.getObjectAt(3); DERInteger p1 = (DERInteger)seq.getObjectAt(4); DERInteger p2 = (DERInteger)seq.getObjectAt(5); DERInteger exp1 = (DERInteger)seq.getObjectAt(6); DERInteger exp2 = (DERInteger)seq.getObjectAt(7); DERInteger crtCoef = (DERInteger)seq.getObjectAt(8); pubSpec = new RSAPublicKeySpec( mod.getValue(), pubExp.getValue()); privSpec = new RSAPrivateCrtKeySpec( mod.getValue(), pubExp.getValue(), privExp.getValue(), p1.getValue(), p2.getValue(), exp1.getValue(), exp2.getValue(), crtCoef.getValue()); } else // "DSA" { DERInteger v = (DERInteger)seq.getObjectAt(0); DERInteger p = (DERInteger)seq.getObjectAt(1); DERInteger q = (DERInteger)seq.getObjectAt(2); DERInteger g = (DERInteger)seq.getObjectAt(3); DERInteger y = (DERInteger)seq.getObjectAt(4); DERInteger x = (DERInteger)seq.getObjectAt(5); privSpec = new DSAPrivateKeySpec( x.getValue(), p.getValue(), q.getValue(), g.getValue()); pubSpec = new DSAPublicKeySpec( y.getValue(), p.getValue(), q.getValue(), g.getValue()); } KeyFactory fact = KeyFactory.getInstance(type, provider); return new KeyPair( fact.generatePublic(pubSpec), fact.generatePrivate(privSpec)); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?