Reputation: 77
I have given a .key (abc.key) file. Using the above .key file, encryption and decryption is done in Java. Here i need to implement same encryption and decryption functionality using C#.net.
Please give me idea on how to crack this. I am a newbie to these cryptography stuff.
Some of parameters they are setting in java are below. hope it will give little idea
String keyFile="abc.key";
String keyAlgorithm= "AES";
String cipherTransformation="AES/CBC/PKCS5Padding";
String needtoEncStr = "Password1";
Any help will be greatly appreciated.
UPDATED :
@daveBM: I used bouncycastle but m getting different result(not same as java output). below is my complete code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
public class BCEngine
{
private readonly Encoding _encoding;
private readonly IBlockCipher _blockCipher;
private PaddedBufferedBlockCipher _cipher;
private IBlockCipherPadding _padding;
public BCEngine(IBlockCipher blockCipher, Encoding encoding)
{
_blockCipher = blockCipher;
_encoding = encoding;
}
public void SetPadding(IBlockCipherPadding padding)
{
if (padding != null)
_padding = padding;
}
public string Encrypt(string plain, string key)
{
byte[] result = BouncyCastleCrypto(true, _encoding.GetBytes(plain), key);
return Convert.ToBase64String(result);
}
public string Decrypt(string cipher, string key)
{
byte[] result = BouncyCastleCrypto(false, Convert.FromBase64String(cipher), key);
return _encoding.GetString(result);
}
/// <summary>
///
/// </summary>
/// <param name="forEncrypt"></param>
/// <param name="input"></param>
/// <param name="key"></param>
/// <returns></returns>
/// <exception cref="CryptoException"></exception>
private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input, string key)
{
try
{
_cipher = _padding == null ? new PaddedBufferedBlockCipher(_blockCipher) : new PaddedBufferedBlockCipher(_blockCipher, _padding);
byte[] keyByte = _encoding.GetBytes(key);
_cipher.Init(forEncrypt, new KeyParameter(keyByte));
return _cipher.DoFinal(input);
}
catch (Org.BouncyCastle.Crypto.CryptoException ex)
{
throw new CryptoException(ex.Message);
}
}
}
static void Main(string[] args)
{
Encoding _encoding;
IBlockCipherPadding _padding;
string key = "abc.key";
Stream inStr = null;
inStr = File.OpenRead(key);
Stream stream = inStr;
byte[] bytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(bytes, 0, (int)stream.Length);
string data1 = Encoding.UTF8.GetString(bytes); // this is your string.
_encoding = Encoding.ASCII;
Pkcs7Padding pkcs = new Pkcs7Padding();
_padding = pkcs;
BCEngine bcEngine = new BCEngine(new AesEngine(), _encoding);
bcEngine.SetPadding(_padding);
string data=bcEngine.Encrypt("AbcDefg12$", data1);
}
I am getting this output "S2jjvVJVKfGodPfMuI4v+g==" when my expected output is "3df36eb77ccfc05e264a6212c2db5380"... Please let me know what is going wrong.
Upvotes: 0
Views: 321
Reputation: 687
You can use this code that someone kindly posted in some other related question.
Encrypting & Decrypting a String in C#
What you need to do is use the method Encrypt(string plainText, string passPhrase) where the plainText is whatever you want to encrypt and the passPhrase is the content of your .key file.
By the way, just mentioning that you had a .key file was enough. We don't need to know the content :)..
Hope that helps
Upvotes: 1