加密和解密使用的字符串C#的CryptoStream的一个Java equilavent [英] Encrypting and Decrypting String using a Java equilavent of the C# CryptoStream

查看:1288
本文介绍了加密和解密使用的字符串C#的CryptoStream的一个Java equilavent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在发展中的Java的移动平台操作系统的应用程序。

I am looking at developing an application in Java for a mobile platform operating system.

我已经开发了在C#WPF为Windows环境的应用程序。我正在使用顺序的CryptoStream加密和使用下面的代码解密的字符串。如下所示的代码是加密唯一

I have developed an application in C# WPF for the Windows Environment. I am using a cryptostream in order to encrypt and decrypt a string using the following code. the code shown below is the encryption only

public string encrypt(string encryptionString)
    {
        byte[] clearTextBytes = Encoding.UTF8.GetBytes(encryptionString);

        SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();

        MemoryStream ms = new MemoryStream();
        byte[] rgbIV = Encoding.ASCII.GetBytes("ryojvlzmdalyglrj");
        byte[] key = Encoding.ASCII.GetBytes("hcxilkqbbhczfeultgbskdmaunivmfuo");
        CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);

        cs.Write(clearTextBytes, 0, clearTextBytes.Length);

        cs.Close();

        return Convert.ToBase64String(ms.ToArray());
    }



加密的字符串存储在一个在线数据库。我需要能够做的是对Java应用程序,以便能够从数据库中读取字符串,并使用从C#应用程序相同的加密密钥解密串

The encrypted string is stored in an online database. What I need to be able to do is for the java application to be able to read the string from the database and decrypt the string using the same encryption keys from the C# application.

感谢您的帮助。

推荐答案

我个人喜欢 BouncyCastle的的Java加密。此代码(使用BouncyCastle的轻量级API)应该做的伎俩:

Personally, I like BouncyCastle for Java crypto. This code (using the BouncyCastle lightweight API) should do the trick:

String decrypt(byte[] cryptoBytes, byte[] key, byte[] iv) {
    BlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
    cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
    byte[] out = new byte[cipher.getOutputSize(cryptoBytes.length)];
    int offset = cipher.processBytes(cryptoBytes, 0, cryptoBytes.length, out, 0);
    cipher.doFinal(out, offset);
    return new String(out);
}



我觉得BouncyCastle的轻量级API比JCE提供东西,痛苦少,但你可以使用它,如果你想的供应商。

I find BouncyCastle's lightweight API to be less painful than the JCE provider stuff but you can use it as a provider if you wish.

它看起来像两个.NET SymmetricAlgorithm 和BC的 PaddedBufferedBlockCipher 默认为PKCS7填充,所以你应该使用默认值确定。

It looks like both the .net SymmetricAlgorithm and BC's PaddedBufferedBlockCipher default to PKCS7 padding so you should be OK with using the defaults.

这篇关于加密和解密使用的字符串C#的CryptoStream的一个Java equilavent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