Triple DES Encrypt C# - Decrypt in Java

后端 未结 3 1868
孤独总比滥情好
孤独总比滥情好 2021-01-07 14:45

I\'m getting a Triple DES decrypted string from the clients server, which has been coded in c# (see below):

using System.IO;
using System;
using System.Secur         


        
3条回答
  •  温柔的废话
    2021-01-07 15:32

    If someone find himself/herself in the same problem like I did, here is a java implementation (android) of the same .NET decrypt function:

     public static byte[] byteArrayConcat(byte[] array1, byte[] array2) {
            byte[] result = new byte[array1.length + array2.length];
            System.arraycopy(array1, 0, result, 0, array1.length);
            System.arraycopy(array2, 0, result, array1.length, array2.length);
            return result;
        }
    
    
     private byte[] fGPKeyTo3DESKey(byte[] GPKey) {
    
            byte[] _3DESKey = new byte[24];
            byte[] tmp = new byte[8];
    
            arraycopy(GPKey, 0, tmp, 0, 8);
    
            _3DESKey = DaPlugUtils.byteArrayConcat(GPKey, tmp);
    
            return _3DESKey;
        }
    
     private static byte[] hexStringtoByteArray(String hex) {
            int len = hex.length();
    
            byte[] data = new byte[len / 2];
            for (int i = 0; i < len; i += 2) {
                data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + Character.digit(hex.charAt(i + 1), 16));
            }
            return data;
        }
    
    public String desDecryptPin(String pin, String encryptKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
    
            int bytesCount = 0;
            int positionCount = 0;
    
    
            byte[] bytEncryptedChunk = new byte[8];
    
            ArrayList Input = new ArrayList();
            bytesCount = pin.length();
    
            for (int i = 0; i < bytesCount; i += 2) {
                if (positionCount == 8) {
                    positionCount = 0;
                    Input.add(bytEncryptedChunk);
                    bytEncryptedChunk = new byte[8];
                }
                bytEncryptedChunk[positionCount] = (byte) (Integer.parseInt(pin.substring(i, i + 2), 16));
                positionCount++;
            }
    
            if (positionCount != 0) {
                Input.add(bytEncryptedChunk);
            }
    
    
            byte[] _3DESKey = fGPKeyTo3DESKey(hexStringtoByteArray(encryptKey));
    
            DESedeKeySpec keySpec = new DESedeKeySpec(_3DESKey);
            SecretKey k = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
            Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, k);
    
    
            String res = "";
    
            for (byte[] bs : Input) {
                byte[] decryptPin = cipher.doFinal(bs);
                String a = new String(decryptPin, StandardCharsets.US_ASCII);
    
                res += a;
            }
    
            return res.trim();
        }
    

提交回复
热议问题