Encrypt and decrypt file in J2ME - encryption

I'm having problem decrypting an encrypted file in J2ME using bouncy castle.
What I'm trying to do is select a file to encrypt,write the encrypted file and try decrypt it back to its orginal form (write to another file for verification purpose).
I have this error when reading the encrypted file.
Stack Trace :
s: pad block corrupted
at j.a(+219)
at e.c(+38)
at e.b(+30)
at com.aaron.midlets.BluetoothServerMidlet.c(+134)
at com.aaron.midlets.BluetoothServerMidlet.b(+161)
at com.aaron.midlets.BluetoothServerMidlet.a(+67)
at com.aaron.midlets.BluetoothServerMidlet.startApp(+105)
at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:43)
at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:374)
at com.sun.midp.main.Main.runLocalClass(Main.java:466)
at com.sun.midp.main.Main.main(Main.java:120)
Here are part of my code :
private void createEncryptFile() {
FileConnection fc = FileListingUtil.getFile("root1/", "test.encrypt");
try {
fc.create();
readAndEncrypt();
} catch (Exception e) {
}
}
private void readAndEncrypt() {
FileConnection fc = FileListingUtil.getFile("root1/", "test.original");
FileConnection fc2 = FileListingUtil.getFile("root1/", "test.encrypt");
try {
InputStream test = fc.openDataInputStream();
OutputStreamWriter output = new OutputStreamWriter(fc2.openOutputStream());
int fileSize = (int) fc.fileSize();
byte[] imgData = new byte[fileSize];
int bytesRead = 0;
while (bytesRead < fileSize) {
bytesRead += test.read(imgData, bytesRead, fileSize - bytesRead);
}
EncryptorUtil util = new EncryptorUtil("12345678");
try {
byte[] dataE = util.encrypt(imgData);
for (int y = 0; y < dataE.length; ++y) {
output.write((int) dataE[y]);
}
} catch (CryptoException ex) {
ex.printStackTrace();
}
test.close();
output.close();
createDecryptFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void createDecryptFile() {
FileConnection fc = FileListingUtil.getFile("root1/", "test.decrypt");
try {
fc.create();
readAndDecrypt();
} catch (Exception e) {
e.printStackTrace();
}
}
private void readAndDecrypt() {
FileConnection fc = FileListingUtil.getFile("root1/", "test.encrypt");
FileConnection fc2 = FileListingUtil.getFile("root1/", "test.decrypt");
try {
InputStream test = fc.openDataInputStream();
OutputStreamWriter output = new OutputStreamWriter(fc2.openOutputStream());
int fileSize = (int) fc.fileSize();
byte[] imgData = new byte[fileSize];
int bytesRead = 0;
while (bytesRead < fileSize) {
bytesRead += test.read(imgData, bytesRead, fileSize - bytesRead);
}
EncryptorUtil util = new EncryptorUtil("12345678");
try {
byte[] dataE = util.decrypt(imgData);
for (int y = 0; y < dataE.length; ++y) {
output.write((int) dataE[y]);
}
} catch (CryptoException ex) {
ex.printStackTrace();
}
test.close();
output.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
the last function will throw the exception.

I can see one problem. You write test.encrypt file as writer (which converts each byte into char, doubling it). You read it back as InputStream, which reads out bytes. So your encrypted data is corrupted.

Related

C# - TcpClient. Send data response after receive only works without iterate

My server receive some TCP/IP data and response to client:
private static void startClient(TcpClient clientSocket)
{
NetworkStream networkStream = null;
DateTime fecha = DateTime.Now;
try
{
clientSocket.NoDelay = true;
networkStream = clientSocket.GetStream();
string receiveData = readData(clientSocket);
string responseData = "abc"; //ProcessData(receiveData);
if (responseData != null)
writeData(networkStream, responseData);
}
catch (SocketException e)
{
throw;
}
finally
{
networkStream.Close();
}
}
private static string readData(TcpClient tcpClient)
{
try
{
var bytesFrom = new byte[tcpClient.ReceiveBufferSize];
StringBuilder dataFromClient = new StringBuilder();
int readCount;
while ((readCount = tcpClient.GetStream().Read(bytesFrom, 0, tcpClient.ReceiveBufferSize)) != 0)
{
dataFromClient.Append(Encoding.ASCII.GetString(bytesFrom, 0, readCount));
}
//int bytesRead = tcpClient.GetStream().Read(bytesFrom, 0, tcpClient.ReceiveBufferSize);
//string dataFromClient = Encoding.ASCII.GetString(bytesFrom, 0, bytesRead);
return dataFromClient.ToString();
}
catch (SocketException e)
{
throw;
}
}
private static void writeData(NetworkStream networkStream, string dataToClient)
{
Byte[] sendBytes = null;
try {
sendBytes = Encoding.ASCII.GetBytes(dataToClient);
networkStream.Write(sendBytes,0, sendBytes.Length);
networkStream.Flush();
}
catch(SocketException e)
{
throw;
}
}
With this solution the client never receives the response sent:
http://postimg.org/image/6srtslf2f/
However changing the reception to a single call to NetworkStream.Read ...
private static string readData(TcpClient tcpClient)
{
try
{
var bytesFrom = new byte[tcpClient.ReceiveBufferSize];
int bytesRead = tcpClient.GetStream().Read(bytesFrom, 0, tcpClient.ReceiveBufferSize);
string dataFromClient = Encoding.ASCII.GetString(bytesFrom, 0, bytesRead);
return dataFromClient.ToString();
}
catch (SocketException e)
{
throw;
}
}
... the client receives the response
http://postimg.org/image/uih9hadfr/
UPDATE
I found solution here. I´ve fixed it using 2/3 ways described . First handle EOS end message. Seccond is setting receive timeout if client works bad and sends bad data without EOS:
private static string readData(TcpClient tcpClient)
{
var clientStream = tcpClient.GetStream();
var dataFromClient = string.Empty;
var buffer = new byte[RECEIVE_BUFFER_SIZE];
if (!clientStream.CanRead)
return "";
tcpClient.ReceiveTimeout = RECEIVE_TIMEOUT;
try
{
int readCount;
while ((readCount = clientStream.Read(buffer, 0, buffer.Length)) != 0)
{
dataFromClient += Encoding.ASCII.GetString(buffer, 0, readCount);
if (dataFromClient.EndsWith(EOS))
return dataFromClient;
}
return dataFromClient.ToString();
}
catch (Exception ex)
{
var socketExept = ex.InnerException as SocketException;
if (socketExept != null && socketExept.SocketErrorCode == SocketError.TimedOut)
Logger.Warn(string.Format("Se excedio el timeout de recepcion: {0} ms",RECEIVE_TIMEOUT));
else
Logger.Error(string.Format("Error leyendo el mensaje recibido"), ex);
return dataFromClient.ToString();
}
}

AESEncoder class is not supported on j2me chinese phone

I am using this encryption class in a J2ME app. My J2ME application works fine on all Nokia devices. The app doesn't work on the Chinese MIw200 phone. Perhaps this cryptography is not supported on that phone? Is there another solution or any other method to encrypt and decrypt?
Please help me. Thanks a lot in advance.
My code is below:
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class AESEncoder {
private SecretKeySpec keyspec;
private Cipher cipher;
private String secretkey;
public AESEncoder(String secretkey) {
this.secretkey = secretkey;
keyspec = new SecretKeySpec(secretkey.getBytes(), 0, 16, "AES");
// keyspec=new SecretKeySpec(key, offset, len, secretkey);
try {
cipher = Cipher.getInstance("AES/ECB/NoPadding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
public byte[] encrypt(String text) throws Exception {
if (text == null || text.length() == 0) {
throw new Exception("Empty string");
}
int encrypted = 0;
byte[] bytenc = null;//new byte[32];
byte[] input = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, keyspec);
// byte empty[]=padString(text).getBytes();
// encrypted = cipher.doFinal(padString(text).getBytes());
// encrypted=cipher.doFinal(padString(text).getBytes(), 0, 0, padString(text).getBytes(), 0);
input = padString(text).getBytes();
bytenc = new byte[input.length];
encrypted = cipher.doFinal(input, 0, input.length, bytenc, 0);
String str = new String(bytenc, 0, encrypted);
// encrypted=cipher.update(padString(text).getBytes(), 0, 0, 0, 0);
// System.out.println("Encrypted is:>>" + str);
// bytenc=hexToBytes(String.valueOf(encrypted));
} catch (Exception e) {
throw new Exception("[encrypt] " + e.getMessage());
}
return bytenc;
}
public String encrypt_hsm(String text) throws Exception {
if (text == null || text.length() == 0) {
throw new Exception("Empty string");
}
String base64=null;
int encrypted = 0;
byte[] bytenc = null;//new byte[32];
byte[] input = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, keyspec);
// byte empty[]=padString(text).getBytes();
// encrypted = cipher.doFinal(padString(text).getBytes());
// encrypted=cipher.doFinal(padString(text).getBytes(), 0, 0, padString(text).getBytes(), 0);
input = padString(text).getBytes();
bytenc = new byte[input.length];
encrypted = cipher.doFinal(input, 0, input.length, bytenc, 0);
String str = new String(bytenc, 0, encrypted);
base64 = Base64.encode(bytenc);
// encrypted=cipher.update(padString(text).getBytes(), 0, 0, 0, 0);
// System.out.println("Encrypted is:>>" + str);
// bytenc=hexToBytes(String.valueOf(encrypted));
} catch (Exception e) {
throw new Exception("[encrypt] " + e.getMessage());
}
return base64;
}
public byte[] decrypt(String code) throws Exception {
if (code == null || code.length() == 0) {
throw new Exception("Empty string");
}
int decrypted = 0;
byte[] bytedec = null;
byte[] input = null;
try {
cipher.init(Cipher.DECRYPT_MODE, keyspec);
// input=hexToBytes(code);
input = Base64ToBytes(code);
bytedec = new byte[input.length];
decrypted = cipher.doFinal(input, 0, input.length, bytedec, 0);
String str = new String(bytedec, 0, decrypted);
// System.out.println("Decrypted is:>>" + str);
} catch (Exception e) {
throw new Exception("[decrypt] " + e.getMessage());
}
return bytedec;
}
public static String bytesToHex(byte[] bsData) {
int nDataLen = bsData.length;
String sHex = "";
for (int nIter = 0; nIter < nDataLen; nIter++) {
int nValue = (bsData[nIter] + 256) % 256;
int nIndex1 = nValue >> 4;
sHex += Integer.toHexString(nIndex1);
int nIndex2 = nValue & 0x0f;
sHex += Integer.toHexString(nIndex2);
}
return sHex;
}
public static byte[] hexToBytes(String str) {
if (str == null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i = 0; i < len; i++) {
buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
}
return buffer;
}
}
private static String padString(String source) {
char paddingChar = ' ';
int size = 32;
int x = source.length() % size;
int padLength = size - x;
for (int i = 0; i < padLength; i++) {
source += paddingChar;
}
// System.out.println("====>Pad String:" + source);
return source;
}
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
private byte[] Base64ToBytes(String code) {
code = code.replace('-', '+');
code = code.replace('_', '/');
code = code.replace(',', '=');
System.out.println("Final Base 64:"+code);
byte[] aesString = Base64.decode(code);
// System.out.println("Base64 after decoding:"+new String(aesString));
return aesString;
}
}
Please post a stack trace. Otherwise we are all just guessing.
AES is a required cipher, sometime between v4 and v7. Spend some time and confirm that this cipher isn't available on the MIw200.
Someone getting "AES not available" on a Mac got some suggestions.
Try allowing the system to fall back to 3DES or DES, instead of forcing AES. Here's an example that tests encryption with each algorithm.
Try using the BouncyCastle API. Here is a how-to guide.

wait() in asp.net (showing uploaded image)

I want to force my program to wait some moments after doing something, and then do somwthing else, in asp.net and silverlight.
(In Detail, I want to upload an image by silverlight program, and then show it in my page by an Image control. But when I upload images which their size is about 6KB or upper, the image doesn't show, however it has been uplaoded successfully. I think that waiting some moments may solve the problem)
May anyone guide me?
thank you
I use the code in this page
This is the code in MyPage.Xaml:
private void UploadBtn_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = UPLOAD_DIALOG_FILTER;
if ((bool)dlg.ShowDialog())
{
progressBar.Visibility = Visibility.Visible;
progressBar.Value = 0;
_fileName = dlg.File.Name;
UploadFile(_fileName, dlg.File.OpenRead());
}
else
{
//user clicked cancel
}
}
private void UploadFile(string fileName, Stream data)
{
// Just kept here
progressBar.Maximum = data.Length;
UriBuilder ub = new UriBuilder("http://mysite.com/receiver.ashx");
ub.Query = string.Format("fileName={0}", fileName);
WebClient c = new WebClient();
c.OpenWriteCompleted += (sender, e) =>
{
PushData(data, e.Result);
e.Result.Close();
data.Close();
};
try
{
c.OpenWriteAsync(ub.Uri);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void PushData(Stream input, Stream output)
{
byte[] buffer = new byte[input.Length];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
output.Write(buffer, 0, buffer.Length);
}
progressBar.Value += input.Length;
if (progressBar.Value >= progressBar.Maximum)
{
progressBar.Visibility = System.Windows.Visibility.Collapsed;
loadImage();
}
}
private void loadImage()
{
Uri ur = new Uri("http://mysite.com/upload/" + _fileName);
img1.Source = new BitmapImage(ur);
}
And this is receiver.ashx:
public void ProcessRequest(HttpContext context)
{
string filename = context.Request.QueryString["filename"].ToString(); using (FileStream fs = File.Create(context.Server.MapPath("~/upload/" + filename)))
{
SaveFile(context.Request.InputStream, fs);
}
}
private void SaveFile(Stream stream, FileStream fs)
{
byte[] buffer = new byte[stream.Length];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
{
fs.Write(buffer, 0, bytesRead);
}
}

blackberry HttpConnection.GET

URL: http://monSite/GET.asp
we must authenticate before getting the result.
I want to send the login and password with HttpConn.setRequestMethod (HttpConnection.POST) and retrieve the XML file with HttpConn.setRequestMethod (HttpConnection.GET) with the same HTTP Client.
conn = (HttpConnection) new ConnectionFactory().getConnection(_url).getConnection();
URLEncodedPostData postData = null;
postData = new URLEncodedPostData("UTF-8", false);
postData.append("userName",_username);
postData.append("passWord", _password);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LANGUAGE, "en-US");
conn.setRequestProperty(HttpProtocolConstants.HEADER_CACHE_CONTROL,"no-cache, no-store, no-transform");
// Specify the content type.
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, postData.getContentType());
byte [] postBytes = postData.getBytes();
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postBytes.length));
os = conn.openOutputStream();
os.write(postBytes);
os.flush();
os.close();
//GET XML file
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Confirguration/CLDC-1.0");
if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
int total = 0;
int size = 1024;
char[] buffer = new char[size];
int len;
InputStreamReader isr = new InputStreamReader(conn.openInputStream(), "UTF-8");
while ((len = isr.read(buffer, 0, size)) > 0)
{
buff.append(buffer, 0, len);
total += len;
}
result = buff.toString();
} else {
result = "Error in connection" + conn.getResponseCode();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
the HttpConnection.POST works very well but GET no (login failed: the authentication parameter does not saved)
In HttpConnection.GET request you need to append attribute in your url like:
String url="http://www.xyz?userName="+_username+"&password="+_password+"";
and then get the InputStream
below link may be helpful for you
http://supportforums.blackberry.com/t5/Java-Development/Make-an-HTTP-Connection-to-get-a-Content-of-URL/td-p/95075

How do encrypt a long or int using the Bouncy Castle crypto routines for BlackBerry?

How do encrypt/decrypt a long or int using the Bouncy Castle crypto routines for BlackBerry? I know how to encrypt/decrypt a String. I can encrypt a long but can't get a long to decrypt properly.
Some of this is poorly done, but I'm just trying stuff out at the moment.
I've included my entire crypto engine here:
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
public class CryptoEngine
{
// Global Variables
// Global Objects
private static AESFastEngine engine;
private static BufferedBlockCipher cipher;
private static KeyParameter key;
public static boolean setEncryptionKey(String keyText)
{
// adding in spaces to force a proper key
keyText += " ";
// cutting off at 128 bits (16 characters)
keyText = keyText.substring(0, 16);
keyText = HelperMethods.cleanUpNullString(keyText);
byte[] keyBytes = keyText.getBytes();
key = new KeyParameter(keyBytes);
engine = new AESFastEngine();
cipher = new PaddedBufferedBlockCipher(engine);
// just for now
return true;
}
public static String encryptString(String plainText)
{
try
{
byte[] plainArray = plainText.getBytes();
cipher.init(true, key);
byte[] cipherBytes = new byte[cipher.getOutputSize(plainArray.length)];
int cipherLength = cipher.processBytes(plainArray, 0, plainArray.length, cipherBytes, 0);
cipher.doFinal(cipherBytes, cipherLength);
String cipherString = new String(cipherBytes);
return cipherString;
}
catch (DataLengthException e)
{
Logger.logToConsole(e);
}
catch (IllegalArgumentException e)
{
Logger.logToConsole(e);
}
catch (IllegalStateException e)
{
Logger.logToConsole(e);
}
catch (InvalidCipherTextException e)
{
Logger.logToConsole(e);
}
catch (Exception ex)
{
Logger.logToConsole(ex);
}
// else
return "";// default bad value
}
public static String decryptString(String encryptedText)
{
try
{
byte[] cipherBytes = encryptedText.getBytes();
cipher.init(false, key);
byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
int decryptedLength = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
cipher.doFinal(decryptedBytes, decryptedLength);
String decryptedString = new String(decryptedBytes);
// crop accordingly
int index = decryptedString.indexOf("\u0000");
if (index >= 0)
{
decryptedString = decryptedString.substring(0, index);
}
return decryptedString;
}
catch (DataLengthException e)
{
Logger.logToConsole(e);
}
catch (IllegalArgumentException e)
{
Logger.logToConsole(e);
}
catch (IllegalStateException e)
{
Logger.logToConsole(e);
}
catch (InvalidCipherTextException e)
{
Logger.logToConsole(e);
}
catch (Exception ex)
{
Logger.logToConsole(ex);
}
// else
return "";// default bad value
}
private static byte[] convertLongToByteArray(long longToConvert)
{
return new byte[] { (byte) (longToConvert >>> 56), (byte) (longToConvert >>> 48), (byte) (longToConvert >>> 40), (byte) (longToConvert >>> 32), (byte) (longToConvert >>> 24), (byte) (longToConvert >>> 16), (byte) (longToConvert >>> 8), (byte) (longToConvert) };
}
private static long convertByteArrayToLong(byte[] byteArrayToConvert)
{
long returnable = 0;
for (int counter = 0; counter < byteArrayToConvert.length; counter++)
{
returnable += ((byteArrayToConvert[byteArrayToConvert.length - counter - 1] & 0xFF) << counter * 8);
}
if (returnable < 0)
{
returnable++;
}
return returnable;
}
public static long encryptLong(long plainLong)
{
try
{
String plainString = String.valueOf(plainLong);
String cipherString = encryptString(plainString);
byte[] cipherBytes = cipherString.getBytes();
long returnable = convertByteArrayToLong(cipherBytes);
return returnable;
}
catch (Exception e)
{
Logger.logToConsole(e);
}
// else
return Integer.MIN_VALUE;// default bad value
}
public static long decryptLong(long encryptedLong)
{
byte[] cipherBytes = convertLongToByteArray(encryptedLong);
cipher.init(false, key);
byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
int decryptedLength = cipherBytes.length;
try
{
cipher.doFinal(decryptedBytes, decryptedLength);
}
catch (DataLengthException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InvalidCipherTextException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
long plainLong = convertByteArrayToLong(decryptedBytes);
return plainLong;
}
public static boolean encryptBoolean(int plainBoolean)
{
return false;
}
public static boolean decryptBoolean(int encryptedBoolean)
{
return false;
}
public static boolean testLongToByteArrayConversion()
{
boolean returnable = true;
// fails out of the bounds of an integer, the conversion to long from byte
// array does not hold, need to figure out a better solution
for (long counter = -1000000; counter < 1000000; counter++)
{
long test = counter;
byte[] bytes = convertLongToByteArray(test);
long result = convertByteArrayToLong(bytes);
if (result != test)
{
returnable = false;
Logger.logToConsole("long conversion failed");
Logger.logToConsole("test = " + test + "\n result = " + result);
}
// regardless
}
// the end
Logger.logToConsole("final returnable result = " + returnable);
return returnable;
}
}
It's probably the conversion from long -> byte[] -> String, in particular the conversion from String back into byte[]. You don't pass in an encoding for String.getBytes() so it's going to be using the default character encoding, which may be altering your data.
My suggestion is to expose encrypt/decrypt methods that take byte[] as an argument, to avoid String/byte[] conversion.
Also, you may want to take a look at the native RIM AES classes, AESEncryptorEngine and AESDecryptorEngine which may be faster than BouncyCastle (since they're native APIs) and require less code.
I just realized that with strong ciphers, the result length is usually/always greater than the length of the original. If it wasn't, a lookup table could be used. As such, its not possible to encrypt a long into a long every time, especially if it uses all 64 bits. If this doesn't make sense, see me for more info:
How do I encrypt a string and get a equal length encrypted string?

Resources