2011-12-27 130 views
-1

我是java中的安全新手。java中的3Des加密/解密

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

下面提到的引脚块使用ZPK加密。 有没有办法在java中使用清除键来解密块。

clear_1 = "801CB5C89DC416C149FB645BB36897AD" 

clear_2 = "45B98FC7D33149E0512F0ED9135E5826" 

encrypted_pin_block = "6288FA9534BF2AA3" 

encrypted_pin_block = "B8D876F238348EB0" 

其中一个加密块的解密值为2222

+0

我试着仰视的加密方法样本实施例,但如果是DESede,我不能得到的结果。你假设人们知道ZPK和LMK,但通常他们不会。如果你发布实际的协议和算法,你就会有机会。 DESede解密不起作用,XOR'Key和解密与此。 – 2011-12-29 13:56:32

回答

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"); 
     } 
    }