is there AES source code (VB.NET) that decrypts AS3CRYPTO crypted msgs - asp.net

Is there a simple ASP.NET (.VB) available for AES-encrypting?
here is a link to c# one but its complicate one having pretty much parameters, for example salt.
[http://www.gutgames.com/post/AES-Encryption-in-C.aspx]
I need a simple one that works together with GOOGLEAS3 Class that is called easily like this:
var key:ByteArray = Hex.toArray("1234567890abcdef");
var pt:ByteArray = Hex.toArray( Hex.fromString("aaaaaaaaaaaaaaaaaaaa"));
var aes:AESKey = new AESKey(key);
aes.encrypt(pt);
var out:String = Hex.fromArray(pt).toUpperCase();

AES is built into the framework, as the Aes class in System.Security.Cryptography. There are two concrete implementations, one managed, one using the Windows Crypto Service Provider (faster, but not portable to other platforms). There's also an implementation of Rijndael, from which AES derives.
Remember you need an initialization vector, as well as the key to encrypt/decrypt, as it's a block cipher. If you encrypt without setting one a random one will be used, but you will need to store and retrieve it for decryption.
Example Code: (lifted from chapter 6 of my forthcoming book grin)
static byte[] Encrypt(byte[] clearText, byte[] key, byte[] iv)
{
// Create an instance of our encyrption algorithm.
RijndaelManaged rijndael = new RijndaelManaged();
// Create an encryptor using our key and IV
ICryptoTransform transform = rijndael.CreateEncryptor(key, iv);
// Create the streams for input and output
MemoryStream outputStream = new MemoryStream();
CryptoStream inputStream = new CryptoStream(
outputStream,
transform,
CryptoStreamMode.Write);
// Feed our data into the crypto stream.
inputStream.Write(clearText, 0, clearText.Length);
// Flush the crypto stream.
inputStream.FlushFinalBlock();
// And finally return our encrypted data.
return outputStream.ToArray();
}
and to decrypt
static byte[] Decyrpt(byte[] clearText, byte[] key, byte[] iv)
{
// Create an instance of our encryption algorithm.
RijndaelManaged rijndael = new RijndaelManaged();
// Create an decryptor using our key and IV
ICryptoTransform transform = rijndael.CreateDecryptor(key, iv);
// Create the streams for input and output
MemoryStream outputStream = new MemoryStream();
CryptoStream inputStream = new CryptoStream(
outputStream,
transform,
CryptoStreamMode.Write);
// Feed our data into the crypto stream.
inputStream.Write(clearText, 0, clearText.Length);
// Flush the crypto stream.
inputStream.FlushFinalBlock();
// And finally return our decrypted data.
return outputStream.ToArray();
}
replace the RijndaelManaged class with one of the AES classes and a suitable key.

Related

How to save the IV if using AES GCM for big files

i am trying to use AES GCM for decrypting files (small and large files) on Android.
I have tried to do it like this tutorial
https://proandroiddev.com/security-best-practices-symmetric-encryption-with-aes-in-java-7616beaaade9
But there is only written i should use CiperInputstream if i want to encrypt big files.
So this is the code now
SecureRandom secureRandom = new SecureRandom();
byte[] key = new byte[16];
secureRandom.nextBytes(key);
SecretKey secretKey = SecretKeySpec(key, "AES");
byte[] iv = new byte[12]; //NEVER REUSE THIS IV WITH SAME KEY
secureRandom.nextBytes(iv);
final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv); //128 bit auth tag length
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
byte[] cipherText = cipher.doFinal(plainText);
ByteBuffer byteBuffer = ByteBuffer.allocate(4 + iv.length + cipherText.length);
byteBuffer.putInt(iv.length);
byteBuffer.put(iv);
byteBuffer.put(cipherText);
byte[] cipherMessage = byteBuffer.array();
So now i have created the CiperInputStream, how to encrypt, add the IV and at least save the file?
FileInputStream inputStream = new FileInputStream(plainText);
CipherInputStream cis = new CipherInputStream(inputStream, cipherEnc);
Furthermore i want to upload the file, if it is possible without saving the ecnrypted file first.
Currently i upload unencrypted files to a webserver with a given link (seafile).
I just pass the path of the file to a function and then upload it with OkHttpClient

AES Encryption between C# and F5 load balancer / TCL

