コードを書く前に、使用する2つのパッケージを紹介しましょう。
Commons-Codec-1.10.jar
Commonsプロジェクトで使用されるツールパッケージは、DES、SHA1、MD5、Base64、URL、SoundXなどの一般的なコーディング方法を処理します。
Commons-exec-1.3.jar
Apache Commons ExecはApacheのJavaプロジェクトであり、外部プロセスを実行するための一般的な方法を提供します。
このサイトから公式のApache Commonsパッケージを直接ダウンロードできます
コード構造を見てみましょう。
org.apache.commons.codec.binary.base64; import org.apache.commons.comdec.digest.digestutils;/*** @author delver_si **/public class encodedecode {/****md5 encryption*@return*/public Static String(String) digestutils.md5hex(str); } / ** * base64 encryption * @param str * @return * / public static string base64encode(string str){byte [] b = base64.encodebase64(str.getbytes()、true);新しい文字列(b)を返します。 } / ** * base64 decrypt * @param str * @return * / public static string base64decode(string str){byte [] b = base64.decodebase64(str.getBytes());新しい文字列(b)を返します。 } / ** * sha1 * / public static string sha1encode(string str){return digestutils.sha1hex(str); }}メイン関数を1つのクラスファイルに配置します
前のファイルを参照する新しいテストクラスを作成する
Security.encodeandDecode; Import security.exec; public class test {public static void main(string [] args){system.out.println(encodeanddecode.md5encode( "vevb.com")); System.out.println(encodeanddecode.base64decode( "ami1ms5uzxq =") }}OK、最終結果を確認するために実行してください
これらは、Apache Commonsパッケージの基本的な機能にすぎません。他の機能については、中国語の単語バージョンで使用するためにApache Commonsの指示をダウンロードできます。他の機能に関する詳細な研究。