fork download
  1.  
  2. public class SecurityApplet extends Applet {
  3. //private String osName;
  4. private String browserName = new String();
  5. private JSObject jSObject = null;
  6. private BASE64Encoder bASE64Encoder = new BASE64Encoder();
  7. private BASE64Decoder bASE64Decoder = new BASE64Decoder();
  8.  
  9.  
  10. private String appletVersion = "41";
  11.  
  12. private final class CERT_DETAILS {
  13. private static final String NAME="Name" ;
  14. private static final String EMAIL="Email";
  15. private static final String CITY="City";
  16. private static final String STATE="State";
  17. private static final String COUNTRY="Country";
  18. private static final String EXPIRY_DATE="Expiry Date";
  19. private static final String DIGITAL_SIGN="Digital Signature";
  20. private static final String CN_NAME="CNName";
  21. private static final String ISSUER_ORG_NAME="Organization";
  22. private static final String ISSUER_ORG_UNIT="Organization Unit";
  23. private static final String ISSUER_COUNTRY="Issuer Country";
  24. private static final String ISSUER_CNAME="Issuer Name";
  25. private static final String ORG_NAME="Organoization Name";
  26. private static final String ORG_UNIT="Organization Unit";
  27. private static final String PUBLIC_KEY="Public Key";
  28. private static final String USAGE="Usage";
  29. }
  30.  
  31. /*
  32. * Signing Code
  33. * jarsigner -keystore c:/my.keystore -storepass ozziepassword e:/securityApplet.jar ozzie
  34. * jarsigner -verify e:/securityApplet.jar
  35. */
  36. /**
  37. * Generate Key Store
  38. keytool -genkey -alias ozzie -dname "cn=ozzie, ou=engr, o=YourCompanyName, c=US" -keypass ozziepassword -keystore C:\projectname\lib\security\.keystore -
  39.  
  40. storepass ozziepassword
  41.  
  42. Sign JAr
  43. jarsigner -keystore \my.keystore -storepass ozziepassword /Users/kuntalshah/Desktop/V4Security.jar ozzie
  44.  
  45. Verify JAR
  46. jarsigner -verify /Users/kuntalshah/Desktop/V4Security.jar
  47.  
  48. Signing Command with full path
  49. "C:\Program Files (x86)\Java\jdk1.6.0_16\bin\jarsigner.exe" -keystore d:/my.keystore -storepass ozziepassword d:/securityApplet.jar ozzie
  50. */
  51.  
  52. @Override
  53. public void init() {
  54. printMessageToConsole("Applet Initialized Version : " + appletVersion);
  55. createOSPolicyFile();
  56. browserName = "Microsoft Internet Explorer";
  57. }
  58.  
  59. private void createOSPolicyFile() {
  60. File policyFile = new File(System.getProperty("user.home") + File.separator + ".java.policy");
  61. printMessageToConsole("Creating OS Policy File");
  62. try {
  63. printMessageToConsole("Policy File : " + policyFile.getAbsolutePath());
  64. if (policyFile.exists()) {
  65. policyFile.delete();
  66. }
  67. policyFile.createNewFile();
  68. FileOutputStream policyOutStream = new FileOutputStream(policyFile);
  69. String policyfileContent = "grant";
  70. policyfileContent = policyfileContent + "\n{";
  71. policyfileContent = policyfileContent + "permission java.security.AllPermission;";
  72. policyfileContent = policyfileContent + "\n};";
  73. policyOutStream.write(policyfileContent.getBytes());
  74. policyOutStream.flush();
  75. policyOutStream.close();
  76. } catch (Exception e) {
  77. printMessageToConsole("Exception on Creatign policy file : " + policyFile.getAbsolutePath());
  78. e.printStackTrace();
  79. }
  80. }
  81.  
  82. // Configuration Methods
  83. /**
  84. * Read the Os name from systemProperty
  85. */
  86. public String getOsName() {
  87. try {
  88. String osName = System.getProperty("os.name");
  89. printMessageToConsole("Operating system name =>" + osName);
  90. return osName;
  91. } catch (Exception e) {
  92. printMessageToConsole("Error in Get OS Name.");
  93. e.printStackTrace();
  94. return null;
  95. }
  96. }
  97.  
  98. // Initialize Key Stores
  99. private KeyStore initializeBrowserKeyStore() {
  100. KeyStore keyStore = null;
  101. printMessageToConsole("Initializing Key Store for " + browserName);
  102. try {
  103. keyStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
  104. keyStore.load(null, null);
  105. } catch (Exception e) {
  106. printMessageToConsole("Error in method: initializeBrowserKeyStore() -> initialize browser Key Store.");
  107. e.printStackTrace();
  108. }
  109. return keyStore;
  110. }
  111.  
  112. public List<String> getCertificateAllDetails() throws NoSuchFieldException,
  113. printMessageToConsole("Get All Certificate Details");
  114. String certString = "";
  115. int count = 0;
  116. String pubKey = "";
  117. KeyStore browserKeyStore = null;
  118. String certDetails = "";
  119. browserKeyStore = initializeBrowserKeyStore();
  120. List<String> resultValues = new ArrayList<String>();
  121. String aliasnew = null;
  122.  
  123. printMessageToConsole(browserName);
  124. if (browserKeyStore != null) {
  125. printMessageToConsole("INSIDE IE CERTIFICATE READING");
  126. Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
  127. spiField.setAccessible(true);
  128. KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
  129. Field entriesField = spi.getClass().getSuperclass()
  130. .getDeclaredField("entries");
  131. entriesField.setAccessible(true);
  132. @SuppressWarnings("rawtypes")
  133. Collection entries = (Collection) entriesField.get(spi);
  134. resultValues.add("Total Certificates in Browser : "
  135. + entries.size() + "<br><br><br>");
  136. printMessageToConsole("Total Certificates in Browser : "
  137. + entries.size());
  138. for (Object entry : entries) {
  139. aliasnew = (String) invokeGetter(entry, "getAlias");
  140. PrivateKey privateKey = (PrivateKey) invokeGetter(entry,
  141. "getPrivateKey");
  142. X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(
  143. entry, "getCertificateChain");
  144. for (X509Certificate current : certificateChain) {
  145. certString = "";
  146. /*if (certDetails != null
  147. && getkeyUsage(current.getKeyUsage()) != "") */
  148. {
  149. count++;
  150. pubKey = this.bASE64Encoder.encode(current
  151. .getPublicKey().getEncoded());
  152. certDetails = getX509CertificateDetails(current);
  153. Map<String, String> valueMap = new HashMap<String, String>();
  154. valueMap = getMetadata(certDetails);
  155. certString += "====================== Certificate Details for Certificate No : "
  156. + count + "======================<br>";
  157. certString += "Alias : " + aliasnew + " <br>";
  158. certString += "Name : "
  159. + valueMap.get(CERT_DETAILS.NAME) + " <br>";
  160. certString += "Email : "
  161. + valueMap.get(CERT_DETAILS.EMAIL) + " <br>";
  162. certString += "City : "
  163. + valueMap.get(CERT_DETAILS.CITY) + "<br>";
  164. certString += "State : "
  165. + valueMap.get(CERT_DETAILS.STATE) + " <br>";
  166. certString += "Country : "
  167. + valueMap.get(CERT_DETAILS.COUNTRY) + " <br>";
  168. certString += "Expiry Date : "
  169. + valueMap.get(CERT_DETAILS.EXPIRY_DATE)
  170. + "<br>";
  171. certString += "Issuer Organization : "
  172. + valueMap.get(CERT_DETAILS.ISSUER_ORG_NAME)
  173. + "<br>";
  174. certString += "Issuer Organization Unit : "
  175. + valueMap.get(CERT_DETAILS.ISSUER_ORG_UNIT)
  176. + "<br>";
  177. certString += "Key Usage : "
  178. + getkeyUsage(current.getKeyUsage()) + "<br>";
  179. certString += "CNName : "
  180. + valueMap.get(CERT_DETAILS.CN_NAME) + "<br>";
  181. // certString += "Public Key : " + pubKey + "\n";
  182. certString += "====================== "
  183. + "<br><br><br>";
  184. printMessageToConsole(certString);
  185. resultValues.add(certString);
  186. break;
  187. }
  188. }
  189. }
  190. } else {
  191. printMessageToConsole("Keystore is NULL");
  192. }
  193. return resultValues;
  194. }
  195.  
  196.  
  197. // Certificate Access Methods
  198. public String isCertificateInstalled(String pubKey) {
  199. String value = "false";
  200. PrivateKey privatekey = null;
  201. KeyStore keyStoreBrowser = null;
  202. printMessageToConsole("------------------------------Inside isCertificateInstalled method---------------------------------");
  203. try {
  204. keyStoreBrowser = initializeBrowserKeyStore();
  205. if (keyStoreBrowser!=null){
  206. printMessageToConsole("Browser Keys : " + keyStoreBrowser.size());
  207. privatekey = getPrivateKeyFromKeyStore(pubKey, keyStoreBrowser);
  208. if(privatekey != null) {
  209. value = "true";
  210. }
  211. else {
  212. printMessageToConsole("Private Key Not Found.");
  213. }
  214. }
  215. else {
  216. printMessageToConsole("Browser Keystore Null.");
  217. }
  218. } catch (Exception e) {
  219. printMessageToConsole("Exception in Certificate Installation Check.");
  220. e.printStackTrace();
  221. }
  222. printMessageToConsole("------------------------------End isCertificateInstalled method---------------------------------");
  223. return value;
  224. }
  225.  
  226. private PrivateKey getPrivateKeyFromKeyStore(String pubkey,KeyStore browser) {
  227. PrivateKey privateKey = null;
  228. String pubKey1 = "";
  229. printMessageToConsole("Inside Get Private Key" );
  230.  
  231. if (browser != null) {
  232. printMessageToConsole("Checking Browser Key Store keys for : " + browserName);
  233. try {
  234. Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
  235. spiField.setAccessible(true);
  236. KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browser);
  237. Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
  238. entriesField.setAccessible(true);
  239. @SuppressWarnings("rawtypes")
  240. Collection entries = (Collection) entriesField.get(spi);
  241. for (Object entry : entries) {
  242. String alias = (String) invokeGetter(entry, "getAlias");
  243. X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
  244. for (X509Certificate current : certificateChain) {
  245. pubKey1 = this.bASE64Encoder.encode(current.getPublicKey().getEncoded());
  246. if (pubkey.equals(pubKey1) && !pubkey.equals("")) {
  247. // Check for key usage and also put a null check
  248. privateKey = (PrivateKey) invokeGetter(entry,"getPrivateKey");
  249. printMessageToConsole("Private Key is " + privateKey.toString());
  250. printMessageToConsole("Private Key Found from Browser");
  251. return privateKey;
  252. }
  253. }
  254. }
  255. } catch (Exception e) {
  256. printMessageToConsole("Exception while looping through browser certificates.");
  257. e.printStackTrace();
  258. return null;
  259. }
  260.  
  261. }
  262. return privateKey;
  263. }
  264.  
  265.  
  266. private String getX509CertificateDetails(X509Certificate cerificate) {
  267. String certDetails = "";
  268. printMessageToConsole("Inside Get Browser Certificate Details for Netscape.");
  269. try {
  270. if (cerificate.getSubjectAlternativeNames()!=null){
  271. String emailAddress[] = cerificate.getSubjectAlternativeNames().toString().split(",");
  272. if (!emailAddress.equals(null)) {
  273. certDetails = "E-Mail Address"
  274. + "="
  275. + emailAddress[1].substring(0,
  276. emailAddress[1].length() - 2) + ",";
  277. }
  278. }
  279. DateFormat dateFormat = new SimpleDateFormat(
  280. "HH:mm:ss a dd-MM-yyyy");
  281. certDetails += cerificate.getSubjectDN().getName() + "///"
  282. + cerificate.getIssuerDN().getName();
  283. certDetails = certDetails + "///"
  284. + dateFormat.format(cerificate.getNotBefore()) + "///"
  285. + dateFormat.format(cerificate.getNotAfter());
  286. certDetails = certDetails + "///" + cerificate.getSigAlgName()
  287. + "///" + cerificate.getSerialNumber();
  288.  
  289. String[] keyUsagesData = { "digitalSignature", "nonRepudiation",
  290. "keyEncipherment", "dataEncipherment", "keyAgreement",
  291. "keyCertSign", "cRLSign", "encipherOnly", "decipherOnly" };
  292. boolean[] keyUsages = cerificate.getKeyUsage();
  293.  
  294. if (keyUsages != null && keyUsages.length == 9) {
  295. String tmp = "";
  296. for (int i = 0; i < keyUsages.length; i++) {
  297. tmp = tmp + keyUsagesData[i] + "=" + keyUsages[i] + ",";
  298. }
  299. certDetails = certDetails + "///"
  300. + tmp.substring(0, tmp.length() - 1);
  301. } else {
  302. certDetails = certDetails + "///" + "null";
  303. }
  304. printMessageToConsole("Certificate Details : " + certDetails);
  305.  
  306. } catch (Exception e) {
  307. printMessageToConsole("Exception in Get Browser Certificate Details");
  308. e.printStackTrace();
  309. certDetails = e.getMessage();
  310. }
  311.  
  312. return certDetails;
  313. }
  314.  
  315. /**
  316. * This method is used to get all Certificates details of IE.
  317. * Certificate details will be separated by && and, two certificates will be separated by ||.
  318. * @return
  319. * @author janki.panwala
  320. */
  321. public String getAllCertificateDetail(){
  322. String certificatesString = "";
  323. try {
  324. printMessageToConsole("In method: getAllCertificateDetail()");
  325. KeyStore browserKeyStore = initializeBrowserKeyStore();
  326. if(browserKeyStore != null) {
  327. Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
  328. spiField.setAccessible(true);
  329. KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
  330. Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
  331. entriesField.setAccessible(true);
  332. Collection entries = (Collection) entriesField.get(spi);
  333. for (Object entry : entries) {
  334. //PrivateKey privateKey = (PrivateKey) invokeGetter(certificate, "getPrivateKey");
  335. String aliasnew = (String) invokeGetter(entry, "getAlias");
  336. X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
  337. for (X509Certificate certificate : certificateChain) {
  338. if(!getkeyUsage(certificate.getKeyUsage()).equals("")) {
  339. if(certificatesString != null && certificatesString.trim().length() > 0) {
  340. certificatesString += "||" + getCertificateDetailString(certificate);
  341. }
  342. else{
  343. certificatesString += getCertificateDetailString(certificate);
  344. }
  345. }
  346. }
  347. }
  348. }
  349. else{
  350. printMessageToConsole("Error in Method: getAllCertificateDetail()-> Browser key store is null");
  351. }
  352. } catch (Exception e) {
  353. printMessageToConsole("Error in Method: getAllCertificateDetail()");
  354. }
  355. return certificatesString;
  356. }
  357.  
  358. private String getCertificateDetailString( X509Certificate certificate) {
  359. String certificateDetails = "";
  360. String digitalSign = "";
  361. try {
  362. String publicKey = this.bASE64Encoder.encode(certificate.getPublicKey().getEncoded());
  363. String certDetails = getX509CertificateDetails(certificate);
  364. int oCount = 0, cnCount = 0, ouCount = 0, cCount = 0;
  365. String[] certs = certDetails.split(",");
  366. if (certs.length > 0) {
  367. for (int i = 0; i < certs.length; i++) {
  368. String[] keyValue = certs[i].split("=");
  369. String certiField = "";
  370. if (keyValue.length == 2) {
  371. if (keyValue[0].trim().equals("CN") && cnCount == 0) {
  372. certDetails = CERT_DETAILS.NAME;
  373. cnCount++;
  374. } else if (keyValue[0].trim().equals("CN") && cnCount == 1) {
  375. certDetails = CERT_DETAILS.ISSUER_CNAME;
  376. cnCount++;
  377. } else if (keyValue[0].trim().equals("E-Mail Address")) {
  378. certDetails = CERT_DETAILS.EMAIL;
  379. } else if (keyValue[0].trim().equals("L")) {
  380. certDetails = CERT_DETAILS.CITY;
  381. } else if (keyValue[0].trim().equals("ST")) {
  382. certDetails = CERT_DETAILS.STATE;
  383. } else if (keyValue[0].trim().equals("C") && cCount == 0) {
  384. certDetails = CERT_DETAILS.COUNTRY;
  385. cCount++;
  386. } else if (keyValue[0].trim().equals("C") && cCount == 1) {
  387. certDetails = CERT_DETAILS.ISSUER_COUNTRY;
  388. } else if (keyValue[0].trim().equals("O") && oCount == 0) {
  389. certDetails = CERT_DETAILS.ORG_NAME;
  390. oCount++;
  391. } else if (keyValue[0].trim().equals("O") && oCount == 1) {
  392. certDetails = CERT_DETAILS.ISSUER_ORG_NAME;
  393. oCount++;
  394. } else if (keyValue[0].trim().equals("OU") && ouCount == 0) {
  395. certDetails = CERT_DETAILS.ORG_UNIT;
  396. ouCount++;
  397. } else if (keyValue[0].trim().equals("OU") && ouCount == 1) {
  398. certDetails = CERT_DETAILS.ISSUER_ORG_UNIT;
  399. ouCount++;
  400. } else if (keyValue[0].trim().equals("keyEncipherment")) {
  401. digitalSign = keyValue[1];
  402. }
  403.  
  404. if(certDetails != "" && !keyValue[0].trim().equals("keyEncipherment")) {
  405. certDetails += "="+keyValue[1];
  406. }
  407.  
  408. } else if (keyValue.length == 3) {
  409. if (keyValue[0].trim().equals("C")) {
  410. String[] subKeyValue = keyValue[1].split("///");
  411. if (subKeyValue.length > 2) {
  412. certDetails += CERT_DETAILS.EXPIRY_DATE +"="+subKeyValue[2];
  413. } else {
  414. certDetails += CERT_DETAILS.COUNTRY +"="+subKeyValue[0];
  415. }
  416. }
  417. if (keyValue[2].equals("true") || keyValue[2].equals("false")) {
  418. // valueMap.put(CERT_DETAILS.DIGITAL_SIGN, keyValue[2]);
  419. } else {
  420. certDetails = CERT_DETAILS.CN_NAME +"="+keyValue[2];
  421. }
  422. }
  423.  
  424. if(certiField != "") {
  425. if(certs.length > 0 && i != certs.length - 1) {
  426. certificateDetails += certiField + "&&";
  427. }
  428. else {
  429. certificateDetails += certiField + "&&";
  430. }
  431. }
  432. }
  433. }
  434.  
  435. /** Usage **/
  436. String usage = "";
  437. if (getkeyUsage(certificate.getKeyUsage()).equalsIgnoreCase("Non-Repudiation")) {
  438. usage = "Signing";
  439. } else if (getkeyUsage(certificate.getKeyUsage()).equalsIgnoreCase( "KeyEncipherment")) {
  440. usage = "Encryption";
  441. }
  442. else {
  443. usage = digitalSign;
  444. }
  445.  
  446. if(certificateDetails.endsWith("&&")) {
  447. certificateDetails += CERT_DETAILS.DIGITAL_SIGN + "="+usage;
  448. }
  449. else {
  450. certificateDetails += "&&"+CERT_DETAILS.DIGITAL_SIGN + "="+usage;
  451. }
  452.  
  453. /** Set Public key **/
  454. if(certificateDetails.endsWith("&&")) {
  455. certificateDetails += CERT_DETAILS.PUBLIC_KEY + "="+publicKey;
  456. }
  457. else {
  458. certificateDetails += "&&"+CERT_DETAILS.PUBLIC_KEY + "="+publicKey;
  459. }
  460.  
  461. printMessageToConsole("Certificate String: " + certificateDetails);
  462. } catch (Exception e) {
  463. printMessageToConsole("Error in Method: getCertificateDetailString()");
  464. }
  465. return certificateDetails;
  466. }
  467.  
  468. /**
  469. * @author Hardik Mishra
  470. * @param alias
  471. * @return
  472. */
  473. public String getCertificateDetail(String type) {
  474. try {
  475. String alias = "";
  476. String pubKey = "";
  477. String certDetails = "";
  478. KeyStore browserKeyStore = null;
  479. printMessageToConsole("INSIDE METHOD OF getCertificateDetail");
  480. List<String> resultValues = new ArrayList<String>();
  481.  
  482. X509Certificate cerificate = null;
  483. PrivateKey privatekey = null;
  484. browserKeyStore = initializeBrowserKeyStore();
  485. String aliasnew = null;
  486.  
  487. {
  488. printMessageToConsole("Internet Explorer");
  489. if (browserKeyStore != null) {
  490. try {
  491. printMessageToConsole("INSIDE IE CERTIFICATE READING");
  492. Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
  493. spiField.setAccessible(true);
  494. KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
  495. Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
  496. entriesField.setAccessible(true);
  497. @SuppressWarnings("rawtypes")
  498. Collection entries = (Collection) entriesField.get(spi);
  499. for (Object entry : entries) {
  500. aliasnew = (String) invokeGetter(entry, "getAlias");
  501. PrivateKey privateKey = (PrivateKey) invokeGetter(entry, "getPrivateKey");
  502. X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
  503. for (X509Certificate current : certificateChain) {
  504. if(!getkeyUsage(current.getKeyUsage()).equals("")) {
  505. pubKey = this.bASE64Encoder.encode(current.getPublicKey().getEncoded());
  506. certDetails = getX509CertificateDetails(current);
  507. Map<String, String> valueMap = new HashMap<String, String>();
  508. if (certDetails != null) {
  509. valueMap = getMetadata(certDetails);
  510. if (type.equalsIgnoreCase("Name")) {
  511. resultValues.add(valueMap.get(CERT_DETAILS.NAME)+ "(" + aliasnew + ")");
  512. } else if (type
  513. .equalsIgnoreCase("Email")) {
  514. resultValues.add(valueMap
  515. .get(CERT_DETAILS.EMAIL)
  516. + "(" + aliasnew + ")");
  517. } else if (type
  518. .equalsIgnoreCase("City")) {
  519. resultValues.add(valueMap
  520. .get(CERT_DETAILS.CITY)
  521. + "(" + aliasnew + ")");
  522. } else if (type
  523. .equalsIgnoreCase("State")) {
  524. resultValues.add(valueMap
  525. .get(CERT_DETAILS.STATE)
  526. + "(" + aliasnew + ")");
  527. } else if (type
  528. .equalsIgnoreCase("Country")) {
  529. resultValues.add(valueMap
  530. .get(CERT_DETAILS.COUNTRY)
  531. + "(" + aliasnew + ")");
  532. } else if (type
  533. .equalsIgnoreCase("Expiry Date")) {
  534. resultValues.add(valueMap
  535. .get(CERT_DETAILS.EXPIRY_DATE)
  536. + "(" + aliasnew + ")");
  537. } else if (type
  538. .equalsIgnoreCase("Organization")) {
  539. resultValues.add(valueMap
  540. .get(CERT_DETAILS.ISSUER_ORG_NAME)
  541. + "(" + aliasnew + ")");
  542. } else if (type
  543. .equalsIgnoreCase("Organization Unit")) {
  544. resultValues.add(valueMap
  545. .get(CERT_DETAILS.ISSUER_ORG_UNIT)
  546. + "(" + aliasnew + ")");
  547. } else if (type.equalsIgnoreCase("Digital Signature")) {
  548. if (getkeyUsage(current.getKeyUsage()).equalsIgnoreCase("Non-Repudiation")) {
  549. resultValues.add("false");
  550. } else if (getkeyUsage(current.getKeyUsage()).equalsIgnoreCase( "KeyEncipherment")) {
  551. resultValues.add("true");
  552. }
  553. else {
  554. resultValues.add(valueMap.get(CERT_DETAILS.DIGITAL_SIGN));
  555. }
  556. } else if (type.equalsIgnoreCase("PublicKey")) {
  557.  
  558. resultValues.add((pubKey).toString());
  559. } else if (type.equalsIgnoreCase("CNName")) {
  560. resultValues.add(valueMap.get(CERT_DETAILS.CN_NAME)+ "(" + aliasnew + ")");
  561. } else {
  562.  
  563. }
  564. } else {
  565. printMessageToConsole("CERTDETAILS IS NULL");
  566. }
  567.  
  568. if (privatekey==null){
  569. printMessageToConsole("PRIVATE KEY IS NULL");
  570. }
  571.  
  572. break;
  573. }
  574. }
  575. }
  576. } catch (Exception e) {
  577. e.printStackTrace();
  578. printMessageToConsole("Error occure during IE certificate Reading");
  579.  
  580. }
  581. } else {
  582. printMessageToConsole("Keystore is NULL");
  583. }
  584. String returnString = "";
  585. for (String value : resultValues) {
  586. returnString += value + "||";
  587. }
  588. return returnString;
  589. }
  590. } catch (Exception e) {
  591. e.printStackTrace();
  592. printMessageToConsole("Error occure during get CertificateeDetails");
  593.  
  594. }
  595. return "";
  596. }
  597. // Get KeyUsage value from certificate
  598. private String getkeyUsage(boolean[] keyval) {
  599. boolean[] arykeyUsage = keyval;
  600. if(arykeyUsage != null) {
  601. for (int i = 0; i < 9; i++) {
  602. printMessageToConsole("value of KeyUsage::" + arykeyUsage[i]);
  603. if (arykeyUsage[i] && i == 2)
  604. {
  605. return "KeyEncipherment";
  606. } else if (arykeyUsage[i] && (i == 1 || i == 0)) {
  607. return "Non-Repudiation";
  608. }
  609. }
  610. }
  611. return "";
  612. }
  613. // Encryption, Decryption and Signing Methods
  614.  
  615. public String Encrypt(String text, String pubkey) {
  616. printMessageToConsole("---------------------Inside Encrypt Method--------------------------------------");
  617. String encryptedText;
  618. PublicKey publicKey = null;
  619. Cipher cipher;
  620. try {
  621. publicKey = getPublicKeyFromString(pubkey);
  622. if (publicKey!=null) {
  623. if(text != null){
  624. byte[] plainText = text.getBytes();
  625. cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  626. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  627. encryptedText = bASE64Encoder.encode(cipher.doFinal(plainText));
  628. /*cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  629. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  630. byte[] encryptedData = Base64.encodeBase64(cipher.doFinal(text.getBytes()));
  631. encryptedText = new String(encryptedData);*/
  632. }
  633. else{
  634. encryptedText = "Error@Encrypt: Null data received";
  635. }
  636. }
  637. else{
  638. encryptedText = "Error@Encrypt: Public Key not found";
  639. }
  640. }
  641. e.printStackTrace();
  642. encryptedText = "Error@Encrypt: Algorithm Not Found Exception."+ e.getMessage();
  643. }
  644. catch (NoSuchPaddingException e) {
  645. e.printStackTrace();
  646. encryptedText = "Error@Encrypt: No Such Padding Exception."+ e.getMessage();
  647. }
  648. catch (InvalidKeyException e) {
  649. e.printStackTrace();
  650. encryptedText = "Error@Encrypt: Invalid Key Exception."+ e.getMessage();
  651. }
  652. catch (IllegalBlockSizeException e) {
  653. e.printStackTrace();
  654. encryptedText = "Error@Encrypt: Illegal Block Size Exception."+ e.getMessage();
  655. }
  656. catch (BadPaddingException e) {
  657. e.printStackTrace();
  658. encryptedText = "Error@Encrypt: Bad Padding Exception.."+ e.getMessage();
  659. }
  660. catch (Exception e) {
  661. e.printStackTrace();
  662. encryptedText = "Error@Encrypt: "+ e.getMessage();
  663. }
  664. printMessageToConsole("-------------------------------End Encrypt Method--------------------------------------");
  665. return encryptedText;
  666.  
  667. }
  668. public String Decrypt(String text, String pubkey) {
  669. printMessageToConsole("--------------------------------Inside Decrypt Method-----------------------------------");
  670. PrivateKey privatekey = null;
  671. KeyStore keyStoreBrowser = null;
  672. String decryptedString;
  673. Cipher cipher = null;
  674. byte[] encryptText;
  675. try {
  676. keyStoreBrowser = initializeBrowserKeyStore();
  677. if(keyStoreBrowser != null) {
  678. privatekey = getPrivateKeyFromKeyStore(pubkey, keyStoreBrowser);
  679. if(privatekey != null) {
  680. if(text != null){
  681. encryptText = this.bASE64Decoder.decodeBuffer(text);
  682. cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  683. cipher.init(Cipher.DECRYPT_MODE, privatekey);
  684. decryptedString = new String(cipher.doFinal(encryptText));
  685. /* encryptText = Base64.decodeBase64(text.getBytes());
  686. cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  687. cipher.init(Cipher.DECRYPT_MODE, privatekey);
  688. decryptedString = new String(cipher.doFinal(encryptText));*/
  689. }
  690. else{
  691. decryptedString = "Error@Decrypt: Null data received to decrypt.";
  692. }
  693. }
  694. else{
  695. printMessageToConsole("Private is null");
  696. decryptedString = "Error@Decrypt: Private Key Not Found.";
  697. }
  698. }
  699. else{
  700. printMessageToConsole("KeyStore not found");
  701. decryptedString = "Error@Decrypt: KeyStore is null.";
  702. }
  703. }
  704. /*catch (IOException e) {
  705. e.printStackTrace();
  706. decryptedString = "Error@Decrypt: IO Exception"+ e.getMessage();
  707. }*/
  708. e.printStackTrace();
  709. decryptedString = "Error@Decrypt: Algorithm Not Found Exception."+ e.getMessage();
  710. }
  711. catch (NoSuchPaddingException e) {
  712. e.printStackTrace();
  713. decryptedString = "Error@Decrypt: No Such Padding Exception."+ e.getMessage();
  714. }
  715. catch (InvalidKeyException e) {
  716. e.printStackTrace();
  717. decryptedString = "Error@Decrypt: Invalid Key Exception."+ e.getMessage();
  718. }
  719. catch (IllegalBlockSizeException e) {
  720. e.printStackTrace();
  721. decryptedString = "Error@Decrypt: Illegal Block Size Exception."+ e.getMessage();
  722. }
  723. catch (BadPaddingException e) {
  724. e.printStackTrace();
  725. decryptedString = "Error@Decrypt: Bad Padding Exception.."+ e.getMessage();
  726. }
  727. catch (Exception e) {
  728. e.printStackTrace();
  729. decryptedString = "Error@Decrypt:"+ e.getMessage();
  730. }
  731. printMessageToConsole("--------------------------------End Decrypt Method-----------------------------------");
  732. return decryptedString;
  733. }
  734. public String SignData(String data, String pubkey) {
  735. printMessageToConsole("--------------------------------Inside Sign Method-----------------------------------");
  736. PrivateKey privatekey = null;
  737. KeyStore keyStoreBrowser = null;
  738. Signature signer;
  739. String signedData;
  740. try {
  741. keyStoreBrowser = initializeBrowserKeyStore();
  742. if(keyStoreBrowser != null) {
  743. privatekey = getPrivateKeyFromKeyStore(pubkey, keyStoreBrowser);
  744. if(privatekey != null) {
  745. if(data != null) {
  746. signer = Signature.getInstance("SHA1withRSA");
  747. signer.initSign(privatekey);
  748. signer.update(data.getBytes());
  749. signedData = new String(signer.sign());
  750. }
  751. else{
  752. signedData = "Error@Sign: Null data received";
  753. }
  754. }
  755. else{
  756. signedData = "Error@Sign: Private not found";
  757. }
  758. }
  759. else{
  760. signedData = "Error@Sign: KeyStore is null";
  761. }
  762. e.printStackTrace();
  763. signedData = "Error@Sign: No Such Algorith Exception."+ e.getMessage();
  764. }
  765. catch (InvalidKeyException e) {
  766. e.printStackTrace();
  767. signedData = "Error@Sign: Invalid Key Exception."+ e.getMessage();
  768. }
  769. catch (SignatureException e) {
  770. e.printStackTrace();
  771. signedData = "Error@Sign: Signature Exception."+ e.getMessage();
  772. }
  773. catch(Exception e) {
  774. e.printStackTrace();
  775. signedData = "Error@Sign: "+ e.getMessage();
  776. }
  777. printMessageToConsole("--------------------------------End Sign Method-----------------------------------");
  778. return signedData;
  779. }
  780. /**
  781. * Dhruti verifyData with publickey,Plaintext and SignData
  782. */
  783. public String verifyData(String data, String publicKey) {
  784.  
  785. byte[] digitalSignature = (SignData(data, publicKey)).getBytes();
  786.  
  787. boolean result = true;
  788. Signature signer = null;
  789. try {
  790. signer = Signature.getInstance("SHA1withRSA");
  791. e.printStackTrace();
  792. return "Error@Verify: No Such Algorith Exception."+ e.getMessage();
  793. }
  794. PublicKey pubkey = getPublicKeyFromString(publicKey);
  795. if (publicKey==null)
  796. return "Error@Verify: Publick Key Null.";
  797. try {
  798. signer.initVerify(pubkey);
  799. } catch (InvalidKeyException e) {
  800. e.printStackTrace();
  801. return "Error@Verify: Invalid Key Exception."+ e.getMessage();
  802. }
  803.  
  804. try {
  805. signer.update(data.getBytes());
  806. } catch (SignatureException e) {
  807. e.printStackTrace();
  808. return "Error@Verify: Signature Exception."+ e.getMessage();
  809. }
  810.  
  811. try {
  812. result = signer.verify(digitalSignature);
  813. } catch (SignatureException e) {
  814. e.printStackTrace();
  815. return "Error@Verify: Signature Exception."+ e.getMessage();
  816.  
  817. }
  818.  
  819. if (result == true) {
  820. return "TRUE";
  821. } else {
  822. return "FALSE";
  823. }
  824. }
  825. public static PublicKey getPublicKeyFromString(String key) {
  826. BASE64Decoder base64Decoder = new BASE64Decoder();
  827. PublicKey publicKey = null;
  828. try {
  829. publicKey = new RSAPublicKeyImpl(base64Decoder.decodeBuffer(key));
  830. } catch (InvalidKeyException e) {
  831. e.printStackTrace();
  832. } catch (IOException e) {
  833. e.printStackTrace();
  834. }
  835. return publicKey;
  836. }
  837. private Map<String, String> getMetadata(String certDetail) {
  838. // /CN=Test Organization Signing Certificate, ST=Test State,
  839. // OID.2.5.4.17=500050, OU=Test Department, O=Test Company Organization,
  840. // C=IN
  841. // /CN=Test TCS Sub-CA for TCS 2011, OU=Sub-CA, O=Tata Consultancy
  842. // Services Ltd., C=IN
  843.  
  844.  
  845. //Output
  846. // E-Mail Address= kuntal.shah@digi-corp.com,
  847. // CN=KUNTAL NITINBHAI SHAH, ST=GUJARAT, OID.2.5.4.17=380015, OU=DIRECTOR,
  848. // O=DIGICORP INFORMATION SYSTEMS PVT LTD, C=IN
  849. ///CN=SafeScrypt sub-CA for RCAI Class 3 2012, OU=Sub-CA, O=Sify Technologies Limited, C=IN
  850. ///17:30:46 PM 20-02-2012
  851. ///17:30:46 PM 20-02-2014
  852. ///SHA256withRSA
  853. ///94715422213
  854. ///digitalSignature=true,nonRepudiation=true,keyEncipherment=false,dataEncipherment=false,keyAgreement=false,keyCertSign=false,cRLSign=false,encipherOnly=false,decipherOnly=false
  855. int oCount = 0, cnCount = 0, ouCount = 0, cCount = 0;
  856. printMessageToConsole(" Create Value Map For : " + certDetail);
  857. Map<String, String> valueMap = new HashMap<String, String>();
  858. String[] certs = certDetail.split(",");
  859. if (certs.length > 0) {
  860. for (int i = 0; i < certs.length; i++) {
  861. String[] keyValue = certs[i].split("=");
  862. if (keyValue.length == 2) {
  863. if (keyValue[0].trim().equals("CN") && cnCount == 0) {
  864. valueMap.put(CERT_DETAILS.NAME, keyValue[1]);
  865. cnCount++;
  866. } else if (keyValue[0].trim().equals("CN") && cnCount == 1) {
  867. valueMap.put(CERT_DETAILS.ISSUER_CNAME, keyValue[1]);
  868. cnCount++;
  869. } else if (keyValue[0].trim().equals("E-Mail Address")) {
  870. valueMap.put(CERT_DETAILS.EMAIL, keyValue[1]);
  871. } else if (keyValue[0].trim().equals("L")) {
  872. valueMap.put(CERT_DETAILS.CITY, keyValue[1]);
  873. } else if (keyValue[0].trim().equals("ST")) {
  874. valueMap.put(CERT_DETAILS.STATE, keyValue[1]);
  875. } else if (keyValue[0].trim().equals("C") && cCount == 0) {
  876. valueMap.put(CERT_DETAILS.COUNTRY, keyValue[1]);
  877. cCount++;
  878. } else if (keyValue[0].trim().equals("C") && cCount == 1) {
  879. valueMap.put(CERT_DETAILS.ISSUER_COUNTRY, keyValue[1]);
  880. } else if (keyValue[0].trim().equals("O") && oCount == 0) {
  881. valueMap.put(CERT_DETAILS.ORG_NAME, keyValue[1]);
  882. oCount++;
  883. } else if (keyValue[0].trim().equals("O") && oCount == 1) {
  884. valueMap.put(CERT_DETAILS.ISSUER_ORG_NAME, keyValue[1]);
  885. oCount++;
  886. } else if (keyValue[0].trim().equals("OU") && ouCount == 0) {
  887. valueMap.put(CERT_DETAILS.ORG_UNIT, keyValue[1]);
  888. ouCount++;
  889. } else if (keyValue[0].trim().equals("OU") && ouCount == 1) {
  890. valueMap.put(CERT_DETAILS.ISSUER_ORG_UNIT, keyValue[1]);
  891. ouCount++;
  892. } else if (keyValue[0].trim().equals("keyEncipherment")) {
  893. valueMap.put(CERT_DETAILS.DIGITAL_SIGN, keyValue[1]);
  894. }
  895.  
  896. } else if (keyValue.length == 3) {
  897. if (keyValue[0].trim().equals("C")) {
  898. String[] subKeyValue = keyValue[1].split("///");
  899. if (subKeyValue.length > 2) {
  900. valueMap.put(CERT_DETAILS.EXPIRY_DATE,
  901. subKeyValue[2]);
  902. } else {
  903. valueMap.put(CERT_DETAILS.COUNTRY, subKeyValue[0]);
  904. }
  905. }
  906. if (keyValue[2].equals("true")
  907. || keyValue[2].equals("false")) {
  908. // valueMap.put(CERT_DETAILS.DIGITAL_SIGN, keyValue[2]);
  909. } else {
  910. valueMap.put(CERT_DETAILS.CN_NAME, keyValue[2]);
  911. }
  912. }
  913. }
  914. }
  915. return valueMap;
  916. }
  917. public static String symmetricEncrypt(String text, String secretKey){
  918. System.out.println("--------------------------------Inside symmetric encrypt Method-----------------------------------");
  919. BASE64Decoder decoder = new BASE64Decoder();
  920. byte[] raw;
  921. String encryptedString;
  922. SecretKeySpec skeySpec ;
  923. Cipher cipher;
  924. BASE64Encoder bASE64Encoder = new BASE64Encoder();
  925. try {
  926. if(text != null) {
  927. /* raw = Base64.decodeBase64(secretKey.getBytes());
  928. skeySpec = new SecretKeySpec(raw, "AES");
  929. cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  930. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  931. byte[] encryptedData = Base64.encodeBase64(cipher.doFinal(text.getBytes()));
  932. encryptedString = new String(encryptedData);
  933. */
  934. byte[] encryptText = text.getBytes();
  935. raw = decoder.decodeBuffer(secretKey);
  936. skeySpec = new SecretKeySpec(raw, "AES");
  937. cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  938. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  939. encryptedString = bASE64Encoder.encode(cipher.doFinal(encryptText));
  940. }
  941. else{
  942. encryptedString = "Error@SymmetricEncrypt: Null data Received";
  943. }
  944. }
  945. /*catch (IOException e) {
  946. e.printStackTrace();
  947. encryptedString = "Error@SymmetricEncrypt: IO Exception " + e.getMessage();
  948. }*/
  949. e.printStackTrace();
  950. encryptedString = "Error@SymmetricEncrypt: No Such Algorithm Exception " + e.getMessage();
  951. } catch (NoSuchPaddingException e) {
  952. e.printStackTrace();
  953. encryptedString = "Error@SymmetricEncrypt: No Such Padding Exception " + e.getMessage();
  954. }
  955. catch (InvalidKeyException e) {
  956. e.printStackTrace();
  957. encryptedString = "Error@SymmetricEncrypt: Invalid Key Exception " + e.getMessage();
  958. }
  959. catch (IllegalBlockSizeException e) {
  960. e.printStackTrace();
  961. encryptedString = "Error@SymmetricEncrypt: IO Exception " + e.getMessage();
  962. }
  963. catch (BadPaddingException e) {
  964. e.printStackTrace();
  965. encryptedString = "Error@SymmetricEncrypt: IO Exception " + e.getMessage();
  966. }
  967. catch (Exception e) {
  968. System.out.println(e.getCause().toString());
  969. encryptedString = "Error@SymmetricEncrypt: " + e.getMessage();
  970. }
  971. System.out.println("--------------------------------End symmetric encrypt Method-----------------------------------");
  972. return encryptedString;
  973. }
  974. public static String symmetricDecrypt(String text, String secretKey){
  975. System.out.println("--------------------------------Inside symmetric decrypt Method-----------------------------------");
  976. BASE64Decoder decoder = new BASE64Decoder();
  977. BASE64Decoder base64Decoder = new BASE64Decoder();
  978. Cipher cipher;
  979. String decryptedString;
  980. byte[] encryptText;
  981. byte[] raw;
  982. SecretKeySpec skeySpec;
  983. try {
  984. if(text != null) {
  985. /*raw = Base64.decodeBase64(secretKey.getBytes());
  986. skeySpec = new SecretKeySpec(raw, "AES");
  987. encryptText = Base64.decodeBase64(text.getBytes());
  988. cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  989. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  990. decryptedString = new String(cipher.doFinal(encryptText));*/
  991. raw = decoder.decodeBuffer(secretKey);
  992. skeySpec = new SecretKeySpec(raw, "AES");
  993. encryptText = base64Decoder.decodeBuffer(text);
  994. cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  995. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  996. decryptedString = new String(cipher.doFinal(encryptText));
  997. }
  998. else {
  999. decryptedString = "Error@SymmetricDecrypt: Null data received to decrypy.";
  1000. }
  1001. }
  1002. /*catch (IOException e) {
  1003. e.printStackTrace();
  1004. decryptedString = "Error@SymmetricDecrypt: IO Exception " + e.getMessage();
  1005. }*/
  1006. e.printStackTrace();
  1007. decryptedString = "Error@SymmetricDecrypt: No Such Algorithm Exception " + e.getMessage();
  1008. }
  1009. catch (NoSuchPaddingException e) {
  1010. e.printStackTrace();
  1011. decryptedString = "Error@SymmetricDecrypt: No Such Padding Exception " + e.getMessage();
  1012. }
  1013. catch (InvalidKeyException e) {
  1014. e.printStackTrace();
  1015. decryptedString = "Error@SymmetricDecrypt: Invalid Key Exception " + e.getMessage();
  1016. }
  1017. catch (IllegalBlockSizeException e) {
  1018. e.printStackTrace();
  1019. decryptedString = "Error@SymmetricDecrypt: Illegal Block Size Exception " + e.getMessage();
  1020. }
  1021. catch (BadPaddingException e) {
  1022. e.printStackTrace();
  1023. decryptedString = "Error@SymmetricDecrypt: Bad Padding Exception " + e.getMessage();
  1024. }
  1025. catch(Exception e){
  1026. e.printStackTrace();
  1027. decryptedString = "Error@SymmetricDecrypt:" + e.getMessage();
  1028. }
  1029. return decryptedString;
  1030. }
  1031. public static String getSecretKey() {
  1032. KeyGenerator kgen;
  1033. String strSecretkey;
  1034. try {
  1035. kgen = KeyGenerator.getInstance("AES");
  1036. kgen.init(128);
  1037. SecretKey secretkey = kgen.generateKey();
  1038. BASE64Encoder encode = new BASE64Encoder();
  1039. strSecretkey = encode.encode(secretkey.getEncoded());
  1040. }
  1041. e.printStackTrace();
  1042. strSecretkey = "Error@SecretKeyGeneration: No Such Algorith Exception. " + e.getMessage();
  1043. }
  1044. catch (Exception e) {
  1045. e.printStackTrace();
  1046. strSecretkey = "Error@SecretKeyGeneration: " + e.getMessage();
  1047. }
  1048. return strSecretkey;
  1049. }
  1050. // Common /\- Misc Methods
  1051. public void printMessageToConsole(String message) {
  1052. System.out.println(message);
  1053. }
  1054. private static Object invokeGetter(Object instance, String methodName)
  1055. Method getAlias = instance.getClass().getDeclaredMethod(methodName);
  1056. getAlias.setAccessible(true);
  1057. return getAlias.invoke(instance);
  1058. }
  1059. public String getJavaVerify() {
  1060. String result="";
  1061. Object object5 = jSObject.call("getJavaVerify", null);
  1062.  
  1063. if (object5 != null && object5 instanceof String) {
  1064. result = (String) object5;
  1065. printMessageToConsole("GOT Java Verify============" + result);
  1066. if (result == "true" || result.equalsIgnoreCase("true")) {
  1067. return "true";
  1068. } else {
  1069. return "false";
  1070. }
  1071.  
  1072. } else {
  1073. return "false";
  1074. }
  1075. }
  1076. public static String generateKeyPair() {
  1077. //String[] keys = new String[(int) (2)];
  1078. String strKeys = "";
  1079. try {
  1080. keyGen = KeyPairGenerator.getInstance("RSA");
  1081. keyGen.initialize(2048);
  1082. java.security.KeyPair pair = keyGen.generateKeyPair();
  1083. PrivateKey privateKey = pair.getPrivate();
  1084. PublicKey publicKey = pair.getPublic();
  1085. byte[] publicEncoded = publicKey.getEncoded();
  1086. byte[] privateEncoded = privateKey.getEncoded();
  1087. BASE64Encoder base64Encoder = new BASE64Encoder();
  1088. strKeys = base64Encoder.encode(publicEncoded);
  1089. //keys[0] = strKey;
  1090. strKeys +=","+ base64Encoder.encode(privateEncoded);
  1091. //keys[1] = strKey;
  1092. e.printStackTrace();
  1093. strKeys = "Error@GenerateKeys: No algorithm found." + e.getMessage();
  1094. //keys[0] = "Error@GenerateKeys: No algorithm found." + e.getMessage();
  1095. } catch (Exception e) {
  1096. e.printStackTrace();
  1097. strKeys = "Error@GenerateKeys: Problem in generating keys." + e.getMessage();
  1098. //keys[0] = "Error@GenerateKeys: Problem in generating keys." + e.getMessage();
  1099. }
  1100. return strKeys;
  1101. }
  1102. public static String publicEncrypt(String text, String pubKey){
  1103. String encryptedText = null;
  1104. try {
  1105. PublicKey publicKey = getPublicKeyFromString(pubKey);
  1106. BASE64Encoder bASE64Encoder = new BASE64Encoder();
  1107. byte[] plainText = text.getBytes();
  1108. Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  1109. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  1110. cipher.update(plainText);
  1111. encryptedText = bASE64Encoder.encode(cipher.doFinal());
  1112. } catch (Exception e) {
  1113. e.printStackTrace();
  1114. encryptedText = "Error@PublicEncrypt" + e.getMessage();
  1115. }
  1116. return encryptedText;
  1117. }
  1118. public static String privateDecrypt(String text, String priKey){
  1119. String decryptedString = null;
  1120. try {
  1121. PrivateKey privateKey = getPrivateKeyFromString(priKey);
  1122. BASE64Decoder base64Decoder = new BASE64Decoder();
  1123. byte[] encryptText = base64Decoder.decodeBuffer(text);
  1124. Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  1125. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  1126. decryptedString = new String(cipher.doFinal(encryptText));
  1127. } catch (Exception e) {
  1128. e.printStackTrace();
  1129. decryptedString = "Error@PrivateDecrypt." + e.getMessage();
  1130. }
  1131. return decryptedString;
  1132. }
  1133. public static PrivateKey getPrivateKeyFromString(String key) throws Exception{
  1134. BASE64Decoder base64Decoder = new BASE64Decoder();
  1135. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  1136. EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(base64Decoder.decodeBuffer(key));
  1137. PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
  1138. return privateKey;
  1139. }
  1140. public String getCertificateAllDetailsForLogin() throws NoSuchFieldException,
  1141. String certString = "";
  1142. int count = 0;
  1143. String pubKey = "";
  1144. KeyStore browserKeyStore = null;
  1145. String certDetails = "";
  1146. browserKeyStore = initializeBrowserKeyStore();
  1147. String resultValues ="";
  1148. String aliasnew = null;
  1149.  
  1150. printMessageToConsole(browserName);
  1151. if (browserKeyStore != null) {
  1152. Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
  1153. spiField.setAccessible(true);
  1154. KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
  1155. Field entriesField = spi.getClass().getSuperclass()
  1156. .getDeclaredField("entries");
  1157. entriesField.setAccessible(true);
  1158. @SuppressWarnings("rawtypes")
  1159. Collection entries = (Collection) entriesField.get(spi);
  1160. for (Object entry : entries) {
  1161. aliasnew = (String) invokeGetter(entry, "getAlias");
  1162. PrivateKey privateKey = (PrivateKey) invokeGetter(entry,
  1163. "getPrivateKey");
  1164. X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(
  1165. entry, "getCertificateChain");
  1166. for (X509Certificate current : certificateChain) {
  1167. /*if (certDetails != null
  1168. && getkeyUsage(current.getKeyUsage()) != "") */
  1169. {
  1170. count++;
  1171. pubKey = this.bASE64Encoder.encode(current
  1172. .getPublicKey().getEncoded());
  1173. pubKey = pubKey.replaceAll("\\s","#");
  1174. certDetails = getX509CertificateDetails(current);
  1175. Map<String, String> valueMap = new HashMap<String, String>();
  1176. valueMap = getMetadata(certDetails);
  1177. certString += "Alias $$ " + aliasnew + "||";
  1178. certString += "Name : "
  1179. + valueMap.get(CERT_DETAILS.NAME) + "||";
  1180. certString += "Email $$ "
  1181. + valueMap.get(CERT_DETAILS.EMAIL) + "||";
  1182. certString += "City $$ "
  1183. + valueMap.get(CERT_DETAILS.CITY) + "||";
  1184. certString += "State $$ "
  1185. + valueMap.get(CERT_DETAILS.STATE) + "||";
  1186. certString += "Country $$ "
  1187. + valueMap.get(CERT_DETAILS.COUNTRY) + "||";
  1188. certString += "Expiry Date $$ "
  1189. + valueMap.get(CERT_DETAILS.EXPIRY_DATE)
  1190. + "||";
  1191. certString += "Issuer Organization $$ "
  1192. + valueMap.get(CERT_DETAILS.ISSUER_ORG_NAME)
  1193. + "||";
  1194. certString += "Issuer Organization Unit $$ "
  1195. + valueMap.get(CERT_DETAILS.ISSUER_ORG_UNIT)
  1196. + "||";
  1197. certString += "Key Usage $$ "
  1198. + getkeyUsage(current.getKeyUsage()) + "||";
  1199. certString += "CNName $$"
  1200. + valueMap.get(CERT_DETAILS.CN_NAME) + "||";
  1201. certString += "Public Key $$"+"^^ "
  1202. + pubKey + "||";
  1203. certString += "===";
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. } else {
  1209. printMessageToConsole("Keystore is NULL");
  1210. }
  1211. return certString;
  1212. }
  1213.  
  1214.  
  1215. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:2: error: class SecurityApplet is public, should be declared in a file named SecurityApplet.java
public class SecurityApplet extends Applet {
       ^
Main.java:2: error: cannot find symbol
public class SecurityApplet extends Applet {
                                    ^
  symbol: class Applet
Main.java:5: error: cannot find symbol
	private JSObject jSObject = null;
	        ^
  symbol:   class JSObject
  location: class SecurityApplet
Main.java:6: error: cannot find symbol
	private BASE64Encoder bASE64Encoder = new BASE64Encoder();
	        ^
  symbol:   class BASE64Encoder
  location: class SecurityApplet
Main.java:7: error: cannot find symbol
	private BASE64Decoder bASE64Decoder = new BASE64Decoder();
	        ^
  symbol:   class BASE64Decoder
  location: class SecurityApplet
Main.java:99: error: cannot find symbol
	private KeyStore initializeBrowserKeyStore() {
	        ^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:112: error: cannot find symbol
	public List<String> getCertificateAllDetails() throws NoSuchFieldException,
	       ^
  symbol:   class List
  location: class SecurityApplet
Main.java:115: error: cannot find symbol
			InvocationTargetException {
			^
  symbol:   class InvocationTargetException
  location: class SecurityApplet
Main.java:229: error: cannot find symbol
	private PrivateKey getPrivateKeyFromKeyStore(String pubkey,KeyStore browser) {
	                                                           ^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:229: error: cannot find symbol
	private PrivateKey getPrivateKeyFromKeyStore(String pubkey,KeyStore browser) {
	        ^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:269: error: cannot find symbol
	private  String getX509CertificateDetails(X509Certificate cerificate) {
	                                          ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:361: error: cannot find symbol
	private String getCertificateDetailString( X509Certificate certificate) {
	                                           ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:832: error: cannot find symbol
	public static PublicKey getPublicKeyFromString(String key) {
	              ^
  symbol:   class PublicKey
  location: class SecurityApplet
Main.java:844: error: cannot find symbol
	private Map<String, String> getMetadata(String certDetail) {
	        ^
  symbol:   class Map
  location: class SecurityApplet
Main.java:1066: error: cannot find symbol
			InvocationTargetException {
			^
  symbol:   class InvocationTargetException
  location: class SecurityApplet
Main.java:1147: error: cannot find symbol
		public static PrivateKey getPrivateKeyFromString(String key) throws Exception{
		              ^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:1157: error: cannot find symbol
				InvocationTargetException {
				^
  symbol:   class InvocationTargetException
  location: class SecurityApplet
Main.java:6: error: cannot find symbol
	private BASE64Encoder bASE64Encoder = new BASE64Encoder();
	                                          ^
  symbol:   class BASE64Encoder
  location: class SecurityApplet
Main.java:7: error: cannot find symbol
	private BASE64Decoder bASE64Decoder = new BASE64Decoder();
	                                          ^
  symbol:   class BASE64Decoder
  location: class SecurityApplet
Main.java:52: error: method does not override or implement a method from a supertype
	@Override
	^
Main.java:60: error: cannot find symbol
		File policyFile = new File(System.getProperty("user.home") + File.separator	+ ".java.policy");
		^
  symbol:   class File
  location: class SecurityApplet
Main.java:60: error: cannot find symbol
		File policyFile = new File(System.getProperty("user.home") + File.separator	+ ".java.policy");
		                      ^
  symbol:   class File
  location: class SecurityApplet
Main.java:60: error: cannot find symbol
		File policyFile = new File(System.getProperty("user.home") + File.separator	+ ".java.policy");
		                                                             ^
  symbol:   variable File
  location: class SecurityApplet
Main.java:68: error: cannot find symbol
		FileOutputStream policyOutStream = new FileOutputStream(policyFile);
		^
  symbol:   class FileOutputStream
  location: class SecurityApplet
Main.java:68: error: cannot find symbol
		FileOutputStream policyOutStream = new FileOutputStream(policyFile);
		                                       ^
  symbol:   class FileOutputStream
  location: class SecurityApplet
Main.java:100: error: cannot find symbol
		KeyStore keyStore = null;
		^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:103: error: cannot find symbol
			keyStore = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
			           ^
  symbol:   variable KeyStore
  location: class SecurityApplet
Main.java:120: error: cannot find symbol
		KeyStore browserKeyStore = null;
		^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:123: error: cannot find symbol
		List<String> resultValues = new ArrayList<String>();
		^
  symbol:   class List
  location: class SecurityApplet
Main.java:123: error: cannot find symbol
		List<String> resultValues = new ArrayList<String>();
		                                ^
  symbol:   class ArrayList
  location: class SecurityApplet
Main.java:129: error: cannot find symbol
			Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
			^
  symbol:   class Field
  location: class SecurityApplet
Main.java:129: error: cannot find symbol
			Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
			                 ^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:131: error: cannot find symbol
			KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
			^
  symbol:   class KeyStoreSpi
  location: class SecurityApplet
Main.java:131: error: cannot find symbol
			KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
			                   ^
  symbol:   class KeyStoreSpi
  location: class SecurityApplet
Main.java:132: error: cannot find symbol
			Field entriesField = spi.getClass().getSuperclass()
			^
  symbol:   class Field
  location: class SecurityApplet
Main.java:136: error: cannot find symbol
			Collection entries = (Collection) entriesField.get(spi);
			^
  symbol:   class Collection
  location: class SecurityApplet
Main.java:136: error: cannot find symbol
			Collection entries = (Collection) entriesField.get(spi);
			                      ^
  symbol:   class Collection
  location: class SecurityApplet
Main.java:143: error: cannot find symbol
				PrivateKey privateKey = (PrivateKey) invokeGetter(entry,
				^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:143: error: cannot find symbol
				PrivateKey privateKey = (PrivateKey) invokeGetter(entry,
				                         ^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:145: error: cannot find symbol
				X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(
				^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:145: error: cannot find symbol
				X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(
				                                      ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:147: error: cannot find symbol
				for (X509Certificate current : certificateChain) {
				     ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:156: error: cannot find symbol
						Map<String, String> valueMap = new HashMap<String, String>();
						^
  symbol:   class Map
  location: class SecurityApplet
Main.java:156: error: cannot find symbol
						Map<String, String> valueMap = new HashMap<String, String>();
						                                   ^
  symbol:   class HashMap
  location: class SecurityApplet
Main.java:203: error: cannot find symbol
		PrivateKey privatekey = null;
		^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:204: error: cannot find symbol
		KeyStore keyStoreBrowser = null;
		^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:230: error: cannot find symbol
		PrivateKey privateKey = null;
		^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:237: error: cannot find symbol
				Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
				^
  symbol:   class Field
  location: class SecurityApplet
Main.java:237: error: cannot find symbol
				Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
				                 ^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:239: error: cannot find symbol
				KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browser);
				^
  symbol:   class KeyStoreSpi
  location: class SecurityApplet
Main.java:239: error: cannot find symbol
				KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browser);
				                   ^
  symbol:   class KeyStoreSpi
  location: class SecurityApplet
Main.java:240: error: cannot find symbol
				Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
				^
  symbol:   class Field
  location: class SecurityApplet
Main.java:243: error: cannot find symbol
				Collection entries = (Collection) entriesField.get(spi);
				^
  symbol:   class Collection
  location: class SecurityApplet
Main.java:243: error: cannot find symbol
				Collection entries = (Collection) entriesField.get(spi);
				                      ^
  symbol:   class Collection
  location: class SecurityApplet
Main.java:246: error: cannot find symbol
					X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
					^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:246: error: cannot find symbol
					X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
					                                      ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:247: error: cannot find symbol
					for (X509Certificate current : certificateChain) {
					     ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:251: error: cannot find symbol
							privateKey = (PrivateKey) invokeGetter(entry,"getPrivateKey");
							              ^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:282: error: cannot find symbol
			DateFormat dateFormat = new SimpleDateFormat(
			^
  symbol:   class DateFormat
  location: class SecurityApplet
Main.java:282: error: cannot find symbol
			DateFormat dateFormat = new SimpleDateFormat(
			                            ^
  symbol:   class SimpleDateFormat
  location: class SecurityApplet
Main.java:328: error: cannot find symbol
			KeyStore browserKeyStore = initializeBrowserKeyStore();
			^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:330: error: cannot find symbol
				Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
				^
  symbol:   class Field
  location: class SecurityApplet
Main.java:330: error: cannot find symbol
				Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
				                 ^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:332: error: cannot find symbol
				KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
				^
  symbol:   class KeyStoreSpi
  location: class SecurityApplet
Main.java:332: error: cannot find symbol
				KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
				                   ^
  symbol:   class KeyStoreSpi
  location: class SecurityApplet
Main.java:333: error: cannot find symbol
				Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
				^
  symbol:   class Field
  location: class SecurityApplet
Main.java:335: error: cannot find symbol
				Collection entries = (Collection) entriesField.get(spi);
				^
  symbol:   class Collection
  location: class SecurityApplet
Main.java:335: error: cannot find symbol
				Collection entries = (Collection) entriesField.get(spi);
				                      ^
  symbol:   class Collection
  location: class SecurityApplet
Main.java:339: error: cannot find symbol
					X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
					^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:339: error: cannot find symbol
					X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
					                                      ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:340: error: cannot find symbol
					for (X509Certificate certificate : certificateChain) {
					     ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:481: error: cannot find symbol
			KeyStore browserKeyStore = null;
			^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:483: error: cannot find symbol
			List<String> resultValues = new ArrayList<String>();
			^
  symbol:   class List
  location: class SecurityApplet
Main.java:483: error: cannot find symbol
			List<String> resultValues = new ArrayList<String>();
			                                ^
  symbol:   class ArrayList
  location: class SecurityApplet
Main.java:485: error: cannot find symbol
			X509Certificate cerificate = null;
			^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:486: error: cannot find symbol
			PrivateKey privatekey = null;
			^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:495: error: cannot find symbol
						Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
						^
  symbol:   class Field
  location: class SecurityApplet
Main.java:495: error: cannot find symbol
						Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
						                 ^
  symbol:   class KeyStore
  location: class SecurityApplet
Main.java:497: error: cannot find symbol
						KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
						^
  symbol:   class KeyStoreSpi
  location: class SecurityApplet
Main.java:497: error: cannot find symbol
						KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browserKeyStore);
						                   ^
  symbol:   class KeyStoreSpi
  location: class SecurityApplet
Main.java:498: error: cannot find symbol
						Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
						^
  symbol:   class Field
  location: class SecurityApplet
Main.java:501: error: cannot find symbol
						Collection entries = (Collection) entriesField.get(spi);
						^
  symbol:   class Collection
  location: class SecurityApplet
Main.java:501: error: cannot find symbol
						Collection entries = (Collection) entriesField.get(spi);
						                      ^
  symbol:   class Collection
  location: class SecurityApplet
Main.java:504: error: cannot find symbol
							PrivateKey privateKey = (PrivateKey) invokeGetter(entry, "getPrivateKey");
							^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:504: error: cannot find symbol
							PrivateKey privateKey = (PrivateKey) invokeGetter(entry, "getPrivateKey");
							                         ^
  symbol:   class PrivateKey
  location: class SecurityApplet
Main.java:505: error: cannot find symbol
							X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
							^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:505: error: cannot find symbol
							X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
							                                      ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:506: error: cannot find symbol
							for (X509Certificate current : certificateChain) {
							     ^
  symbol:   class X509Certificate
  location: class SecurityApplet
Main.java:510: error: cannot find symbol
									Map<String, String> valueMap = new HashMap<String, String>();
									^
  symbol:   class Map
  location: class SecurityApplet
Main.java:510: error: cannot find symbol
									Map<String, String> valueMap = new HashMap<String, String>();
									                                   ^
  symbol:   class HashMap
  location: class SecurityApplet
Main.java:621: error: cannot find symbol
		PublicKey publicKey = null;
		^
  symbol:   class PublicKey
  location: class SecurityApplet
Main.java:622: error: cannot find symbol
		Cipher cipher;
		^
  symbol:   class Cipher
  location: class SecurityApplet
Main.java:628: error: cannot find symbol
					cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
					         ^
  symbol:   variable Cipher
  location: class SecurityApplet
Main.java:629: error: cannot find symbol
					cipher.init(Cipher.ENCRYPT_MODE, publicKey);
					            ^
  symbol:   variable Cipher
  location: class SecurityApplet
Main.java:644: error: cannot find symbol
		catch (NoSuchAlgorithmException e) {
		       ^
  symbol:   class NoSuchAlgorithmException
  location: class SecurityApplet
Main.java:648: error: cannot find symbol
		catch (NoSuchPaddingException e) {
		       ^
  symbol:   class NoSuchPaddingException
  location: class SecurityApplet
Main.java:652: error: cannot find symbol
		catch (InvalidKeyException e) {
		       ^
  symbol:   class InvalidKeyException
  location: class SecurityApplet
Main.java:656: error: cannot find symbol
		catch (IllegalBlockSizeException e) {
		       ^
  symbol:   class IllegalBlockSizeException
  location: class SecurityApplet
Main.java:660: error: cannot find symbol
		catch (BadPaddingException e) {
		       ^
  symbol:   class BadPaddingException
  location: class SecurityApplet
Main.java:674: error: cannot find symbol
		PrivateKey privatekey = null;
		^
  symbol:   class PrivateKey
  location: class SecurityApplet
100 errors
stdout
Standard output is empty