RC2加密Java

时间:2013-08-06 15:09:28

标签: java encryption

我需要一个java Aplication的数据库密钥的加密/解密算法。 我必须使用以前使用c#的algoritmh工具,但它使用的一些类,没有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创建等效代码吗?或其他替代方案?

谢谢!

1 个答案:

答案 0 :(得分: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(代码中的Vector),这些方法将产生与您列出的代码兼容的输入/输出。