{"id":70,"date":"2008-05-12T17:14:08","date_gmt":"2008-05-12T21:14:08","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2008\/05\/12\/encrypt-a-string-using-sha-encryption\/"},"modified":"2008-05-12T17:14:08","modified_gmt":"2008-05-12T21:14:08","slug":"encrypt-a-string-using-sha-encryption","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2008\/05\/12\/encrypt-a-string-using-sha-encryption\/","title":{"rendered":"Encrypt a string using SHA encryption"},"content":{"rendered":"<p>The following class provides the venue to translate the sample string &#8220;mySecr3tP4ssw0rd&#8221; into the encrypted string &#8220;Nj3lzFVrt9dx2gENZeh2H5xY6PY=&#8221;, which would be tougher to crack by brute force than a plain password in case a hackers gets hold of your data store.<\/p>\n<pre class=\"code\">\nimport java.security.MessageDigest;\nimport sun.misc.BASE64Encoder;\n\n\/**\n * String encryption related utilities.\n * @author C. Peter Chen of http:\/\/dev-notes.com\n * @date 20080512\n *\/\n\npublic class StringEncryptUtil {\n\t\/**\n\t * This main() class is used for demo only.\n\t * @param args\n\t *\/\n\tpublic static void main(String[] args) {\n\t\tSystem.out.println(\"SHA encrypted mySecr3tP4ssw0rd: \" + encryptSha(\"mySecr3tP4ssw0rd\"));\n\t}\n\t\n\t\/**\n\t * Performs a SHA encryption process on the incoming string parameter.\n\t * @param inputStr\n\t * @return SHA-encrypted string if successful, or null if there are problems.\n\t *\/\n\tpublic static synchronized String encryptSha(String inputStr) {\n\t\ttry {\n\t\t\tMessageDigest md = MessageDigest.getInstance(\"SHA\");\n\t\t\tmd.update(inputStr.getBytes(\"UTF-8\"));\n\t\t\tbyte digest[] = md.digest();\n\t\t\treturn (new BASE64Encoder()).encode(digest);\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\treturn null;\n\t\t}\n\t}\n}\n<\/pre>\n<p>As you will notice, there is no decrypt method, that is because there probably is no need for one.  For instance, we have &#8220;Nj3lzFVrt9dx2gENZeh2H5xY6PY=&#8221; in the data store for the user Scott; when Scott logs in, we should SHA-encrypt the password he had just typed in, and compare the encrypted string with the encrypted string found in the data store.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At times, we run into the need to encrypt strings.  One popular situation when this is needed is when we need to store users&#8217; passwords in a database.  This note provides a simple Java class that makes use of the fairly strong SHA (Secure Hash Algorithm) 160-bit encryption method.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}