I would like to use encryption to send traffic to my F5 BIG IP load balancer and have it use its own native CRYPTO:: methods to decrypt a base64 encoded string.
I am able to encrypt and decrypt a string within the appliance and within a Visual Studio 2012 console application but I cannot decrypt an encrypted string in the opposing environment.
Any suggestion here as to how to get the following keys in a compatible format that CRYPTO or C# understands would go a long way!
// C# key and vector declaration:
private const string AesIV = #"!QAZ2WSX#EDC4RFV";
private const string AesKey = #"5TGB&YHN7UJM(IK<";
It appears that in CRYPTO:: it needs it in hex format, I tried to convert it as seen below but that didnt help me.
C# console app code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
namespace ssoconsole_encrypt
{
class Program
{
private const string AesIV = #"!QAZ2WSX#EDC4RFV";
private const string AesKey = #"5TGB&YHN7UJM(IK<";
// set key "abed1ddc04fbb05856bca4a0ca60f21e"
//set iv "d78d86d9084eb9239694c9a733904037"
// set key "56bca4a0ca60f21e"
// set iv "39694c9a73390403"
/// <summary>
/// AES Encryption
/// </summary>
///
static public string Encrypt(string text)
{
// AesCryptoServiceProvider
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.IV = Encoding.UTF8.GetBytes(AesIV);
aes.Key = Encoding.UTF8.GetBytes(AesKey);
string keyme = BitConverter.ToString(aes.Key);
string ivme = BitConverter.ToString(aes.IV);
Console.WriteLine(string.Format("The converted key is: {0}",keyme));
Console.WriteLine(string.Format("The converted iv is: {0}", ivme));
Console.WriteLine(System.Text.Encoding.UTF8.GetString(aes.Key));
// Thread.Sleep(10000);
//Console.WriteLine(aes.Key.ToString());
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// aes.Padding = PaddingMode.Zeros;
// Convert string to byte array
//byte[] src = Encoding.Unicode.GetBytes(text);
byte[] src = Encoding.UTF8.GetBytes(text);
// encryption
using (ICryptoTransform encrypt = aes.CreateEncryptor())
{
byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
// Convert byte array to Base64 strings
return Convert.ToBase64String(dest);
}
}
/// <summary>
/// AES decryption
/// </summary>
static public string Decrypt(string text)
{
// AesCryptoServiceProvider
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.IV = Encoding.UTF8.GetBytes(AesIV);
aes.Key = Encoding.UTF8.GetBytes(AesKey);
//aes.IV = Encoding.UTF8.GetBytes(#"01020304050607080900010203040506");
//aes.Key = Encoding.UTF8.GetBytes(#"01020304050607080900010203040506");
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// Convert Base64 strings to byte array
byte[] src = System.Convert.FromBase64String(text);
try
{
// decryption
using (ICryptoTransform decrypt = aes.CreateDecryptor())
{
byte[] dest = decrypt.TransformFinalBlock(src, 0, src.Length);
// return Encoding.Unicode.GetString(dest);
return Encoding.UTF8.GetString(dest);
}
}
catch (CryptographicException e)
{
return e.ToString();
}
}
static void Main()
{
string username = "jschoombee";
string encrypted = Encrypt(username);
string decrypted = Decrypt(encrypted);
// string decrypted = Decrypt("epvhTN55JnnVV9DBn1Cbsg==");
// string decrypted = Decrypt(encrypted);
Console.WriteLine(string.Format("jschoombee encrypted is : {0}",encrypted));
Console.WriteLine(string.Format("the Decrypted username for jp is : {0}", decrypted));
Thread.Sleep(1000000);
}
}
}
This is the Console Output:
The converted key is: 35-54-47-42-26-59-48-4E-37-55-4A-4D-28-49-4B-3C
The converted iv is: 21-51-41-5A-32-57-53-58-23-45-44-43-34-52-46-56
5TGB&YHN7UJM(IK<
jschoombee encrypted is : tGG9Un6VqcAOTQawlxwRXg==
the Decrypted username for jp is : jschoombee
This it the F5 / TCL code:
when RULE_INIT {
set static::hexkey "355447422659484E37554A4D28494B3C"
log local0.info"====Rule_Init===="
log local0.info "Key is $static::hexkey"
log local0.info"================="
}
when HTTP_REQUEST_DATA {
set iv "2151415A325753582345444334524656"
set text_to_encrypt "jschoombee"
set enc_out_no_binary [CRYPTO::encrypt -alg aes-256-cbc -keyhex $static::hexkey -ivhex $iv $text_to_encrypt]
set dec_in [CRYPTO::decrypt -alg aes-256-cbc -keyhex $static::hexkey -ivhex $iv $enc_out_no_binary]
log local0.info "The decrypted NO binary $dec_in"
log local0.info "The Encrypted NO binary Base64 is: [b64encode "$enc_out_no_binary"]"
binary scan $enc_out_no_binary H* enc_hex
log local0.info "The Encrypted NO binary Hex is: $enc_hex"
log local0.info "This is the IV $iv"
HTTP::release
}
The F5/TCL Output Log File:
Feb 11 13:05:45 AMS4-LB-01 info tmm1[9650]: <HTTP_REQUEST_DATA>: The decrypted NO binary jschoombee
Feb 11 13:05:45 AMS4-LB-01 info tmm1[9650]: <HTTP_REQUEST_DATA>: The Encrypted NO binary Base64 is: Rlz4cC9SlpRyON4cZI+dtQ==
Feb 11 13:05:45 AMS4-LB-01 info tmm1[9650]: <HTTP_REQUEST_DATA>: The Encrypted NO binary Hex is: 465cf8702f5296947238de1c648f9db5
Feb 11 13:05:45 AMS4-LB-01 info tmm1[9650]: <HTTP_REQUEST_DATA>: This is the IV 2151415A325753582345444334524656
There are some very strange things happening in your code regarding the key and IV.
First of all the key you've specified is 16 characters. In UTF-8 those result in 16 bytes. You are however specifying a key of 32 bytes (256 bits) in your C# code. Also be warned that many libraries (incorrectly) use AES-256 to mean Rijndael with a 256 bit block size. It's probably better to just use AES-128 and focus on making your protocol and code secure.
Second, a key can never be a character string. A character string normally is restricted with regards to which values can be used. E.g. control codes cannot be entered. This means that your key will never reach its intended strength. If you want to use a static key, you should specify it in hexadecimals as you do in your F5 code.
A static IV does not make much sense. The whole idea of the IV is to make sure that you will generate a different ciphertext if you encrypt a block with a value already processed. So please use a random IV, and place it in front of your ciphertext.
You seem to have the hang on using encoding/decoding on your plaintext (UTF-8) and ciphertext (Base 64). So please try and follow the advice given above and try again.

Encrypt in .Net and decrypt in AS3

I need to encrypt some files in ASP.net and decrypt them in a flash application built with Action Script 3.
AS3 developer found a lib call AS3crypto which seems like a good one for AS3. The idea is encrypt and decrypt using same key. Symmetrical Encryption?
But I am struggling to find .Net equivalent that would use same algorithm for encryption.
I have tried RC4 example from 4guysfromrolla blog which works too slow for me.
I have tried AES on this example (http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(v=vs.100).aspx) which works great on .Net but I can't seem to decrypt using AS3crypto to get the same file back. AS3crypto doesn't seem to like to have IV for decryption. I can only supply one key.
So far I am lost. How can I encrypt a file in .Net and decrypt it back in AS3 to get the same file back?
Notice: use 16 char length for both Key and IV, ex: Key: 1234567890123456 and IV: 9876543210654321
Here is C# code
public byte[] Encrypt(byte[] someData, string KEY, string IV)
{
//preparing
byte[] keyBytes = Encoding.UTF8.GetBytes(KEY);
byte[] ivBytes = Encoding.UTF8.GetBytes(IV);
//here goes encryption
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Key = keyBytes;
rijndaelManaged.IV = ivBytes;
rijndaelManaged.BlockSize = 128;
rijndaelManaged.Mode = CipherMode.CBC;
ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);
byte[] result = null;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(someData, 0, someData.Length);
cryptoStream.FlushFinalBlock();
result = memoryStream.ToArray();
}
}
return result;
}
And here is AS3 code using AS3Crypto library
private function decrypt(input:ByteArray, decrKey:String, decrIV:String):ByteArray
{
var key:ByteArray = Hex.toArray(Hex.fromString(decrKey));
var pad:IPad = new NullPad();
var aes:ICipher = Crypto.getCipher("aes-cbc", key, pad);
var ivmode:IVMode = aes as IVMode;
ivmode.IV = Hex.toArray(Hex.fromString(decrIV));
aes.decrypt(input);
return input;
}

