他の誰かのAES暗号化によって書かれた参照コード、それを記録

パッケージorg.jimmy.autosearch2019.test。

インポートにjava.security.SecureRandom; 

輸入javax.crypto.Cipher;
輸入javax.crypto.KeyGenerator。
輸入javax.crypto.SecretKey。
インポートするjavax.crypto.spec.SecretKeySpec; 

パブリック クラスTestAes2019052801 { 

    公共 静的 ボイドメイン(文字列[]引数){
         試みる{ 
            文字列の内容 =「123456」
            文字列秘密鍵 = "123456" ;
            バイト [] encryptedContentBytes = 暗号化(コンテンツ、秘密鍵)。
            文字列の暗号化コンテンツ = parseBinaryToHexStr(encryptedContentBytes)
            のSystem.out.println( "プレ暗号文:" + コンテンツ)
            のSystem.out.println( "暗号文:" + 暗号化コンテンツ);
             バイト [] = encryptedContentToBinaryStr parseHexToBinaryStr(暗号化コンテンツ);
             バイト [] = decryptedContentBytes (encryptedContentToBinaryStr、秘密鍵)を復号するステップと、
            文字列decryptedContentは = 新しい新しい文字列(decryptedContentBytes)
            のSystem.out.println( "復号文:" + decryptedContent); 
        } キャッチ(例外E){ 
            e.printStackTrace(); 
        } 
    } 
    
    / ** 
     * @author RAピ危険SU RA RI(ドーン)
     * 2019年。月28日午前2時56分42秒の5 @date午後
     * @detailは16進数の文字列のバイナリ文字列に変換
     * / 
    パブリック 静的 バイト [] parseHexToBinaryStr(文字列hexstrの)がスロー例外{
         バイト []バイト= NULLをIF(hexStr.length()<1。){
             戻りバイト; 
        } 
        バイト = 新しい新しい バイト [hexStr.length()/ 2 ];
         のためにINT0 = Iは、I ++は、IはhexStr.length()/ 2 < ){
             int型高= Integer.parseInt(hexStr.substringが(私は、2 * 2 + I. 1)、16 );
             int型低= Integer.parseInt(hexstrのを.substring、16(I 2 + 1 *、I 2 + 2 *)); 
            バイト[I] =(バイト)(高* 16 + 低); 
        } 
        戻りバイト; 
    } 
    
    / ** 
     * @author RI RAピSU RA危険(ドーン)
     * @date 5月28日午前2時54分56秒の2019インディアン5午後
     2進数の文字列を変換@detail * 16進数の文字列
     * / 
    パブリック 静的文字列parseBinaryToHexStr(バイト []バイト)スロー{例外
        StringBufferのSB = 新しいStringBufferを();
        以下のためにint型 i = 0; I <bytes.length; I ++ ){ 
            文字列ヘクス = Integer.toHexString(バイト[I]&0xffで)。
            もし(hex.length()== 1 ){ 
                ヘクス = "0" + 六角。
            } 
            sb.append(hex.toUpperCase())。
        } 
        を返す)(sb.toStringします。
    } 
    
    / ** 
     * @author ラピスラズリ(ドーン)
     * @date 2019年5月28日下午午前3時30分33秒
     * @detail解密
     * / 
    パブリック 静的 バイト [](復号バイト[]コンテンツ、文字列秘密鍵)をスロー{例外
        するKeyGeneratorするKeyGenerator = KeyGenerator.getInstance( "AES" )。
        keyGenerator.init( 128、新しいのSecureRandom(secretKey.getBytes())); 
        SecretKey generatedSecretKey = keyGenerator.generateKey()。
        バイト [] encodedBytes = generatedSecretKey.getEncoded()。
        SecretKeySpec secretKeySpec = 新しい SecretKeySpec(encodedBytes、 "AES" ); 
        暗号暗号 = Cipher.getInstance( "AES" )。
        cipher.init(Cipher.DECRYPT_MODE、secretKeySpec); 
        バイト []結果= Cipher.doFinal(コンテンツ);  
         戻り値の結果; 
    } 
    
    / ** 
     * @author ラピ危険SU RA里(ドーン)
     * 2019年@date 2時55分25秒PM 5月28日
     * @暗号化の詳細をAES 
     * / 
    パブリック 静的 バイト []暗号化(内容文字列、文字列のSecretKey)がスロー例外{
         // のプロデューサーAESキーを作成 
        するKeyGeneratorのKeyGenerator = KeyGenerator.getInstance(「AES」を);
         // ユーザーのパスワードを初期化するために乱数を使用
         // 128ビットの鍵プロデューサー
         //暗号化は、のSecureRandomは乱数列を生成する確保、重要ではpassword.getBytes()、種子、あるいない
         //ライン上の復号化パスワードする限り、限り同じシード配列が同じであるとして
        KeyGenerator.init(128、新しい新しいのSecureRandom(secretKey.getBytes()));
         // ユーザーのパスワードは、キーが生成する 
        のSecretKey generatedSecretKey = keyGenerator.generateKey();
         //は、このキー場合は、キーの基本的なエンコード形式を返します。リターンはエンコードをサポートしていません
        バイト [] = encodedBytes generatedSecretKey.getEncoded();
         // AES秘密鍵に変換 
        SecretKeySpec SecretKeySpec = 新しい新しい SecretKeySpec(encodedBytes、 "AES" );
         // パスワードを作成している 
        暗号暗号= Cipher.getInstance( "AES" );
         バイト[] =のContentBytesのcontent.getBytes( "UTF-8" );
         //は、パスワードの暗号化モードに初期化
        cipher.init(Cipher.ENCRYPT_MODE、secretKeySpec)。
        // 加密
        バイト []結果= cipher.doFinal(contentBytes)。
        戻り値の結果; 
    } 

}

 

おすすめ

転載: www.cnblogs.com/JimmySeraph/p/10937699.html