3

ここでスタックオーバーフローのリンクを見つけましたuse-3des-encryption-decryption-in-java、しかし実際にはメソッドは2つのパラメーターのみを使用します:HG58YZ3CR9 "と " IvParameterSpec iv = new IvParameterSpec(new byte[8]);"
しかし、トリプルdesの最も強力なオプションは、暗号化に3つの異なるキーを使用できますメッセージ.それを行う方法?私は別のパラメーターとして「SecureRandom」を使用する Cipher で mehond を見つけます.これは正しい方法ですか?
最初のメソッドコードは以下のとおりです:

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("SHA-1");
        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("SHA-1");
        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");
    }
}
4

2 に答える 2

2

このドキュメントに従って、暗号に 168 ビット長のキーを渡すだけです。

キーサイズは 112 または 168 にする必要があります。

キーサイズが 112 の場合、2 つの中間キーを持つトリプル DES キーが生成され、キーサイズが 168 の場合、3 つの中間キーを持つトリプル DES キーが生成されます。

あなたのコードは、MD5 の出力の長さが 128 ビットしかないという事実を補うために何か疑わしいことをしているようです。

インターネットから暗号化コードをコピーして貼り付けても、安全なアプリケーションは作成されません。静的 IV を使用すると、CBC モードが ECB よりも優れているいくつかの理由が損なわれます。静的キーを使用している場合は、短い ASCII 文字列からキーを導出する代わりに、安全な乱数ジェネレータを使用してランダム バイトを生成することを検討する必要があります。また、新しいアプリケーションで AES の代わりに Triple DES を使用する理由はまったくありません。

于 2013-07-04T11:02:42.977 に答える