-1

我需要一个 java 应用程序中数据库密钥的加密/解密算法。我必须使用以前使用 c# 的算法实现,但是它使用的某些类没有 java 等效项。

这是 C# 代码:

public static string Encryptor(string text) 
    {
        SymmetricAlgorithm saEnc;
        byte[] dataorg = Encoding.Default.GetBytes(text);
        saEnc = SymmetricAlgorithm.Create("RC2");
        ICryptoTransform ct = saEnc.CreateEncryptor(Key, Vector);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
        cs.Write(dataorg, 0, dataorg.Length);
        cs.FlushFinalBlock();
        string retorno = Convert.ToBase64String(ms.ToArray());
        ms.Close();
        saEnc.Clear();
        cs.Clear();
        cs.Close();
        ms = null;
        saEnc = null;
        cs = null;

        return retorno;
    }

    /**********************************************************************************
    DESCRIPCIÓN:   Desencripta un texto
    PARÁMETROS:
       Entrada:
               text     Texto a desencriptar 
       Salida:
               Texto desencriptado
    **********************************************************************************/
    public static string Decryptor(string text) 
    {
        SymmetricAlgorithm saDEnc = SymmetricAlgorithm.Create("RC2");

        byte[] textoEncriptado = Convert.FromBase64String(text);
        MemoryStream ms = new MemoryStream(textoEncriptado);
        ICryptoTransform cto = saDEnc.CreateDecryptor(Key, Vector);
        MemoryStream mso = new MemoryStream();
        CryptoStream cso = new CryptoStream(mso, cto, CryptoStreamMode.Write);
        cso.Write(ms.ToArray(), 0, ms.ToArray().Length);
        cso.FlushFinalBlock();

        string retorno = Encoding.Default.GetString(mso.ToArray());

        saDEnc.Clear();
        ms.Close();
        mso.Close();
        cso.Clear();
        cso.Close();
        saDEnc = null;
        ms = null;
        mso = null;
        cso = null;

        return retorno;
    }

在java中创建等效代码的一些帮助?或其他选择?

谢谢!

4

1 回答 1

0

这应该为你做。

public static String encrypt(byte[] key, byte[] iv, String unencrypted) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException{
    RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv);
    Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec);
    byte[] encrypted = cipher.doFinal(unencrypted.getBytes());
    return DatatypeConverter.printBase64Binary(encrypted);
}

public static String decrypt(byte[] key, byte[] iv, String encrypted) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
        IllegalBlockSizeException, BadPaddingException{
    RC2ParameterSpec ivSpec = new RC2ParameterSpec(key.length*8, iv);
    Cipher cipher = Cipher.getInstance("RC2/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "RC2"), ivSpec);
    byte[] decrypted = cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted));

    return new String(decrypted);
}

使用相同的 Key 和 IV(代码中的向量),这些方法将产生与您列出的代码兼容的输入/输出。

于 2013-08-06T16:03:49.947 回答