第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

請(qǐng)問如何在Java中使用3 DES加密/解密?

請(qǐng)問如何在Java中使用3 DES加密/解密?

慕田峪4524236 2019-08-02 07:02:14
如何在Java中使用3 DES加密/解密?我編寫的使用3 DES在Java中編碼字符串的每一種方法都不能被解密回原來的字符串。有沒有人有一個(gè)簡(jiǎn)單的代碼片段,只需對(duì)字符串進(jìn)行編碼,然后將字符串解碼回原來的字符串?我知道我在這個(gè)代碼的某個(gè)地方犯了一個(gè)非常愚蠢的錯(cuò)誤。到目前為止,我一直在做這樣的工作:*注意,我不會(huì)從加密方法中返回base 64文本,也不會(huì)在解密方法中返回base 64不編碼,因?yàn)槲以噲D查看我是否在謎題的base 64部分中出錯(cuò)。public class TripleDESTest {     public static void main(String[] args) {         String text = "kyle boon";         byte[] codedtext = new TripleDESTest().encrypt(text);         String decodedtext  = new TripleDESTest().decrypt(codedtext);         System.out.println(codedtext);         System.out.println(decodedtext);     }     public byte[] encrypt(String message) {         try {             final MessageDigest md = MessageDigest.getInstance("md5");             final byte[] digestOfPassword = md.digest("HG58YZ3CR9".getBytes("utf-8"));             final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);             for (int j = 0,  k = 16; j < 8;)             {                 keyBytes[k++] = keyBytes[j++];             }             final SecretKey key = new SecretKeySpec(keyBytes, "DESede");             final IvParameterSpec iv = new IvParameterSpec(new byte[8]);             final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");             cipher.init(Cipher.ENCRYPT_MODE, key, iv);             final byte[] plainTextBytes = message.getBytes("utf-8");             final byte[] cipherText = cipher.doFinal(plainTextBytes);             final String encodedCipherText = new sun.misc.BASE64Encoder().encode(cipherText);             return cipherText;             }
查看完整描述

3 回答

?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

除了Base 64編碼位(您提到這是一個(gè)測(cè)試)之外,您的代碼都很好,輸出可能沒有意義的原因可能是您顯示了一個(gè)原始字節(jié)數(shù)組(對(duì)字節(jié)數(shù)組執(zhí)行toString()返回其內(nèi)部Java引用,而不是返回其內(nèi)部Java引用的字符串表示形式)。內(nèi)容)。這里有一個(gè)版本,它只是一點(diǎn)點(diǎn)清理,打印“凱爾布恩”作為解碼字符串:

import java.security.MessageDigest;import java.util.Arrays;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;public class TripleDESTest {

    public static void main(String[] args) throws Exception {

        String text = "kyle boon";

        byte[] codedtext = new TripleDESTest().encrypt(text);
        String decodedtext = new TripleDESTest().decrypt(codedtext);

        System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array
        System.out.println(decodedtext); // This correctly shows "kyle boon"
    }

    public byte[] encrypt(String message) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
                .getBytes("utf-8"));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);

        final byte[] plainTextBytes = message.getBytes("utf-8");
        final byte[] cipherText = cipher.doFinal(plainTextBytes);
        // final String encodedCipherText = new sun.misc.BASE64Encoder()
        // .encode(cipherText);

        return cipherText;
    }

    public String decrypt(byte[] message) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
                .getBytes("utf-8"));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        decipher.init(Cipher.DECRYPT_MODE, key, iv);

        // final byte[] encData = new
        // sun.misc.BASE64Decoder().decodeBuffer(message);
        final byte[] plainText = decipher.doFinal(message);

        return new String(plainText, "UTF-8");
    }}





查看完整回答
反對(duì) 回復(fù) 2019-08-03
  • 3 回答
  • 0 關(guān)注
  • 470 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)