Skip to content

Instantly share code, notes, and snippets.

@jafetsanchez
Created July 13, 2011 11:31
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save jafetsanchez/1080133 to your computer and use it in GitHub Desktop.
Save jafetsanchez/1080133 to your computer and use it in GitHub Desktop.
Rijndael on C# and Java
#region Using
using System;
using System.Security.Cryptography;
using System.Text;
#endregion
namespace YourApp.Security.Cryptography
{
/// <summary>
/// Rijndael Encryptor / Decryptor Helper
///
/// <remarks>
/// Created by: Jafet Sanchez
/// Last Update: [date],[author],[description]
///
public class RijndaelCrypt
{
#region Private/Protected Member Variables
/// <summary>
/// Decryptor
///
private readonly ICryptoTransform _decryptor;
/// <summary>
/// Encryptor
///
private readonly ICryptoTransform _encryptor;
/// <summary>
/// 16-byte Private Key
///
private static readonly byte[] IV = Encoding.UTF8.GetBytes("ThisIsUrPassword");
/// <summary>
/// Public Key
///
private readonly byte[] _password;
/// <summary>
/// Rijndael cipher algorithm
///
private readonly RijndaelManaged _cipher;
#endregion
#region Private/Protected Properties
private ICryptoTransform Decryptor { get { return _decryptor; } }
private ICryptoTransform Encryptor { get { return _encryptor; } }
#endregion
#region Private/Protected Methods
#endregion
#region Constructor
/// <summary>
/// Constructor
///
/// <param name="password">Public key
public RijndaelCrypt(string password)
{
//Encode digest
var md5 = new MD5CryptoServiceProvider();
_password = md5.ComputeHash(Encoding.ASCII.GetBytes(password));
//Initialize objects
_cipher = new RijndaelManaged();
_decryptor = _cipher.CreateDecryptor(_password, IV);
_encryptor = _cipher.CreateEncryptor(_password, IV);
}
#endregion
#region Public Properties
#endregion
#region Public Methods
/// <summary>
/// Decryptor
///
/// <param name="text">Base64 string to be decrypted
/// <returns>
public string Decrypt(string text)
{
try
{
byte[] input = Convert.FromBase64String(text);
var newClearData = Decryptor.TransformFinalBlock(input, 0, input.Length);
return Encoding.ASCII.GetString(newClearData);
}
catch (ArgumentException ae)
{
Console.WriteLine("inputCount uses an invalid value or inputBuffer has an invalid offset length. " + ae);
return null;
}
catch (ObjectDisposedException oe)
{
Console.WriteLine("The object has already been disposed." + oe);
return null;
}
}
/// <summary>
/// Encryptor
///
/// <param name="text">String to be encrypted
/// <returns>
public string Encrypt(string text)
{
try
{
var buffer = Encoding.ASCII.GetBytes(text);
return Convert.ToBase64String(Encryptor.TransformFinalBlock(buffer, 0, buffer.Length));
}
catch (ArgumentException ae)
{
Console.WriteLine("inputCount uses an invalid value or inputBuffer has an invalid offset length. " + ae);
return null;
}
catch (ObjectDisposedException oe)
{
Console.WriteLine("The object has already been disposed." + oe);
return null;
}
}
#endregion
}
}
package com.yourpackage.security;
import android.util.Log;
import android.util.Base64;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class RijndaelCrypt {
public static final String TAG = "YourAppName";
private static String TRANSFORMATION = "AES/CBC/PKCS7Padding";
private static String ALGORITHM = "AES";
private static String DIGEST = "MD5";
private static Cipher _cipher;
private static SecretKey _password;
private static IvParameterSpec _IVParamSpec;
//16-byte private key
private static byte[] IV = "ThisIsUrPassword".getBytes();
/**
Constructor
@password Public key
*/
public RijndaelCrypt(String password) {
try {
//Encode digest
MessageDigest digest;
digest = MessageDigest.getInstance(DIGEST);
_password = new SecretKeySpec(digest.digest(password.getBytes()), ALGORITHM);
//Initialize objects
_cipher = Cipher.getInstance(TRANSFORMATION);
_IVParamSpec = new IvParameterSpec(IV);
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "No such algorithm " + ALGORITHM, e);
} catch (NoSuchPaddingException e) {
Log.e(TAG, "No such padding PKCS7", e);
}
}
/**
Encryptor.
@text String to be encrypted
@return Base64 encrypted text
*/
public String encrypt(byte[] text) {
byte[] encryptedData;
try {
_cipher.init(Cipher.ENCRYPT_MODE, _password, _IVParamSpec);
encryptedData = _cipher.doFinal(text);
} catch (InvalidKeyException e) {
Log.e(TAG, "Invalid key (invalid encoding, wrong length, uninitialized, etc).", e);
return null;
} catch (InvalidAlgorithmParameterException e) {
Log.e(TAG, "Invalid or inappropriate algorithm parameters for " + ALGORITHM, e);
return null;
} catch (IllegalBlockSizeException e) {
Log.e(TAG, "The length of data provided to a block cipher is incorrect", e);
return null;
} catch (BadPaddingException e) {
Log.e(TAG, "The input data but the data is not padded properly.", e);
return null;
}
return Base64.encodeToString(encryptedData,Base64.DEFAULT);
}
/**
Decryptor.
@text Base64 string to be decrypted
@return decrypted text
*/
public String decrypt(String text) {
try {
_cipher.init(Cipher.DECRYPT_MODE, _password, _IVParamSpec);
byte[] decodedValue = Base64.decode(text.getBytes(), Base64.DEFAULT);
byte[] decryptedVal = _cipher.doFinal(decodedValue);
return new String(decryptedVal);
} catch (InvalidKeyException e) {
Log.e(TAG, "Invalid key (invalid encoding, wrong length, uninitialized, etc).", e);
return null;
} catch (InvalidAlgorithmParameterException e) {
Log.e(TAG, "Invalid or inappropriate algorithm parameters for " + ALGORITHM, e);
return null;
} catch (IllegalBlockSizeException e) {
Log.e(TAG, "The length of data provided to a block cipher is incorrect", e);
return null;
} catch (BadPaddingException e) {
Log.e(TAG, "The input data but the data is not padded properly.", e);
return null;
}
}
}
@seyedhani
Copy link

It was perfect. Thank a lot! I used it for connection between my Android App and C# Server.

@vnavarro
Copy link

Thx 👍

@BeyMelamed
Copy link

Another Thx :)

@samerzmd
Copy link

Another Thx :P

@arashrasoulzadeh
Copy link

thanks men

@cherno-byl
Copy link

cherno-byl commented Oct 10, 2017

I'm sorry, I'm new to programming and a little bit slow. I want to use this in Android (Java). So how exactly do you use this in LoginActivity? For simple example, say I only have EditText for password and Button for encrypt. When I click encryptButton, I want the return result of String encrypt(byte[]) to show in my Log. How do I achieve this ? What do I have to code in my activity ? Thanks in advance

@luizapata98
Copy link

para cuando en TRANSACT-SQL ?? :,v

@ahmadzulhilmi1990
Copy link

Hi Guys,
function decrypt System.Security.Cryptography.RijndaelManaged and sample java above provide diff. value

Thanks in advance

@insidERR82
Copy link

Thank you @jafetsanchez ,
i use it to communicate between my Android App and Server written in VB.net (had to translate it first).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment