开发者

Encryption between Android and C#

I am using the following C# source code to encrypt plain text using AES (ECB 256):

public static string Encode(string PlainText)
{
 byte[] Key = ASCIIEncoding.UTF8.GetBytes("12345678901234567890123456789012");
 string encrypted = null;


 RijndaelManaged rj = new RijndaelManaged();
 rj.BlockSize = 256;
 rj.KeySize = 256;
 rj.Key = Key;
 rj.GenerateIV();

 byte[] IV  = rj.IV;
 rj.Mode    = CipherMode.ECB;
 rj.Padding = PaddingMode.Zeros;

 try
 {
  using (MemoryStream ms = new MemoryStream())
  {
   using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
   {
    using (StreamWriter sw = new StreamWriter(cs))
    {
     sw.Write(PlainText);
     sw.Close();
     sw.Dispose();
    }

    cs.Close();
    cs.Dispose();
   }

   byte[] encryptArray = ms.ToArray();

   encrypted = (Convert.ToBase64String(encryptArray));

   ms.Close();
   ms.Dispose();
  }
 }
 catch (Exception ex)
 {
  throw ex;
 }

 finally
 {
  rj.Clear();
 }

 return encrypted;
}

And I need decrypt/encrypt data through same algorithm, but I don't know how.

Here my Java Class (not working):

 public static String encrypt(byte[] key, String cleartext, boolean base64) throws Exception
 {
  byte[] rawKey   = key;
  byte[] result   = encrypt(rawKey, cleartext.getBytes());

  // Base 64
  if (base64)
  开发者_运维问答 return toBase64(result);

  // Hex
  return toHex(result);
 }

 public static String decrypt(byte[] key, String encrypted)
   throws Exception
 {
  byte[] rawKey = key;
  byte[] enc    = toByte(encrypted);
  byte[] result = decrypt(rawKey, enc);

  return new String(result);
 }

 private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception
 {
  SecretKeySpec skeySpec          = new SecretKeySpec(raw, "AES");
  Cipher cipher                   = Cipher.getInstance("AES/ECB/NoPadding");

  cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

  byte[] encrypted = cipher.doFinal(clear);

  return encrypted;
 }

Java call:

encrypt("12345678901234567890123456789012".getBytes(), "Example Message", true);

I dont know how I can select the block size or the PaddingMode.Zeros in Java.

¿Any idea?

Thanks in advance


Yes, the problem is the limit of 128 bits block size in AES (see 'Strong Versus Unlimited Strength Cryptography').

Finally, I've used GNU Crypto and it works!. I've imported all source code and I've deleted the code that I'm not using.

If somebody wants the cleaned source code he only have to ask me.

Thanks for the help.


You should also consider Bouncy Castle, it's available for both C# and Java

http://www.bouncycastle.org


Reading over this article, it seems you might need to use a version of Java that allows unlimited strength cryptography so that you can use large key sizes (AES-192 and AES-256). They intentionally limit the key lengths that can be used by default due to import-control restrictions imposed by some countries. See the 'Strong Versus Unlimited Strength Cryptography' section for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