Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
@seyedhani

seyedhani Mar 2, 2015

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

seyedhani commented Mar 2, 2015

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

@vnavarro

This comment has been minimized.

Show comment
Hide comment
@vnavarro

vnavarro commented Apr 28, 2015

Thx 👍

@BeyMelamed

This comment has been minimized.

Show comment
Hide comment
@BeyMelamed

BeyMelamed Dec 18, 2015

Another Thx :)

BeyMelamed commented Dec 18, 2015

Another Thx :)

@samerzmd

This comment has been minimized.

Show comment
Hide comment
@samerzmd

samerzmd Apr 10, 2016

Another Thx :P

samerzmd commented Apr 10, 2016

Another Thx :P

@arashrasoulzadeh

This comment has been minimized.

Show comment
Hide comment
@arashrasoulzadeh

arashrasoulzadeh commented Jul 20, 2017

thanks men

@cherno-byl

This comment has been minimized.

Show comment
Hide comment
@cherno-byl

cherno-byl 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

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

This comment has been minimized.

Show comment
Hide comment
@luizapata98

luizapata98 Nov 27, 2017

para cuando en TRANSACT-SQL ?? :,v

luizapata98 commented Nov 27, 2017

para cuando en TRANSACT-SQL ?? :,v

@ahmadzulhilmi1990

This comment has been minimized.

Show comment
Hide comment
@ahmadzulhilmi1990

ahmadzulhilmi1990 Aug 29, 2018

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

Thanks in advance

ahmadzulhilmi1990 commented Aug 29, 2018

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

Thanks in advance

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