RSA Decrypt returns different byte[] results for same we

As illustrated in http://msdn.microsoft.com/en-us/library/vstudio/system.security.cryptography.rsacryptoserviceprovider.encrypt(v=vs.90).aspx I am trying to use an RSA key that is stored in a User Container to encrypt and decrypt a RijndaelManaged Key and IV that is stored in an app.config file (in an NT service).
To create the encrypted keys I did the following and then added the strings to the config file
CspParameters cp = new CspParameters();
cp.KeyContainerName = "ContainerName";
// Get the existing or create a new RSA Key
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
//Create a new instance of the RijndaelManaged class.
RijndaelManaged RM = new RijndaelManaged();
//Encrypt the symmetric key and IV.
byte[] EncryptedSymmetricKey = rsa.Encrypt(RM.Key, false);
byte[] EncryptedSymmetricIV = rsa.Encrypt(RM.IV, false);
string configKey = Convert.ToBase64String(EncryptedSymmetricKey));
string configIV = Convert.ToBase64String(EncryptedSymmetricIV));
When I go to use the Key and IV from the config file, I perform the following:
//Get the existing RSA Key from the USER Container identified by Provider in the appsettings
CspParameters cp = new CspParameters();
cp.KeyContainerName = "ContainerName";
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp))
{
//Decrypt the RijndaelManaged KEY and IV from the config file using the RSA Key
byte[] Encrypted_RM_Key = Convert.FromBase64String(AppSettings["configKey"]);
byte[] Encrypted_RM_IV = Convert.FromBase64String(AppSettings["configIV"]);
byte[] Decrypted_RM_Key = rsa.Decrypt(Encrypted_RM_Key, false);
byte[] Decrypted_RM_IV = rsa.Decrypt(Encrypted_RM_IV, false);
//Encrypt the file using RijndaelManaged
RijndaelManaged RM = new RijndaelManaged();
RM.Key = Decrypted_RM_Key;
RM.IV = Decrypted_RM_IV;
....
}
While the service is running RM.Key and RM.IV stay the same. If the service is restarted, the resulting byte arrays in RM.IV and RM.Key are different which causes any decryption attempt on data that was encrypted before the service was restarted to fail with a Padding Invalid error.
Question: If I am using the same RSA key on the same encrypted data from the config file, why are the resulting Key and IV values different when the service is restarted?
Note: This also happens if I decrypt the values in my OnStart() service method and then try to decrpyt the same values in a dll that has been added to the project.
Wild guess: is your service running as the user for which you stored the keys in the container? You can add some traces along the way (or debug) and see whether you get the same RSA key every time.

