パブリッククラスAESUtils { //算法/模式/填充 プライベート静的最終列CipherMode = "AES / CBC / PKCS5Padding"変換。 //秘钥 プライベート静的な文字列DEFAULST_SECURITY_KEY = "69a183bbca2d9a01914d2dfa34455742"。 プライベート静的最後の文字列encodFormat = "UTF-8"; / ** *创建密钥 * * @paramキー * @return * / プライベート静的SecretKeySpec createKey(文字列のキー){ キー=キー== nullの?「」:キー。 StringBuilderのSB =新しいStringBuilderの(16)。 sb.append(キー); 一方、(sb.length()<16){ sb.append( "0")。 } バイト[]データ= NULL; {試みる データ= sb.toString()をGetBytesメソッド( "UTF-8")。 }キャッチ(にUnsupportedEncodingException電子){ e.printStackTrace(); } 新しいSecretKeySpec(データ、 "AES")を返します。 } / ** *创建初始化向量 * * @paramパスワード * @return * / プライベート静的IvParameterSpec createIV(文字列のパスワード){ パスワード=パスワード== nullの?"":パスワード; StringBuilderのSB =新しいStringBuilderの(16)。 sb.append(パスワード); 一方、(sb.length()< sb.append( "0"); } IF(sb.length()> 16){ sb.setLength(16); } バイト[]データ= NULL; 試み{ = sb.toStringデータ()GetBytesメソッド(「UTF 8「); }キャッチ(にUnsupportedEncodingException E){ e.printStackTrace(); } 新しい新IvParameterSpec(データを返す); } / ** * AES-128 / CBC暗号化アルゴリズムのバイト * * @param内容 * @param秘密のパスワードキー * @param静脈の初期化ベクトル * @returnバイト[] * / パブリック静的バイト[] aes128CBCEncrypt(バイト[]コンテンツ、文字列のパスワード、文字列IV){ {試す 暗号暗号= Cipher.getInstance(CipherModeを); cipher.init(Cipher.ENCRYPT_MODE、createKey(パスワード)、createIV(IV)); バイト[]結果= Cipher.doFinal(コンテンツ); 戻り値の結果; }キャッチ(例外E){ e.printStackTrace(); } 戻りNULL; } / ** * AES-128 /復号アルゴリズムは、CBCバイトアレイである * * @paramコンテンツ * @paramパスワードキー * @param静脈初期化ベクトル * @returnバイト[ ] * / パブリック静的バイト[] aes128CBCDecrypt(バイト[]コンテンツ、文字列のパスワード、文字列IV){ 試み{ 暗号暗号= Cipher.getInstance(CipherMode)。 cipher.init(Cipher.DECRYPT_MODE、createKey(パスワード)、createIV(IV)); バイト[]結果= cipher.doFinal(コンテンツ)。 結果を返します。 }キャッチ(例外e){ e.printStackTrace(); } はnullを返します。 } / ** * AES-128加密字符串 * * @paramコンテンツ * @return * / パブリック静的文字列の暗号化(ストリング含有量){ 試み{ するKeyGenerator kgen = KeyGenerator.getInstance( "AES")。 kgen.init(128、新規のSecureRandom(DEFAULST_SECURITY_KEY.getBytes(encodFormat)))。 バイト[]バイト= kgen.generateKey()クラスのgetEncoded()。 暗号暗号= Cipher.getInstance( "AES")。 cipher.init(Cipher.ENCRYPT_MODE、新しいSecretKeySpec(バイト、 "AES")); バイト[]結果= cipher.doFinal(content.getBytes(encodFormat))。 StringBufferのSB =新しいStringBufferを(); 以下のために(INT iが= 0; I <result.length; I ++){ 文字列ヘクス= Integer.toHexString(結果[I]&0xFFで)。 IF(hex.length()== 1){ ヘクス= '0' +六角。 } sb.append(hex.toUpperCase())。 リターンsb.toString(); }キャッチ(例外e){ e.printStackTrace(); } nullを返す; } / ** * CBC AES-128暗号化、Base64でのトランスコーディングを使用して暗号化 * * @paramコンテンツが暗号化されるコンテンツ *の@paramのencodingFormat * @paramキーキー * @param initVector初期化ベクトル * @return * @throws例外 * / パブリック静的な文字列aesCBCEncrypt(文字列コンテンツ、encodingFormat文字列、文字列のキー、文字列initVector){ 試み{ SecretKeySpec keySpec =新しい新しいSecretKeySpec(key.getBytes(encodingFormat) 、「AESは」); // CBCモード、ベクトルIVで、暗号化アルゴリズムの強度を増大させることができます IvParameterSpecベクトル=新しいIvParameterSpec(initVector.getBytes(encodingFormat))。 暗号暗号= Cipher.getInstance( "AES / CBC / PKCS5Padding"変換)。 cipher.init(Cipher.ENCRYPT_MODE、keySpec、ベクター)。 バイト[]暗号化= cipher.doFinal(content.getBytes(encodingFormat))。 //此处使用BASE64做转码 。文字列結果=新しいBASE64Encoder()エンコード(暗号化)。 結果を返します。 }キャッチ(例外e){ e.printStackTrace(); } はnullを返します。 } / ** * AES-CBC 128解密方式 * * @paramであるコンテンツ復号Base64文字列 * @param encodingFormat * @paramキー密钥 * @param initVector初期化ベクトル * @return * / パブリック静的な文字列aesCBCDecrypt(文字列コンテンツ、encodingFormat文字列、文字キー、文字列initVector){ 試み{ SecretKeySpec keySpec =新しい新しいSecretKeySpec(key.getBytes(encodingFormat)、「AES "); IvParameterSpec IvParameterSpec新しい新しいベクトル=(initVector.getBytes()); 暗号暗号= Cipher.getInstance(" AES / CBC / PKCS5Padding「変換); cipher.init(Cipher.DECRYPT_MODE、keySpec、ベクトル); // BASE64と第Base64でデコード使用して暗号化された対応するため、符号化 バイト[] base64Bytes =新しいBASE64Decoder( )decodeBuffer(コンテンツ); バイト[] =オリジナルcipher.doFinal(base64Bytes)。 文字列の結果=新しい新しいString(オリジナル、encodingFormat); 戻り値の結果; }キャッチ(例外e){ e.printStackTrace(); } はnullを返します。 } / ** *加密普通内容 * * * / パブリック静的文字列の暗号化(文字列のコンテンツ、文字列キー){ IF(のisEmpty(キー)){ 戻り暗号化(コンテンツ)。 } {試みる するKeyGenerator kgen = KeyGenerator.getInstance( "AES")。 kgen.init(128、新しいのSecureRandom(key.getBytes(encodFormat))); バイト[]バイト= kgen.generateKey()クラスのgetEncoded()。 暗号暗号= Cipher.getInstance( "AES")。 cipher.init(Cipher.ENCRYPT_MODE、新しいSecretKeySpec(バイト、 "AES")); バイト[]結果= cipher.doFinal(content.getBytes(encodFormat))。 StringBufferのSB =新しいStringBufferを(); 以下のために(INT iが= 0; I <result.length; I ++){ 文字列ヘクス= Integer.toHexString(結果[I]&0xFFで)。 IF(hex.length()== 1){ ヘクス= '0' +六角。 } sb.append(hex.toUpperCase())。 } )(sb.toStringを返します。 }キャッチ(例外e){ e.printStackTrace(); } はnullを返します。 } / ** *解密 * * @param内容 * @return * / パブリック静的文字列の復号化(文字列コンテンツ){ IF(のisEmpty(コンテンツ)){ 戻りヌル。 } バイト[]バイト=新しいバイト[content.length()/ 2]。 以下のために(INT iが= 0; I <content.length()/ 2; I ++){ int型の高= Integer.parseInt(iは*(2 content.substring、I * 2 + 1)、16)。 INTロー= Integer.parseInt(16、(I * 2 + 1、iは2 + 2 *)content.substring)。 [I] =(バイト)(高* 16 +低)バイト。 } {試みる するKeyGenerator kgen = KeyGenerator.getInstance( "AES")。 kgen.init(128、新しいのSecureRandom(DEFAULST_SECURITY_KEY.getBytes())); 秘密鍵のSecretKey = kgen.generateKey()。 SecretKeySpec secretKeySpec =新しいSecretKeySpec(secretKey.getEncoded()、 "AES"); 暗号暗号= Cipher.getInstance( "AES")。 cipher.init(Cipher.DECRYPT_MODE、secretKeySpec)。 バイト[]結果= cipher.doFinal(バイト)。 新しいString(結果)を返します。 }キャッチ(例外e){ e.printStackTrace(); } はnullを返します。 } / ** *解密 * * @param内容 * @param securityKey * @return * / パブリック静的文字列の復号化(文字列のコンテンツ、文字列securityKey){ IF(のisEmpty(securityKey)){ 戻り復号化(コンテンツ)。 } バイト[]バイト=新しいバイト[content.length()/ 2]。 以下のために(INT iが= 0; I <content.length()/ 2; I ++){ int型の高= Integer.parseInt(iは*(2 content.substring、I * 2 + 1)、16)。 INTロー= Integer.parseInt(16、(I * 2 + 1、iは2 + 2 *)content.substring)。 [I] =(バイト)(高* 16 +低)バイト。 } {試みる するKeyGenerator kgen = KeyGenerator.getInstance( "AES")。 kgen.init(128、新しいのSecureRandom(securityKey.getBytes())); 秘密鍵のSecretKey = kgen.generateKey()。 SecretKeySpec secretKeySpec =新しいSecretKeySpec(secretKey.getEncoded()、 "AES"); } 暗号暗号= Cipher.getInstance( "AES")。 cipher.init(Cipher.DECRYPT_MODE、secretKeySpec)。 バイト[]結果= cipher.doFinal(バイト)。 新しいString(結果)を返します。 }キャッチ(例外e){ e.printStackTrace(); } はnullを返します。 } プライベート静的ブールが等しい(バイト[] A1、バイト[] A2){ IF(!A1 = NULL && A2 = NULL && a1.length == a2.length){ ためには、(INT iは0 =; I <A1。長さ; I ++){ IF(A1 [I] = A2 [I]){! 戻り偽。 } } trueを返します。 } falseを返します。 isEmpty(String str)文字{boolean型プライベート静的 リターンstrの== nullの|| str.length()== 0。 } 無効メイン(文字列...引数)公共の静的にUnsupportedEncodingException {スロー / *文字列initVectorを= "AaBbCcDd1234 @#$!"; 文字列のキー= "WwXxYyZz1234 @#$!"; int型の合計= 100000; int型実行= 0; int型のカウント= 0; String []型strArr =新しいString [] { "////// \\\\ +++ ,,"、 "+++ ,, ////// \\ ++!"、 「++ !@#%¥......、。、......&*(」、 "【】)&(\\\" + \ "中国"、 "&(\\\" + \ "中国谁哟的事实上122你的"、 ランダムランダム=新しいランダム(のSystem.currentTimeMillis()); 以下のために(INT iが= 0; I <総; I ++){ ラン++。 StringBuilderのビルダー=新しいStringBuilderの(); builder.append(にRandom.nextInt(strArr.length))。 builder.append(にRandom.nextInt(strArr.length))。 builder.append(にRandom.nextInt(strArr.length))。 builder.append(にRandom.nextInt(strArr.length))。 builder.append(にRandom.nextInt(strArr.length))。 。バイト)[] =オリジナルbuilder.toString(GetBytesメソッド( "UTF-8"); バイト[]暗号化= aes128CBCEncrypt(元の、キー、initVector)。 バイト[]復号化され= aes128CBCDecrypt(暗号化、鍵、initVector)。 IF(等しい(元は、復号化されました)){+結果)。 のSystem.out.println( "ランの合計数である" +ラン); / * 文字列結果= aesCBCDecrypt( "cArgNYmA0cCFwKRV7B5FBvrKBSW4G8M +チー/ xqO0GDP4 ="、 "UTF-8"、 "69a183bbca2d9a01914d2dfa34455742"、 "69a183bbca2d9a01")。 System.out.println( "結果" +結果)。 } }
AES暗号化ツール
おすすめ
転載: www.cnblogs.com/liangb/p/12096736.html
おすすめ
ランキング