プログラミングでは、多くの場合、いくつかの特別なコンテンツを暗号化する必要があります。今日、私はいくつかの簡単な暗号化方法を要約して、それらをあなたと共有しました!
Javaで文字列の単純な暗号化と復号化を実装する方法は?ユーザー情報のセキュリティを確保するには、システムがユーザー情報を保存するときに、パスワードを暗号化してデータベースに保存する必要があります。
パスワードを使用する必要がある場合は、データを取り出して復号化します。
Plantextパスワードの保存は避けてください。
プラン1:
パッケージcom.tnt.util; java.security.messagedigestをインポートします。 public class stringutil {private final static string [] hexdigits = {"0"、 "1"、 "2"、 "3"、 "4" 5 "、" 6 "、" 7 "、8" 9 "、" a "、" b "、" c "、" d "、" e "}; / ***バイトアレイをhex stringに変換** @param b* byte array* @return hex string*/ public static string bytearraytohexstring(byte [] b){stringbuffer resultsb = new StringBuffer(); for(int i = 0; i <b.length; i ++){resultsb.append(bytetohexstring(b [i])); } return resultsb.toString(); } private static string bytetohexstring(byte b){int n = b; if(n <0)n = 256 + n; int d1 = n / 16; int d2 = n%16; return hexdigits [d1] + hexdigits [d2]; } public static string md5encode(string origin){string resultstring = null; try {resultString = new String(Origin); MESSAGEDGEST MD = MESSAGEDGEST.GETINSTANCE( "MD5"); resultString = bytearraytohexstring(md.digest(resultString .getBytes())); } catch(Exception ex){} return resultString; } public static void main(string [] args){system.err.println(md5encode( "123456")); }}計画2
パッケージcom.shangyu.core.utils; public class md5 {public static string getmd5(byte [] source){string s = null; char hexdigits [] = {//バイトを16進表現 '0' 0 '0、' 1 '、' 2 '、' 4 '' '' '' '' '' '5' 7 '7' 7 '7' 7 '7' 7 '7' 7 '7' 7 '7' 7 '7'を変換するために使用される文字。 'a'、 'b'、 'c' '、' d '、' e '、' f '}; try {java.security.messagegest md = java.security.messagegest.getinstance( "md5"); md.update(source); byte tmp [] = md.digest(); // md5の計算結果は128ビットの長い整数です。// 16バイトであるバイトで表されます。 char str [] = new char [16 * 2]; //各バイトが16進数で表されている場合、2つの文字が使用されます。 //(int i = 0; i <16; i ++){//最初のバイトから始まる変換結果における対応する文字位置を表します。 // i番目のバイトstr [k ++] = hexdigits [byte0 >>> 4&0xf]; //バイトの上部4ビットの数値変換を取得します// >>> //論理シフトの右に、シンボルビットを右にシフトしましたstr [k ++] = hexdigits [byte0&0xf]; //バイトの下部4ビットの数値変換} s = new String(str); // stringに変換後の結果を変換} catch(例外e){e.printstacktrace();} return s;} public static string getmd5(string str){return getmd5(str.getBytes())読んでくれてありがとう、私はそれがあなたを助けることができることを願っています。このサイトへのご支援ありがとうございます!