java中的3Des加密/解密

问题描述 投票:0回答:1

我是 Java 安全方面的新手。

我有两个清晰的双长度密钥,用于在 Futurex HSM 中生成 ZPK(在 LMK 下)。

下面提到的引脚块是使用 ZPK 加密的。 有没有办法使用java中的明文密钥来解密该块。

clear_1 = "801CB5C89DC416C149FB645BB36897AD"

clear_2 = "45B98FC7D33149E0512F0ED9135E5826"

encrypted_pin_block = "6288FA9534BF2AA3"

encrypted_pin_block = "B8D876F238348EB0"

这些加密块之一的解密值为

2222

java encryption 3des
1个回答
0
投票

参见 3dec 加密和解密示例

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;



class ZiggyTest2{


        public static void main(String[] args) throws Exception{  
            String text = "I am sunil";

            byte[] codedtext = new ZiggyTest2().encrypt(text);
            String decodedtext = new ZiggyTest2().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 {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] digestOfPassword = md.digest("ABCDEABCDE"
                            .getBytes("utf-8"));
            byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
            for (int j = 0, k = 16; j < 8;) {
                    keyBytes[k++] = keyBytes[j++];
            }

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

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

            return cipherText;
        }

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

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

            byte[] plainText = decipher.doFinal(message);

            return new String(plainText, "UTF-8");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.