AES Encrypt in C# (Help) and Decrypt in Java (Done)

匿名 (未验证) 提交于 2019-12-03 02:27:02

问题:

We have a .NET application that needs to pass some data over to a Java app. We are wanting to give it some simple encryption, M.C. Hawking wont be hacking in but it needs to not be plain text.

I found some great Java code that lets me encrypt/decrypt using AES. What I am hoping to find is the matching peice of this for C# that will let me Encrypt a string that will be decryptable with my Java routine.

Here is my java class:

class SimpleProtector {        private final String ALGORITHM = "AES";        private final byte[] keyValue = new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };         public String encrypt(String valueToEnc) throws Exception    {               Key key = generateKey();               Cipher c = Cipher.getInstance(ALGORITHM);               c.init(Cipher.ENCRYPT_MODE, key);               byte[] encValue = c.doFinal(valueToEnc.getBytes());               String encryptedValue = new BASE64Encoder().encode(encValue);               return encryptedValue;    }         public String decrypt(String encryptedValue) throws Exception    {               Key key = generateKey();               Cipher c = Cipher.getInstance(ALGORITHM);               c.init(Cipher.DECRYPT_MODE, key);               byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);              byte[] decValue = c.doFinal(decordedValue);               String decryptedValue = new String(decValue);               return decryptedValue;        }         private Key generateKey() throws Exception    {               Key key = new SecretKeySpec(keyValue, ALGORITHM);               return key;        } } 

Thanks for any tips!

回答1:

See answers to this question: Using AES encryption in C#



回答2:

we can do the encrypt/decrypt in c# and the other in java without using any other third party api. Please go to the link given below, its pretty easy. http://zenu.wordpress.com/2011/09/21/aes-128bit-cross-platform-java-and-c-encryption-compatibility/



回答3:

The only way I have found to encrypt/decrypt in Java and then have C# do the other is to use bouncy castle's API (http://www.bouncycastle.org/documentation.html).

If you want to encrypt and make it harder to break, your best bet is to zip it first, then encrypt, as you then are encrypting something that looks more random.

For a discussion of using AES in C# and Java you can look at this message: http://forums.sun.com/thread.jspa?threadID=603209

You will want to use BouncyCastle to also serialize/deserialize the encryption key, and for security you may want to use RSA to have it encrypted by one side and decrypted by the other language, again using BouncyCastle to serialize/deserialize the key.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!