{"id":143,"date":"2012-08-07T12:58:06","date_gmt":"2012-08-07T11:58:06","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=143"},"modified":"2014-01-14T11:15:42","modified_gmt":"2014-01-14T10:15:42","slug":"how-to-sign-a-java-code","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=143","title":{"rendered":"How to sign a Java code"},"content":{"rendered":"<p>Some commands in Java requires special permissions to run on client PC. This restrictions are applied especially when programming web applets. <!--more-->To allow the applet to use the special commands, the Java applet needs to be &#8220;signed&#8221;. This signature can be generated by a trusted vendor like Thawte or similar, but issuing such certificate cost significant amount of money.<\/p>\n<p>You can also generate the certificate yoursel using JDK tools called <em>keytool<\/em> and <em>jarsigner <\/em>but then the applet will tell users that the signature has not been verified. If this is not a roadblock for you, you can sign the code using the following code (create a *.bat file, copy the following code as its contents and modify the parameters according to your needs)<\/p>\n<pre lang=\"batch\" gutter=false>SET PWD=MySecretP@ssw0rd\r\nSET USER=John_Doe\r\nSET STOREPASS=St0reP@ssw0rd \r\nSET CERTFILE=SignatureStore\r\nSET JARCERTFILE=Applet.jar\r\nSET VALIDITY=3650\r\n\r\nSET COMPANY_NAME=IT Forever\r\nSET ORG_UNIT=Application Outsourcing\r\nSET ORG=Development Company\r\nSET LAND=Copenhagen\r\nSET STATE=Denmark\r\nSET COUNTRY=DK\r\n\r\nREM del %CERTFILE%\r\nkeytool -genkey -alias %USER% -keystore %CERTFILE% -keypass %PWD% -dname \"CN=%COMPANY_NAME%, OU=%ORG_UNIT%, O=%ORG%, L=%LAND%, ST=%STATE%, C=%COUNTRY%\" -storepass %STOREPASS% -validity %VALIDITY%\r\njarsigner -keystore %CERTFILE% -storepass %STOREPASS% -keypass %PWD% %JARCERTFILE% %USER%\r\nREM del %CERTFILE%<\/pre>\n<p>Or&#8230;if you have PKCS12 (.p12) certificate you can sign the code using the following batch (windows .bat file)<\/p>\n<pre lang=\"batch\" gutter=false>@SET KEYFILE=key.pem\r\n@SET CERTFILE=cert.pem\r\n@SET P12FILE=cert.p12\r\n@SET JARCERTFILE=Applet.jar\r\n@SET DAYS=365\r\n@SET PASSWORD=St0reP@ssw0rd\r\n@SET C=DK\r\n@SET ST=Denmark\r\n@SET L=Copenhagen\r\n@SET O=Development Company\r\n@SET OU=Application Outsourcing\r\n@SET CN=Application Outsourcing\r\n@SET EMAIL=john_doe@development_company.com\r\n@SET BASEPATH=c:\\OpenSSL-Win32\r\n@set OPENSSL_CONF=%BASEPATH%\\bin\\openssl.cfg\r\n\r\n@ECHO Generating PKCS12 certificate file automatically providing password\r\n@ECHO ...........................\r\n\r\n@%BASEPATH%\\bin\\openssl req -x509 -newkey rsa:2048 -passout pass:%PASSWORD% -keyout %BASEPATH%\\bin\\PEM\\%KEYFILE% -out %BASEPATH%\\bin\\PEM\\%CERTFILE% -days %DAYS% -subj \"\/C=%C%\/ST=%ST%\/L=%L%\/O=%O%\/OU=%OU%\/CN=%CN%\/emailAddress=%EMAIL%\"\r\n@%BASEPATH%\\bin\\openssl pkcs12 -export -in %BASEPATH%\\bin\\PEM\\%CERTFILE% -inkey %BASEPATH%\\bin\\PEM\\%KEYFILE% -out %P12FILE% -name \"%CN%\" -passin pass:%PASSWORD% -password pass:%PASSWORD%\r\n@ECHO ...........................\r\n@ECHO Your new certificate has been generated to %P12FILE%\r\n@ECHO ...........................\r\n\r\n@ECHO Clean up...\r\n@del %BASEPATH%\\bin\\PEM\\%CERTFILE%\r\n@del %BASEPATH%\\bin\\PEM\\%KEYFILE%\r\n@ECHO Done\r\n@ECHO ...........................\r\n\r\n@ECHO Signing JAR file %JARCERTFILE%\r\njarsigner -storetype pkcs12 -keystore %P12FILE% -storepass %PASSWORD% %JARCERTFILE% \"%CN%\"\r\n@ECHO Done<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Some commands in Java requires special permissions to run on client PC. This restrictions are applied especially when programming web applets.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9,10],"tags":[248,28,27,246,245,237,238,247],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-development","category-java","tag-certificate","tag-certificates","tag-code-signature","tag-jarsigner","tag-keytool","tag-openssl","tag-p12","tag-pkcs12"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-2j","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=143"}],"version-history":[{"count":3,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":889,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions\/889"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}