Android JAVA 中的三重 DES 算法

Triple DES Algorithm in Android JAVA

我是 Android-Java 的新手。我在 C# 中有以下函数用于加密(三重 DES)

public string Encrypt(string data)
    {
        try
        {
            if (!string.IsNullOrEmpty(data))
            {
                //byte[] keyArray;
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(data);
                //System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();

                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                TripleDESCryptoServiceProvider CryptDesECB = new TripleDESCryptoServiceProvider();
                CryptDesECB.Key = keyArray;
                CryptDesECB.Mode = CipherMode.ECB;
                CryptDesECB.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = CryptDesECB.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                CryptDesECB.Clear();
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            return string.Empty;
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }

我已尝试使用以下代码在 Android 平台中使用相同的加密功能:

public static String EncryptText(String message) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = message.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte [] base64Bytes = Base64.encode(buf,Base64.DEFAULT);
        String base64EncryptedString = new String(base64Bytes);

        return base64EncryptedString;
}

但是,两个平台的加密结果不一样?如果有人可以帮助我指出 Android 函数中的问题。

提前致谢。

我在下面写了 class 三重 DES ECB 模式 PKCS5/7 填充模式加密解密,它对我有用:

public class DESedeEncryption {

    private static final String UNICODE_FORMAT = "UTF8";
    public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
    private KeySpec myKeySpec;
    private SecretKeyFactory mySecretKeyFactory;
    private Cipher cipher;
    byte[] keyAsBytes;
    private String myEncryptionKey;
    private String myEncryptionScheme;
    SecretKey key;

    public DESedeEncryption() throws Exception
    {
        myEncryptionKey = "YOURPRIVATEKEY";
        myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
        keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
        myKeySpec = new DESedeKeySpec(keyAsBytes);
        mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
        cipher = Cipher.getInstance(myEncryptionScheme);
        key = mySecretKeyFactory.generateSecret(myKeySpec);
    }

    /**
     * Method To Encrypt The String
     */
    public String encrypt(String unencryptedString) {
        String encryptedString = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
            byte[] encryptedText = cipher.doFinal(plainText);
            encryptedString = Base64.encodeToString(encryptedText, Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedString.endsWith("\n") ? encryptedString.replace("\n","") : encryptedString;
    }
    /**
     * Method To Decrypt An Ecrypted String
     */
    public String decrypt(String encryptedString) {
        String decryptedText=null;
        try {
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptedText = Base64.decode(encryptedString, Base64.DEFAULT);
            byte[] plainText = cipher.doFinal(encryptedText);
            decryptedText= bytes2String(plainText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decryptedText;
    }
    /**
     * Returns String From An Array Of Bytes
     */
    private static String bytes2String(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i > bytes.length; i++) {
            stringBuffer.append((char) bytes[i]);
        }
        return stringBuffer.toString();
    }
}