Encryption Sample Code
Encryption Sample Code
Encryption Sample Code
namespace AES_Example
{
class Program
{
static void Main(string[] args)
{
try
{
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[]
IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged aesAlg = new AesManaged())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
return plaintext;
}
}
}
Android Sample code
public class CryptLib {
/**
*/
ENCRYPT, DECRYPT;
Cipher _cx;
_cx = Cipher.getInstance("AES/CBC/PKCS5Padding");
/**
* @param inputString
try {
.getInstance(MD5);
digest.update(inputString.getBytes());
h = "0" + h;
hexString.append(h);
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
/**
*
* @param _inputText
* @param _encryptionKey
* @param _mode
* @param _initVector
* Initialization vector
* @throws UnsupportedEncodingException
* @throws InvalidKeyException
* @throws InvalidAlgorithmParameterException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
InvalidKeyException, InvalidAlgorithmParameterException,
IllegalBlockSizeException, BadPaddingException {
//_encryptionKey = md5(_encryptionKey);
//System.out.println("key="+_encryptionKey);
len = _key.length;
int ivlen = _initVector.getBytes("UTF-8").length;
ivlen = _iv.length;
//_keyGen.init(128);
// for the
// specified key
// data and
// algorithm
// name.
// IvParameterSpec
// specified buffer
// iv used as
// initialization
// vector.
// encryption
if (_mode.equals(EncryptMode.ENCRYPT)) {
// Potentially insecure random numbers on Android 4.3 and older.
// Read
// https://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html
// multi-part
// transformation
// (encryption)
// output
// decryption
if (_mode.equals(EncryptMode.DECRYPT)) {
Base64.DEFAULT);
// multi-part
// transformation
// (decryption)
System.out.println(_out);
}
/***
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static String SHA256 (String text, int length) throws NoSuchAlgorithmException,
UnsupportedEncodingException {
String resultStr;
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(text.getBytes("UTF-8"));
//return result.toString();
resultStr = result.toString();
else
{
return resultStr;
/***
* This function encrypts the plain text to cipher text using the key
* @param _plainText
* @param _key
* Encryption Key. You'll have to use the same key for decryption
* @param _iv
* initialization Vector
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws InvalidAlgorithmParameterException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
BadPaddingException {
/***
* This funtion decrypts the encrypted text to plain text using the key
* provided. You'll have to use the same key which you used during
* encryprtion
* @param _encryptedText
* @param _key
* @param _iv
* initialization Vector
* @throws InvalidKeyException
* @throws UnsupportedEncodingException
* @throws InvalidAlgorithmParameterException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException {
/**
* @param length
* Desired length
* * @return
*/
ranGen.nextBytes(aesKey);
if(length> result.toString().length())
return result.toString();
else
Input
CryptLib _crypt = new CryptLib();
String iv = "gqLOHUioQ0QjhuvI";
let enc = try AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt((data?.arrayOfBytes())!)
return result
let dec = try AES(key: key, iv: iv, blockMode: .CBC, padding: PKCS7()).decrypt((data?.arrayOfBytes())!)
return String(result!)
Input