using System; using System.Security.Cryptography; using System.Text; using System.IO;
class Program { static void Main() { Encoding ascii = Encoding.ASCII; byte[] key = {0x9b, 0x4c, 0x3d, 0x50, 0x3b, 0x00, 0x00, 0x00}; // use null bytes initially byte[] cipherText2 = {0x2e, 0x4c, 0x56, 0xe7, 0xf9, 0xf9, 0xd0, 0xef, 0xab, 0x3c, 0x4d, 0x2e, 0x2f, 0x21, 0xf5, 0xcd, 0x8e, 0x9e, 0xd0, 0xf8, 0xe7, 0xa8, 0xd0, 0xbc}; byte[] cipherText = null; byte[] plainText = null; string s = null; bool isCandidate = false; StreamWriter sw = new StreamWriter("candidates.txt"); for(int i = 0; i < 255; i += 2) { for(int j = 0; j < 255; j += 2) { for(int k = 0; k < 255; k += 2) { key[5] = (byte)i; key[6] = (byte)j; key[7] = (byte)k; cipherText = DesDecrypt(cipherText2, key); plainText = DesDecrypt(cipherText, key); s = ascii.GetString(plainText); isCandidate = true; foreach(char c in s) { int ascval = (int)c; if (ascval == 63 || ascval < 32 || ascval > 126) { isCandidate = false; break; } } if (isCandidate) { string result = String.Format("{0} : {1} : {2} - {3}", i, j, k, s); // now includes bytes Console.WriteLine(result); // now writes candidate to console as well sw.WriteLine(result); } } } Console.WriteLine("i = {0} completed", i); // indication of where we're at } sw.Close(); }
static byte[] DesEncrypt(byte[] plainText, byte[] key) { // Create a new DES key. DESCryptoServiceProvider des = new DESCryptoServiceProvider(); // Set the KeySize = 64 for 56-bit DES encryption. // The msb of each byte is a parity bit, so the key length is actually 56 bits. des.KeySize = 64; des.Key = key; des.Mode = CipherMode.ECB; des.Padding = PaddingMode.None;
ICryptoTransform ic = des.CreateEncryptor();
byte[] enc = ic.TransformFinalBlock(plainText, 0, 24); // 24 bytes assumed
return enc; }
static byte[] DesDecrypt(byte[] cipherText, byte[] key) { DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.KeySize = 64; des.Key = key; des.Mode = CipherMode.ECB; des.Padding = PaddingMode.None;
ICryptoTransform ic = des.CreateDecryptor();
byte[] dec = ic.TransformFinalBlock(cipherText, 0, 24);
return dec; }
}
|
|
|
|
VulpesPosted Nov 11, 2011, 3:15 PM
Consider this code:
void swap1(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void swap2(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
// within some other function
int x = 1;
int y = 2;
swap1(x, y);
cout << "x = "<< x << ", y = " << y << endl; // x = 1, y = 2
swap2(&x, &y);
cout << "x = "<< x << ", y = " << y << endl; // x = 2, y = 1
When function swap1 is called, the arguments are passed by value which means that the values of the local variables x and y are copied into the parameters of the same name.
When this function executes, the values of the parameters x and y are exchanged within the function. However when the function returns, the local variables x and y still have the values they did to start with. This is because it's only copies of their values which have been swapped, not the original values.
When function swap2 is called, the arguments are passed by reference which means that the addresses of the local variables x and y are passed to parameters of the same name. This means, in effect, that the parameters share the same memory addresses as the local variables.
When this function executes, the values of the parameters x and y are exchanged within the function as before. However when the function returns, the local variables x and y have also had their values swapped. This is because the parameters were sharing the same memory addresses as these variables which therefore exchanged the values of the variables themselves.
Sam HobbsPosted Nov 12, 2011, 8:54 PM
Sam HobbsPosted Nov 12, 2011, 8:46 PM
There are many questions in the C# Language forum that should not be; they should be put in one of the other forums that are appropriate for the question. If a question is about use of .Net but the language is C++, then it is not definite where it should be. So try to do what you should do and I hope everyone will have respect and appreciation for your efforts. In otehr forusm, there would be separate forums for C++ and for other topics that are not about the language but is about programs written in C++. The CodeGuru.com forums is an example of that. Since the C# Corner web site (this one) gets so few C++ questions, it is reasonable for C++ questions to be put in the C++ forum. If however the question is much more a question of Security & Cryptography using .Net then it seems appropriate to put the question here. Hopefully you will get good at converting code between C++ and C#.
siabanie baniePosted Nov 12, 2011, 8:30 PM
siabanie baniePosted Nov 12, 2011, 8:27 PM
Sam HobbsPosted Nov 11, 2011, 7:01 PM
and
I don't even know where to look for Java language documentation.
If the length method of the Java String class does not always return 8 for the data and if exactly 8 bytes needs to be processed in that situation, then of course there is a difference.
In C, the string length (strlen) is determined by the first character with the value zero. The C++ std::string class determines the length in a more meaningful manner. If the code is supposed to process exactly 8 bytes and the code that uses the length property does not work then I certainly would investigate what determines the length.
Sam HobbsPosted Nov 11, 2011, 5:02 PM
Your description of Java sounds similar to MFC. It would help for people learning Java to understand that. I will try to remember it.
VulpesPosted Nov 11, 2011, 4:19 PM
This means that you can't write a method to swap basic types unless you wrap them in a reference type first.
When you pass a reference type (i.e. a reference to an object) by value, then its the reference that gets copied. Consequently, the method does have access to the original object and so can alter its state.
Sam HobbsPosted Nov 11, 2011, 3:56 PM
It depends on the language. The simplified answer is that a call by reference passes an object so that it can be modified; in other words, a reference to the object is passed so the actual object can be accessed. A call by value passes the value; in other words, a copy of the data so that the data in the calling function is not modified.
The terms are meaningful in a language such as VB 6 and I assume for VB .Net too.
The terms are so confusing for the C language that I think it is foolish to use the terms for C. I would have said that regardless of what Vulpes said. In C, an address can be passed and therefore it is confusing whether that is a call by reference or a call by value. Certainly we dcan try to define what is what, but I think it is best to not even try for C.
In C++ there is the reference operator and such; I am not sure of the terminology. In C++ a reference is a new syntax for pointers, but they are usually not described as pointers and C++ references are much safer than pointers. A reference in C++ is confusing because they are used everywhere in a program, not just for function calls. Similarly, C# defines all classes as a reference type and C# has the "ref" keyword. All that is still confusing a bit for me; I suppose that ref in a function prototype is needed only for value types but all that is one thing that can be very confusing for those of us learning C#.
So my advice is to not use the terms "by reference" and "by value" for C, C++ and C# unless you are describing use of the "ref" keyword in C#. I am not sure about Java except that I assume the terms are equally confusing/meaningless for Java too.
When the terms are used, the important distinction is that by value means that the called function cannot change the data that the calling function has but by reference means that it can.
siabanie baniePosted Nov 11, 2011, 2:32 PM
VulpesPosted Nov 11, 2011, 2:00 PM
http://www.c-sharpcorner.com/Forums/Post/31/visual-c.aspx
Sam HobbsPosted Nov 11, 2011, 1:49 PM
So eventhough the syntax of all three languages are nearly identical, there are substantial differences in the libraries. C# uses .Net (technically it is CLI) and the other two do not.
Also note that C has a runtime library that is part of the language and the C++ standard library does many of the things that the C runtime does, but not everything. So at that level, C and C++ vary in the manner that if you use C++ you tend to do things the C++ way that cannot be done using C.
For most things, the difference in performance that can be acheived by all three is usually not considered to be significant and the portability of C# and Java usually is a significant consideration. When performance is critical and portability is not a concern, C++ is considered a better choice than C# and Java.
siabanie baniePosted Nov 10, 2011, 10:01 PM
Hi Vulpes,
Thanks again for the helped - But I wonder if I could do the above code as like this example shown below.
I am using visual studio and new about this language and IDE so, I like to see how we can implement it in this way?
Is it feasible or does it make it difficult to see?
object.cpp
#include <iostream>
using namespace std;
#include "circle.hh"
main()
{
cout << "Please enter the radius?\n";
double r;
cin >> r;
CircleC circle(r); // construct a circle radius r
cout << "Area: " << circle.Area() << "\n";
cout << "Circumference: " << circle.Circum() << "\n";
}
circle.hh
// C++ header file for CircleC
class CircleC {
public:
CircleC(double radius); // Constructor for CircleC
double Circum();// Member function - circumference
double Area(); // member function - area
private:
double radius;// private data cannot be accessed outside
};
circle.cpp
// C++ implementation
#include "circle.hh"
CircleC::CircleC(double r) : radius(r) {}
double CircleC::Circum() { return 2.0*3.14159*radius;}
double CircleC::Area() { return 3.14159*radius*radius;}
siabanie baniePosted Nov 10, 2011, 7:52 PM
VulpesPosted Nov 10, 2011, 5:09 AM
I've used a do/while loop below:
siabanie baniePosted Nov 9, 2011, 6:36 PM
If I want to say: Ask the user to try again without exit immediately? How can I do that? Do I have to do another loops?
I mean once user placing all the matrix; it will print "Do you want to try again" Press "Y" or "N" will Exit the console.
Then if user press "Y" it will go repeat all over again. No will exit.
VulpesPosted Nov 9, 2011, 6:31 PM
siabanie baniePosted Nov 9, 2011, 6:21 PM
Yes sorry I should have start new tread but...:)
Thanks for the input - I have tried them but it didnt seem give me the output I expected? Or perhaps I typed an incorrect row or columns?
As i tried this:
e.g
(1 2 3) (5)
(4 5 6) (6)
(7)
Which the correct answer will be
(35)
(92)
Edited: Sorry I think I placed a wrong rows and columns - Thanks again yes and work great! Thanks!
VulpesPosted Nov 9, 2011, 3:49 PM
However, in view of the problems we've had 'losing' threads, I'm prepared to answer it on here :)
As arrays in C++ need to be fixed size at compile time, the only thing you can do is to define the dimensions of the array to be big enough to accommodate the maximum matrix and vector sizes the user is likely to want to use. If that's 10 by 10 say, then I'd define MAX_SIZE to be 10.
The following program should do what you want:
siabanie baniePosted Nov 9, 2011, 1:04 PM
e.g
(1 2 3) (5)
(4 5 6) (6)
(7)
Which the correct answer will be
(35)
(92)
The program need so that it is easy to change the size of the matrix and vector.
Here what I have done so far but it isn't working as I want. I wonder if would be easy to use const declaraions to specify the dimensions, any idea?
#include "stdafx.h"
#include
#include
using namespace std;
int main()
{
int **mat1;
int **mat2;
int **result;
int row,col;
cout<<"Please enter row/col"<
mat1 = new int *[row];
mat2 = new int *[row];
result = new int *[row];
int k,i,j;
for (k=0; k
mat2[k] = new int[col];
result[k] = new int[col];
for (i=0; i
for (j=0; j
for(i=0; i
cout<
PS: if you have a better way and simple way to demo this would be helpful. Thanks.
siabanie baniePosted Nov 9, 2011, 12:59 PM
VulpesPosted Nov 9, 2011, 10:39 AM
Having said that the first thing I did was to test the algorithm on plaintext which was 24 bytes long and it double encrypted and decrypted fine.
But, no matter, if it's working go with it :)
siabanie baniePosted Nov 9, 2011, 10:19 AM
VulpesPosted Nov 8, 2011, 3:18 PM
siabanie baniePosted Nov 8, 2011, 2:53 PM
VulpesPosted Nov 7, 2011, 4:33 AM
siabanie baniePosted Nov 6, 2011, 9:39 PM
VulpesPosted Nov 6, 2011, 3:31 PM
siabanie baniePosted Nov 6, 2011, 1:13 PM
Back to the Java code, do you mind explaining to me few lines of the code what they are actually doing so I can follow them up and maybe tweak them please.
Here the snippets of the code:
...
...
..
..
..
..
..
..
..
..
..
VulpesPosted Nov 6, 2011, 9:03 AM
If you look at this link (http://msdn.microsoft.com/en-us/library/system.security.cryptography.paddingmode.aspx), there are five possible padding modes including 'None' which you can try in turn. Just change the line: