この記事では、AES暗号化と復号化を実装するためのヘックスエンコードとデコードの使用について説明します。次のように、参照のために共有してください。
ここでは、AES暗号化と復号化法がエンコードされ、六角形を使用してデコードされます。
パッケージcom.baidu.wallet.bdwallet.utils; Import java.io.unsupportedencodingception; Import java.security.invalidkeyexception; Import java.security.nosuchalgorithmexception; nosuchalgorithmexception; import javax.crypto.badpadindexception; javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.DecoderException;import org.apache.commons.codec.binary.Hex;public class Test { private static final String aes = "aes";プライベート静的最終文字列UTF8 = "UTF-8"; / ** AES encryption * @param content * @param pkey * @return * @throws decoderexception */ private static byte [] encrypt(string content、string pkey)throws decoderexception {try {string private_key = pkey; byte [] encodeformat = null; {//秘密の鍵をデコードする必要があるのはなぜですか?秘密の鍵はhexによってエンコードされた秘密の鍵の値であるため、使用される場合はデコードする必要があります。 } catch(decoderexception e){e.printstacktrace(); } secretKeyspec key = new SecretKeyspec(encodeFormat、AES); //暗号オブジェクトは実際に暗号化操作cipher cipher = cipher.getInstance( "aes/ecb/pkcs5padding"); // byte [] bytecontent = content.getBytes(utf8)をエンコードするためのコンテンツを暗号化します。 // cipherオブジェクトの初期化cipher.init(cipher.encrypt_mode、key); //暗号化操作の正式な実行byte [] result = cipher.dofinal(bytecontent);返品結果; } catch(nosuchalgorithmexception e){e.printstacktrace(); } catch(nosuchpaddingexception e){e.printstacktrace(); } catch(InvalidKeyException e){e.printstacktrace(); } catch(unsupportedencodingexception e){e.printstacktrace(); } catch(Illegalblocksizeexcection e){e.printstacktrace(); } catch(badPaddingException e){e.printstacktrace(); } nullを返します。 } /** * aes decryption * @param contents * @param password * @return * @throws decoderexception * /private static byte [] decrypt(] decrypt(string contents、string password)throws decoderexception {// hexを使用して、ciphertext byte [] content = hex.decodehex(hex.decodehex(tontents.tocharary(); //秘密の鍵をデコードする必要があるのはなぜですか?シークレットキーは、hexによってエンコードされる秘密キーの値であるため、使用する場合はデコードする必要があります[] byte [] encodeformat = hex.decodehex(password.tochararray()); SecretKeysPec key = new SecretKeyspec(encodeformat、AES); //暗号オブジェクトは実際に暗号化操作cipher cipher = cipher.getInstance(AES)を完了します。 // cipherオブジェクトの初期化cipher.init(cipher.decrypt_mode、key); //復号化操作の正式な実行byte [] result = cipher.dofinal(content);返品結果; } catch(nosuchalgorithmexception e){e.printstacktrace(); } catch(nosuchpaddingexception e){e.printstacktrace(); } catch(InvalidKeyException e){e.printstacktrace(); } catch(Illegalblocksizeexcection e){e.printstacktrace(); } catch(badPaddingException e){e.printstacktrace(); } nullを返します。 } /*** aes encryption* @param Context Plaintext* @param private_key Secret Key* @return* @throws decoderexception* /public static string encryption(string context、string private_key)decoderexceptionをスローする//パスワードテキストSTRING ENCRYPTRESULTSTR = HEX.ENCODEHEXSTRING(Encryptresult)をエンコードするhex encoding encryptresultstr; encryptresultstrを返します。 } /*** aes decrypt* @paramコンテキストciphertext* @param private_key secret key* @return* @throws decoderexception* @throws unsupportedencodingexcepting* /public static string decryption(string context、string private_key)スローデコデレクセプション、undupportedencodinception、unsupportedencodingexception byte [] decryptresult = decrypt(context、private_key); string result = new String(decryptresult、utf8);返品結果; } public static void main(string [] args)throws unsupportedencodingexception、decoderexception {//暗号化コンテンツ文字列content = "123456787654321"; // AES暗号化と復号化キー文字列パスワード= "この値は一般に与えられ、両方の送信者が知っています"; // encrypt system.out.println( "encryption:" + content); //暗号化方法を呼び出しますstring encryptresultstr = encryption(content、password); System.out.println( "暗号化後:" + encryptresultstr); //復号化方法文字列result = decrypt(encryptresultstr、パスワード)を呼び出す; // decoding system.out.printlnのコンテンツを復号化する( "decryption:" + result); }}正式なプロジェクトでは、この方法に問題はありません。ここでAES暗号化と復号化を修正する必要があることに注意してください...
上記の方法は、org.apache.commons.codec.binary.hexで使用されます。 Mavenの構成は次のとおりです。
<Dependency> GroupId> Commons-Codec </groupId> <artifactid> commons-codec </artifactid> <バージョン> 1.4 </version> </dependency>
注:ここではバージョン1.4以降を使用する必要があります。1.4未満のHex.EncodeHexString(BYTE [])メソッドはありません。
PS:暗号化と復号化に興味がある友人は、このサイトのオンラインツールを参照することもできます。
パスワードセキュリティオンライン検出:
http://tools.vevb.com/password/my_password_safe
高強度パスワードジェネレーター:
http://tools.vevb.com/password/createstrongpassword
Thunder、Express、およびTornado URL暗号化/復号化ツール:
http://tools.vevb.com/password/urlrethunder
オンラインハッシュ/ハッシュアルゴリズム暗号化ツール:
http://tools.vevb.com/password/hash_encrypt
オンラインMD5/ハッシュ/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160暗号化ツール:
http://tools.vevb.com/password/hash_md5_sha
オンラインSHA1/SHA224/SHA256/SHA384/SHA512暗号化ツール:
http://tools.vevb.com/password/sha_encode
この記事がみんなのJavaプログラミングに役立つことを願っています。