Padding is invalid and cannot be removed with Rijndael decryption

I am seeing the "Padding is invalid and cannot be removed" error when I call the method below to decrypt the string from a windows application. String was encrypted from an asp.net application. Both application references the same assembly. I am able encrypt and decrypt with out any problem from the asp.net application. Here is the main code where I do the encryption and decryption.
private static byte[] EncryptHelper(byte[] arrData, string Password, bool Encrypt)
{
//Create the SymetricAlgorithem object
SymmetricAlgorithm myAlg = new RijndaelManaged();
//define a salt value to derive the key.
byte[] salt = System.Text.Encoding.ASCII.GetBytes("hjkhj877ffasah");
//Instantiate Rfc2898DeriveBytes with the password and salt.
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(Password, salt);
myAlg.Key = key.GetBytes(myAlg.KeySize / 8);
myAlg.IV = key.GetBytes(myAlg.BlockSize / 8);
myAlg.Padding = PaddingMode.PKCS7;
//Create the ICryptoTransform Object
ICryptoTransform encrytptor = Encrypt ? myAlg.CreateEncryptor() : myAlg.CreateDecryptor();
//Create Memorystream to write the encrypted data
using (MemoryStream aStream = new MemoryStream())
{
//Create the CryptoStream Ojbect using the aStream object
using (CryptoStream encryptStream = new CryptoStream(aStream, encrytptor, CryptoStreamMode.Write))
{
//Write the contents to crypto stream
encryptStream.Write(arrData, 0, arrData.Length);
//Flush the cryptostream
encryptStream.FlushFinalBlock();
//Reposition the memorystream to write the contents to an array.
aStream.Position = 0;
}
aStream.Flush();
//Convert to an array and return
return aStream.ToArray();
}
}
This is the method I use to convert the plain text from/to byte array
private static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
private static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
For persist the cipher text to database I use Convert.ToBase64String() and Convert.FromBase64String. Is the problem is with the way I use Rfc2898DeriveBytes class?
Well I think it's important to mention that from a security perspective, you are going to have the same IV for every message with the same password, and a predictable IV is a really big no no.
After that point I kinda don't want to look at it more to see what's going wrong, there are a lot of really bad cut and paste C# encryption on stackoverflow, and they just sit there with no mechanism for update, no one looking at them again except for people finding them to cut and paste again.
Look at Modern Examples of Symmetric Authenticated Encryption of a string. c#.
I try to keep it up to date and reviewed.

Resources