A community member has associated this post with a similar question:
Partial encryption of a binary file
Only moderators can edit this content.
How to convert CHS to Logical Block Addressing LBA in C#?
I have been researching on how LBA Logical Block Addressing works, and how is it been calculated?
Maybe C# would be a great programming language for this task.
If not, then maybe C++ would technically be a better programming language for this task, when it comes to signed or unsigned integers for LBA.
Regards,
John-1477
C#
C++
-
John 406 Reputation points
2021-06-30T00:42:38.93+00:00 Its primary reason applies to optical media, and why it is important for me to know.
-
Timon Yang-MSFT 9,571 Reputation points
2021-06-30T06:30:57.597+00:00 @John
CHS to LBA seems to have a formula:
LBA = (C × HPC + H) × SPT + (S-1)
This seems to have nothing to do with language, you can use your favorite language.
C# can also be used when it comes to signed and unsigned integers.
Integral numeric types (C# reference) -
John 406 Reputation points
2021-06-30T11:00:16.987+00:00 Okay. When declaring variables, is the LBA address parameter or variable is declared as byte or as signed or unsigned integer?
That would need to be equivalent to the hex address while viewing them in any image format.
This, in addition, would apply to real world applications in hex editing.
For example, 1 platter is equal to 1 optical medium.Finally, how would users figure out the following steps:
- convert hex to decimal
- find out its logical block address range in decimal format.
- determine the range from start to end of its logical block address.
This would be important for me to know.
-
John 406 Reputation points
2021-06-30T11:17:00.123+00:00 Here are some real world examples when it comes to bytes per block:
For audio layout: It is 2,352 bytes per block.
If the number of bytes variable is declared as long or as integer, then LBA 1 = 2352 bytes as integer or long.Am I correct?
-
AgaveJoe 26,186 Reputation points
2021-06-30T12:02:06.087+00:00 Hexadecimal (HEX) is a Base16 numbering system. There's nothing to convert to decimal (Base10)- it is a number. Typically, when you are working with HEX and/or binary you stay in HEX and binary formats. It would be very confusing, especially working with addresses, to convert HEX to decimal symbols because all your logical math and calculations will be in hexadecimal. You'll lose all the bits representations in decimal format.
Signed binary is just a method to split a binary number into positive and negative values. The most significant bit (MSB) is the signed bit. A zero indicates a positive value while a one is a negative value. Addresses must be unsigned because the MSB is part of the address. Typically, address are calculated with logical operators not mathematical operators.
These concepts are covered is every beginning level computer science and engineering courses.
-
AgaveJoe 26,186 Reputation points
2021-06-30T12:18:45.96+00:00 Let's use a fictional addressing scheme that starts at zero and goes to 2352 bytes. The address range of each byte is 0x0000 - 0x092F. 0x0000 is the address of the first byte and 0x092F (2351) is the address of the last byte. 16-bits can easily handle the entire address space of this fictional example.
Logical block addressing (LBA) is a little more complex as the address is divided into more than just the physical byte address like the simple example above.
-
John 406 Reputation points
2021-06-30T15:47:30.82+00:00 Here is my possible code snippet example:
using System.IO; using System.Windows.Forms; using System.Security.Cryptography; using System; namespace Binary_File_Encryptor_Build_001 { public partial class Binary_Encryptor_Form : Form { Aes aesEncrypt; Random randomX; long LBA_Adress_Range; long Beginning_LBA; long Ending_LBA; long Post_Randomized_Credentials;
-
John 406 Reputation points
2021-06-30T15:48:25.757+00:00 And here is the button on click event:
private void Encrypt_Region_Button_Click(object sender, EventArgs e) { Beginning_LBA = Beginning_LBA_Text_Box.Text.Length; Ending_LBA = Ending_LBA_Text_Box.Text.Length; LBA_Adress_Range = Ending_LBA - Beginning_LBA; aesEncrypt.CreateEncryptor(); const string Pre_Random_Credentials= ""; int Encryptlength = Pre_Random_Credentials.Length; Post_Randomized_Credentials = randomX.Next(0, Encryptlength); }
-
John 406 Reputation points
2021-06-30T15:51:07.147+00:00 ...and here is the form with two buttons, two text boxes, and two labels.
-
AgaveJoe 26,186 Reputation points
2021-06-30T16:02:44.497+00:00 Ummm yeah, LBA code is found in a device driver and/or firmware not a Windows Form.
Your code snippet appears to be some kind of buggy encryption. Length??? Simply encrypt the data and use the System.IO namespace to write the encrypted byte array to whatever media device you are using.
-
John 406 Reputation points
2021-07-01T00:13:43.98+00:00 Should I use a "for" looping block structure?
I would need to use AES-256 bit encryption cipher and encrypt part of its binary file; while I would
need to randomize its password length. Right now, in that code snippet, its credentials are assigned
to as blank with quotes.Still, I would need to include a "for" looping structure within the click event.
In addition, would I need to include a binary reader and binary writer type, or an alternate file stream
type? -
John 406 Reputation points
2021-07-01T01:23:14.03+00:00 Here's what I have done so far. I've updated my variable declarations below.
Aes aesEncrypt; Random randomX; long LBA_Adress_Range; long Beginning_LBA; long Ending_LBA; long Post_Randomized_Credentials; OpenFileDialog dlg; SaveFileDialog savDlg; string file_Name;
-
John 406 Reputation points
2021-07-01T01:24:21.35+00:00 The click event below:
{ Beginning_LBA = Beginning_LBA_Text_Box.Text.Length; Ending_LBA = Ending_LBA_Text_Box.Text.Length; LBA_Adress_Range = Ending_LBA - Beginning_LBA; aesEncrypt.CreateEncryptor(); const string Pre_Random_Credentials= ""; int Encryptlength = Pre_Random_Credentials.Length; Post_Randomized_Credentials = randomX.Next(0, Encryptlength); for (long i = (long)Beginning_LBA; i< Ending_LBA; i++) { Encryptlength = Pre_Random_Credentials.Length; }
-
John 406 Reputation points
2021-07-01T01:27:27.417+00:00 .. and the click event for the Open Binary File button
private void Open_Binary_File_Button_Click(object sender, EventArgs e)
{
string dir=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (Directory.Exists(dir))
{var dlg = new OpenFileDialog(); { dlg.Title = "Open Binary File"; dlg.Filter = new System.Windows.Forms.OpenFileDialog().Filter; dlg.DefaultExt = "Image Files|*.img|Binary Files|*.bin|"; dlg.ShowDialog(); dlg.InitialDirectory = dir; if (dlg.ShowDialog() == DialogResult.OK) { File_Displayed_Label.Text = dir; } else { MessageBox.Show("The file you've tried to open is not in binary format.", "File Opening Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
-
John 406 Reputation points
2021-07-01T01:28:38.883+00:00 finally , the second half of the code snippet within the click event.
var savDlg = new SaveFileDialog(); { savDlg.Title = "Save Binary File"; savDlg.Filter = new System.Windows.Forms.SaveFileDialog().Filter; savDlg.DefaultExt = "Image Files|*.img|Binary Files|*.bin"; savDlg.ShowDialog(); savDlg.InitialDirectory = dir; if (savDlg.ShowDialog() == (DialogResult.OK)) { file_Name = savDlg.FileName; try { Stream strmX = new FileStream(file_Name, (FileMode)FileAccess.ReadWrite); using (StreamWriter sw = new StreamWriter(strmX)) { sw.Write(file_Name); } } catch (Exception) { MessageBox.Show("The file you've tried to save is not in binary format.", "File Saving Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }
-
John 406 Reputation points
2021-07-01T01:34:02.38+00:00 I have run the program just recently, and it gave me this error:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Binary File Encryptor Build 001
StackTrace:
at Binary_File_Encryptor_Build_001.Binary_Encryptor_Form.Encrypt_Region_Button_Click(Object sender, EventArgs e) in C:\Users\johnc\source\repos\Binary File Encryptor Build 001\Binary File Encryptor Build 001\Binary_Encryptor.cs:line 31Any way users could fix this runtime error?
-
John 406 Reputation points
2021-07-01T21:39:11.923+00:00 I think I may need to rewrite the entire code snippet.
Maybe that's the reason all other users are confused.
-
Timon Yang-MSFT 9,571 Reputation points
2021-07-02T06:33:34.307+00:00 @John
If you need additional information, you can modify the original post and post a comment to remind us that you have made the change. This will make the format of the code look better, and there is no limit of 1000 characters for the comment. -
AgaveJoe 26,186 Reputation points
2021-07-02T10:37:18.087+00:00 It seems, you do not understand the terms you are using and your code does not make a lot of sense. It's hard to figure out the problem you are trying to solve by reading your comments and reviewing your code. I think your goal is to use AES to encrypt file contents???
The official documentation covers AES encryption and how to save a file. Simply copy the code to your project and make whatever adjustments.
Why did you mention LBA and CHS (secondary media data blocks) when your code has nothing to do with LBA or CHS???
-
John 406 Reputation points
2021-07-02T20:22:06.267+00:00 That is correct. I just want to use AES-256 encryption with credentials just to perform
partial encryption of binary and other image files.