java3DESの暗号化と復号化のオフセットのコード例について

 

// 3DES加密
    公開 static String getEnc3DES(String data、String key、String iv)throws Exception { 
        Cipher cipher = Cipher.getInstance( "DESede / CBC / PKCS5Padding" ); 
        DESedeKeySpec dks = new DESedeKeySpec(key.getBytes( "gb2312" )); 
        IvParameterSpec ivs = new IvParameterSpec(iv.getBytes( "gb2312" )); 
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "DESede" ); 
        SecretKey securekey = keyFactory.generateSecret(dks); 
        cipher.init(Cipher.ENCRYPT_MODE、securekey、ivs);
        BASE64Encoder base64Encoder =新しいBASE64Encoder();
        return base64Encoder.encode(cipher.doFinal(data.getBytes( "GB2312" ))); 
    }

 

// 3DES解密
    公開 static String getDes3DES(String data、String key、String iv)throws Exception { 
        BASE64Decoder base64Decoder = new BASE64Decoder();
        バイト [] databyte = base64Decoder.decodeBuffer(data); 
        Cipher cipher = Cipher.getInstance( "DESede / CBC / PKCS5Padding" ); 
        DESedeKeySpec dks = new DESedeKeySpec(key.getBytes( "gb2312" )); 
        IvParameterSpec ivs = new IvParameterSpec(iv.getBytes( "gb2312" )); 
        SecretKeyFactory keyFactory= SecretKeyFactory.getInstance( "DESede" ); 
        SecretKey securekey = keyFactory.generateSecret(dks); 
        cipher.init(Cipher.DECRYPT_MODE、securekey、ivs); 
        返す 新しい文字列を(cipher.doFinal(databyte)、 "GB2312" )。
    }
    

 

  注:KEY値は、必要に応じて生成されます。IVオフセットは自分で生成することもできます。これは単なる例です。渡されるパラメーターデータはjsonタイプまたはその他のもので、渡されると.toString()のように使用できます。

public  static String KEY = "***";
    public  static String subStr()throws Exception { 
        String exOnceByTime = ExDateUtil.exOnceByTime(ExDateUtil.ExDate()); //将2019-04-11 14:33:20变成格式为20190411143320 
        String IV = StringUtils.substring(exOnceByTime、 0、8 );
        IVを返す; 
    } 
    //
    解読数
        データpublic  static JSONObject decode(JSONObject json)throws Exception { Object data = json.get( "data" ); 
        文字列des3des = getDes3DES(data.toString()、KEY、subStr());
        JSONObject.fromObject(des3des)​​;を返します。
    }

おすすめ

転載: www.cnblogs.com/divingCat/p/12699560.html