我们如何在java中进行三重解密?

时间:2017-08-23 08:07:13

标签: java

我正在使用MOL API进行VOUCHER充值。返回的PIN号码成功响应。该PIN码将是加密文本,我需要对其进行三重解密!

我联系了API支持,他们提供了以下代码:

byte[] PinBytes = Base64.decodeBase64(encryptedText.getBytes("utf-8"));
byte[] VectorBytes = Base64.decodeBase64(vectorKey.getBytes("utf-8"));
byte[] SecretKeyBytes = Base64.decodeBase64(secretKey.getBytes("utf-8")); 
TripleDESProvider = CreateTripleDESCryptographicProvider(VectorBytes, SecretKeyBytes)
    DecryptedBytes = TripleDESProvider.Decrypt(PinBytes) 

这里VectorBytes和SecretKeyBytes是由他们提供的安全密钥,PinBytes是加密的响应PIN号。

当我无法使用这三个参数获得正确的解决方案时,我用Google搜索。有什么帮助吗?

我试过了:

try
        {
            String encryptedText = "FN0hbSrVzkqhe+w2rQefAQ==";
            String vectorKey = "7EsBtzAJjMg=";
            //32 bit key
            String secretKey = "08061052989102040806105298910204";
            byte[] PinBytes = Base64.decodeBase64(encryptedText.getBytes("utf-8"));
            byte[] VectorBytes = Base64.decodeBase64(vectorKey.getBytes("utf-8"));
            byte[] SecretKeyBytes = Base64.decodeBase64(secretKey.getBytes("utf-8"));
            final MessageDigest md = MessageDigest.getInstance("md5");
            final byte[] digestOfPassword = md.digest(SecretKeyBytes);
            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(VectorBytes);
            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(PinBytes);

            System.out.println(plainText.toString());           
        }
        catch (java.security.InvalidAlgorithmParameterException e) { System.out.println("Invalid Algorithm"); }
        catch (javax.crypto.NoSuchPaddingException e) { System.out.println("No Such Padding"); }
        catch (java.security.NoSuchAlgorithmException e) { System.out.println("No Such Algorithm"); }
        catch (java.security.InvalidKeyException e) { System.out.println("Invalid Key"); }
        catch (BadPaddingException e) { System.out.println("Invalid Key");}
        catch (IllegalBlockSizeException e) { System.out.println("Invalid Key");}
        catch (UnsupportedEncodingException e) { System.out.println("Invalid Key");}     
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

错误是无效的密钥

他们提供的文件是

将加密引脚从Base64格式转换为字节数组

PinBytes = ConvertFromBase64String ("FN0hbSrVzkqhe+w2rQefAQ==")

由于Vector64和Secret Key以Base64格式提供,因此我们需要将两个数据转换为字节数组

VectorBytes = ConvertFromBase64String ("xxxxxxxxx") 
SecretKeyBytes = ConvertFromBase64String ("xxxxxxxxxxxx")

请创建Triple DES加密提供程序,并将您的IV字节数组和密钥字节数组设置到您的提供程序中。

TripleDESProvider = CreateTripleDESCryptographicProvider(VectorBytes, SecretKeyBytes)

请调用Triple DES Provider解密方法。

DecryptedBytes = TripleDESProvider.Decrypt(PinBytes)

最后将解密后的字节转换回字符串。

ConvertToString(DecryptedBytes)

结果应为8157151550。

2 个答案:

答案 0 :(得分:1)

我不确定这是否是正确的方法,但使用AES加密可行。他们为您提供了向量,使用它来初始化IVParameterSpec,然后创建Key对象和Cipher实例:

// this is the encripted text
byte[] PinBytes = Base64.decodeBase64(encryptedText.getBytes("utf-8"));

byte[] VectorBytes = Base64.decodeBase64(vectorKey.getBytes("utf-8"));

byte[] SecretKeyBytes = Base64.decodeBase64(secretKey.getBytes("utf-8")); 

// initialize the vector with the one you receive               
IvParameterSpec spec = new IvParameterSpec(VectorBytes);

// create the key. DESede should be correct, but if it doesn't work try also with DES
Key key = new SecretKeySpec(SecretKeyBytes, "DESede");

// Initialize the cipher
Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");

// decrypt the string
c.init(Cipher.DECRYPT_MODE, key, spec);
byte[] decodedDecryptedBytes = c.doFinal(PinBytes);
我使用的

Base64对象来自apache common codec library,但您可以使用所需的库。

答案 1 :(得分:0)

最后,我得到了相同的秘密密钥答案!!

try
        {
            byte[] PinBytes = Base64.decodeBase64(encryptedText);
            byte[] VectorBytes = Base64.decodeBase64(vectorKey);
            byte[] SecretKeyBytes = Base64.decodeBase64(secretKey);
         // initialize the vector with the one you receive               
            IvParameterSpec spec = new IvParameterSpec(VectorBytes);

            // create the key. DESede should be correct, but if it doesn't work try also with DES
            Key key = new SecretKeySpec(SecretKeyBytes, "DESede");

            // Initialize the cipher
            Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");

            // decrypt the string
            c.init(Cipher.DECRYPT_MODE, key, spec);
            byte[] decodedDecryptedBytes = c.doFinal(PinBytes);
            return new String(decodedDecryptedBytes, "UTF-8");      
        }
        catch (java.security.InvalidAlgorithmParameterException e) { System.out.println("Invalid Algorithm"); }
        catch (javax.crypto.NoSuchPaddingException e) { System.out.println("No Such Padding"); }
        catch (java.security.NoSuchAlgorithmException e) { System.out.println("No Such Algorithm"); }
        catch (java.security.InvalidKeyException e) { System.out.println("InvalidKeyException : Invalid Key"); }
        catch (BadPaddingException e) { System.out.println("BadPaddingException : Invalid Key");}
        catch (IllegalBlockSizeException e) { System.out.println("IllegalBlockSizeException : Invalid Key");}
        catch (UnsupportedEncodingException e) { System.out.println("UnsupportedEncodingException : Invalid Key");}     
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

错误就是这样:

 byte[] SecretKeyBytes = Base64.decodeBase64(secretKey.getBytes("utf-8"));

我不知道为什么会这样!但通过删除,我得到了答案!!