{"id":10719,"date":"2022-09-24T18:44:18","date_gmt":"2022-09-24T13:14:18","guid":{"rendered":"http:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/"},"modified":"2022-09-24T18:44:18","modified_gmt":"2022-09-24T13:14:18","slug":"solved-simple-password-encryption-how-do-i-do-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/","title":{"rendered":"[Solved] Simple password encryption &#8211; how do i do? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-29782702\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"29782702\" data-parentid=\"29782631\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>In future, I&#8217;d suggest you refrain from begging for answers without first showing some code you&#8217;ve tried.<\/p>\n<p>That being said, I&#8217;ll bite.<\/p>\n<pre><code>import java.security.MessageDigest;\nimport java.security.NoSuchAlgorithmException;\nimport java.security.NoSuchProviderException;\nimport java.security.SecureRandom;\n\npublic class EncryptHelper\n{\n    public static String ehashAndSalt(String passedpass) throws NoSuchAlgorithmException, NoSuchProviderException\n    {\n        String passwordToHash = \"password\";\n        String salt = getSalt();\n\n        String securePassword = getSecurePassword(passwordToHash, salt);\n\n        return securePassword;\n    }\n\n    private static String getSecurePassword(String passwordToHash, String salt)\n    {\n        String generatedPassword = null;\n        try\n        {\n            \/\/ Create MessageDigest instance for MD5\n            MessageDigest md = MessageDigest.getInstance(\"MD5\");\n            \/\/Add password bytes to digest\n            md.update(salt.getBytes());\n            \/\/Get the hash's bytes\n            byte[] bytes = md.digest(passwordToHash.getBytes());\n            \/\/This bytes[] has bytes in decimal format;\n            \/\/Convert it to hexadecimal format\n            StringBuilder sb = new StringBuilder();\n            for(int i=0; i&lt; bytes.length ;i++)\n            {\n                sb.append(Integer.toString((bytes[i] &amp; 0xff) + 0x100, 16).substring(1));\n            }\n            \/\/Get complete hashed password in hex format\n            generatedPassword = sb.toString();\n        }\n        catch (NoSuchAlgorithmException e)\n        {\n            e.printStackTrace();\n        }\n        return generatedPassword;\n    }\n\n    \/\/Add salt\n    private static String getSalt() throws NoSuchAlgorithmException, NoSuchProviderException\n    {\n        \/\/Always use a SecureRandom generator\n        SecureRandom sr = SecureRandom.getInstance(\"SHA1PRNG\", \"SUN\");\n        \/\/Create array for salt\n        byte[] salt = new byte[16];\n        \/\/Get a random salt\n        sr.nextBytes(salt);\n        \/\/return salt\n        return salt.toString();\n    }\n}\n<\/code><\/pre>\n<p>Here&#8217;s a nice and simple helper class for a hash\/salt function. Just be sure to use the same &#8220;salt&#8221; string created when the user was created for when you authenticate the user, otherwise the authentication will fail.<\/p>\n<p>Where passwords are concerned, I find it safer to use a hash\/salt function rather than encryption, as encryption can be broken with the correct public\/private key.<\/p>\n<p>You can find more information on Java&#8217;s Native encryption <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/technotes\/guides\/security\/StandardNames.html#provider\">Here.<\/a><\/p>\n<hr>\n<p><strong>EDIT<\/strong><\/p>\n<p>As @james large pointed out, You should randomise the salt. I&#8217;ve amended the code to show this.<\/p>\n<p>Source of the above example: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/howtodoinjava.com\/2013\/07\/22\/how-to-generate-secure-password-hash-md5-sha-pbkdf2-bcrypt-examples\/\">HowToDoInJava<\/a><\/p>\n<p>I would then suggest you pass the salt and encrypted password to the database when creating new users, and then getting a resultset containing the salt and password and feeding it into a similar method to <code>getSecurePassword()<\/code> and using the outcome of this as a validation.<\/p>\n<p>I hope this helps!<\/p>\n<p><strong>Edit &#8211; 2<\/strong><\/p>\n<p>Insert another row into your table called &#8220;salt&#8221; (or whatever you like), and insert a new user with a PreparedStatement, like so:<\/p>\n<pre><code>PreparedStatement pstmnt  = connection.prepareStatement\n(\"insert into Usernames(`ID`,`Username`,`Password`,`Account type`, `salt`) values (?,?,?,?,?,)\");\npstmnt.setInt(1, id); \/\/would ideally be auto-incremented\npstmnt.setString(2, user); \/\/user String obtained by any means\npstmnt.setString(3, securePassword); \/\/from the hash\/salt example above\npstmnt.setString(4, accType); \/\/whatever naming structure you have for account types\npstmnt.setString(5, salt); \/\/from the above example also.\npstmnt.executeUpdate();\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Simple password encryption &#8211; how do i do? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] In future, I&#8217;d suggest you refrain from begging for answers without first showing some code you&#8217;ve tried. That being said, I&#8217;ll bite. import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; public class EncryptHelper { public static String ehashAndSalt(String passedpass) throws NoSuchAlgorithmException, NoSuchProviderException { String passwordToHash = &#8220;password&#8221;; String salt = getSalt(); String securePassword = &#8230; <a title=\"[Solved] Simple password encryption &#8211; how do i do? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/\" aria-label=\"More on [Solved] Simple password encryption &#8211; how do i do? [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[557,323,340,1470],"class_list":["post-10719","post","type-post","status-publish","format-standard","hentry","category-solved","tag-encryption","tag-java","tag-mysql","tag-passwords"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Simple password encryption - how do i do? [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Simple password encryption - how do i do? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] In future, I&#8217;d suggest you refrain from begging for answers without first showing some code you&#8217;ve tried. That being said, I&#8217;ll bite. import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; public class EncryptHelper { public static String ehashAndSalt(String passedpass) throws NoSuchAlgorithmException, NoSuchProviderException { String passwordToHash = &quot;password&quot;; String salt = getSalt(); String securePassword = ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-24T13:14:18+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Simple password encryption &#8211; how do i do? [closed]\",\"datePublished\":\"2022-09-24T13:14:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/\"},\"wordCount\":223,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"encryption\",\"java\",\"mysql\",\"passwords\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/\",\"name\":\"[Solved] Simple password encryption - how do i do? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-24T13:14:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Simple password encryption &#8211; how do i do? [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Simple password encryption - how do i do? [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Simple password encryption - how do i do? [closed] - JassWeb","og_description":"[ad_1] In future, I&#8217;d suggest you refrain from begging for answers without first showing some code you&#8217;ve tried. That being said, I&#8217;ll bite. import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; public class EncryptHelper { public static String ehashAndSalt(String passedpass) throws NoSuchAlgorithmException, NoSuchProviderException { String passwordToHash = \"password\"; String salt = getSalt(); String securePassword = ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/","og_site_name":"JassWeb","article_published_time":"2022-09-24T13:14:18+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Simple password encryption &#8211; how do i do? [closed]","datePublished":"2022-09-24T13:14:18+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/"},"wordCount":223,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["encryption","java","mysql","passwords"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/","name":"[Solved] Simple password encryption - how do i do? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-24T13:14:18+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-simple-password-encryption-how-do-i-do-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Simple password encryption &#8211; how do i do? [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=10719"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/10719\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=10719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=10719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=10719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}