|
Dear All,
i want to encrypt / decrypt a string that can be of any length to maximum 10 random characters only,
i tried a lots of encryption / decryption algorithms but all gives very long encrypted string, appreciate if anyone can share to limit the encryption string to only 10 characters provided these characters to be random each time.
Many Thanks in Advance.....
|
|
|
|
|
Can't be done.
Think about this. Encryption means that not a single bit of data can be lost. Every bit of it must be represented. Encryption is lossless.
Now, you said you want to pack any string of any length into no more than 80 bits. How on earth are you going to pack a "string of any length", lets just say a 3MB string, into 80 bits?
Encryption is NOT compression.
You may be able to cryptographic HASH a string into 80 bits, though that's going to limit your possible hash values. With such few bits (80 of them), the chance of a collision (2 strings coming up with the same hash value) is higher than normal. There is no way to reverse the process and get the original string back. A cryptographic hash cannot be reversed.
|
|
|
|
|
One-time-pads.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
You cannot encrypt a string to any specific maximum length: when you encrypt a string you are adding information not compressing it - and the encrypted data is likely to be less compressible than the original, or the encryption method includes too much repetitiveness in it;s output and may be easily cracked!
You can make a value from your input which has a fixed size, but that is by a process called Hashing - which is similar to encryption but is not reversible; you cannot retrieve the original input from a hashed value, while you can with encrypted data.
Why do you think you need this? What are you trying to do? There may be a better way ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
@
OriginalGriff
Hi, I am getting a value in a field which i needs to hide from user for sometime, and once the workflow completed i need to show back the orignal string in the same field.
Thats it.
The issue is if user inputs in a field of 10 characters, encryption string goes to more then 10 characters which the field cannot accommodate hence it cannot be decrypted, so basically issue reside with decryption.
|
|
|
|
|
So store the user input away somewhere - a DB maybe - and use a "place holder" value to indicate it. When they want to retrieve it, you cross reference the string back to the original store and you have the string they started with.
The problem is that 10 characters is unusual, most hashes these days start at 128 bits, which won't fit in 10 chars no matter what you do with it.
Suggestion: if you use A-Z and 0-9 that's 36 possible characters - that's a total of 3E15 combinations (36^10) which fits in a 64 bit integer. So ... if you use a random number generator to generate a value in the range 0 to 3E15 (and check it isn't used, if it is regenerate) you could convert that to a 10 character string very easily, and that can be converted back to a big integer and used to lookup the actual string to return.
Make sense?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ya that was the workaround i already done, but was looking the possibility for limiting the encryption which now i understand couldn't happen so i will stick with the workaround you advise and already been using. Many thanks for your time... This gives me confident for what i am doing is correct. Thanks.
|
|
|
|
|
You're welcome!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have a problem to save userControl to PNG By Drop & Drag.
I can save it with a Button_Click But, I try to use an easy way to save UserControl to PNG with drop & drag
What to do am I, I don't know to get path Windows Explorer or The desktop where mouse drop file? Thanks for suggesting.
I use this Button_Click()
<pre>private void saveToPng_Click(object sender, RoutedEventArgs e)
{
string filePath = Environment.CurrentDirectory;
var control = this.Control;
RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Pbgra32);
Rect bounds = VisualTreeHelper.GetDescendantBounds(control);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(control);
ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size));
}
rtb.Render(dv);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (Stream fileStream = new FileStream(filePath+"test.png", FileMode.Create))
{
png.Save(fileStream);
}
}
|
|
|
|
|
|
thanks, Eddy Vluggend
But, I need Drag UserControl from my-app and Drop it to Windows-Explorer or Desktop to Saving Format PNG.
Do U understand my problem?
I want to save UI of UserControl BY Drop-Drag as Format PNG
|
|
|
|
|
|
I use WPF-XAML and the code was about windows-Form, I can't understand that.
Can U help me with a sample of WPF-XAML?
please use WPF-XAML
Thank
|
|
|
|
|
I have no experience with XAML. Google is your friend, I imagine the proces to be very similar
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hi. I am used to visual basic so I need help with a C# conversion. I have a for that allows opening a choice of other forms. I am used to the form.show approach.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void cmdLotto_Click(object sender, EventArgs e)
{
Form = new Form(LottoConverter);
Form.ShowDialog();
}
private void cmdEuro_Click(object sender, EventArgs e)
{
Form = new Form(EuroConverter);
Form.ShowDialog();
|
|
|
|
|
Try this:
private void cmdLotto_Click(object sender, EventArgs e)
{
Form lc = new LottoConverter();
lc.ShowDialog();
}
private void cmdEuro_Click(object sender, EventArgs e)
{
Form ec = new EuroConverter();
ec.ShowDialog();
{
Or better:
private void cmdLotto_Click(object sender, EventArgs e)
{
LottoConverter lc = new LottoConverter();
lc.ShowDialog();
}
private void cmdEuro_Click(object sender, EventArgs e)
{
EuroConverter ec = new EuroConverter();
ec.ShowDialog();
{
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks that makes sense Form is a class and I need an instance of the class. However I still have 4 errors.
Error 1 'Lottery.LottoConverter' is a 'type' but is used like a 'variable' D:\Vb Apps.Net\C# Apps\Lotto\Lottery\Selector.cs 26 28 Lottery
Error 2 'System.Windows.Forms.Form' does not contain a constructor that takes '1' arguments D:\Vb Apps.Net\C# Apps\Lotto\Lottery\Selector.cs 26 19 Lottery
Error 3 'Lottery.EuroConverter' is a 'type' but is used like a 'variable' D:\Vb Apps.Net\C# Apps\Lotto\Lottery\Selector.cs 32 28 Lottery
Error 4 'System.Windows.Forms.Form' does not contain a constructor that takes '1' arguments D:\Vb Apps.Net\C# Apps\Lotto\Lottery\Selector.cs 32 19 Lottery
|
|
|
|
|
Sorry. Typing error. This works fine now. Thanks a lot.
|
|
|
|
|
You're welcome!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Try to create both Contructors to each Form Class, maybe it helps!
|
|
|
|
|
Dear All,
I have created billing system for Motorcycle service centers using c# .
I wan't to add a feature, when the user print the final invoice, customer will receive a sms from the name of the service dealer which including thanks for the coming to the service.
and also after three months of the time that he last came to the service, system has to send a message to the customer that saying please come for the next service bla bla etc.
Can any one guide me how to do this? for this feature I planned to install a 4G Dongle and sim card from local cellular service to the service dealer's pc.
|
|
|
|
|
What have you tried?
Where are you stuck?
What help do you need?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Using a dongle is probably over-complicating things and certainly impacts scalability (if you need a second PC you need another dongle + SIM + contract etc).
There are plenty of webservices out there that will send an SMS for you, and give you additional management info / usage data etc.
Depending on volumes you might even find a free provider, though often the free services (understandably) inject adverts into your text. Not knowing which country you're in I can't recommend a specific service...
Once you have a webservice interface, it's very simple to integrate into your C# project and no worries about hardware etc. Some of these services even allow you to schedule texts for future dates - don't know if up to 3 months in advance, though!
Google "send sms from webservice"
Cheers,
Derek
|
|
|
|
|
I'm facing a new situation involving a program, written in .NET Framework 4.0/C#, which has to encode/decode and sign messages to be sent/received in a particular kind of WAN. This is the current scenario, which is working fine. We have a .p12 file (which contains the sender certificate) and a .cer file (which contains the receiver certificate), which are both installed in the pc. The scenario is working using SHA1 and PKCS#7 at 1024 bit, so the following piece of code works:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
private static string senderCert = "sendername";
private static string receiverCert = "receivername";
public static byte[] Verify(this byte[] signedData)
{
X509Certificate2 certPub = GetReceiverCert();
if (certPub == null) return null;
ContentInfo decodeContentInfo = new ContentInfo(signedData);
SignedCms decodeCMS = new SignedCms(decodeContentInfo, false);
try
{
decodeCMS.Decode(signedData);
SignerInfo signerInfo = decodeCMS.SignerInfos[0];
X509Certificate2Collection certCollection = new
X509Certificate2Collection(certPub);
return decodeCMS.ContentInfo.Content;
}
catch (CryptographicException err)
{
Logger.Log(err);
return null;
}
}
private static X509Certificate2 GetSenderCert()
{
X509Store storeMy = new X509Store(StoreName.My, StoreLocation.CurrentUser);
storeMy.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certColl = storeMy.Certificates.Find
(X509FindType.FindBySubjectName, senderCert, false);
if (certColl.Count == 0)
return null;
storeMy.Close();
return certColl[0];
}
private static X509Certificate2 GetReceiverCert()
{
X509Store storeMy = new X509Store(StoreName.My,
StoreLocation.CurrentUser);
storeMy.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certColl = storeMy.Certificates.Find
(X509FindType.FindBySubjectName, receiverCert, false);
if (certColl.Count == 0)
return null;
storeMy.Close();
return certColl[0];
}
public static byte[] Sign(this byte[] data)
{
X509Certificate2 certificate = GetSenderCert();
if (certificate == null) return null;
if (data == null)
throw new ArgumentNullException("data");
if (certificate == null)
throw new ArgumentNullException("certificate");
ContentInfo content = new ContentInfo(data);
signedCms = new SignedCms(content, false);
CmsSigner signer = new CmsSigner(certificate);
signer.IncludeOption = X509IncludeOption.EndCertOnly;
signedCms.ComputeSignature(signer);
return signedCms.Encode();
}
As I said before, this code works fine under the given scenario (SHA1+PKCS#7 at 1024 bit). Now the client asks an update of the crypthography, in particular using SHA256 instead of SHA1 and PKCS#10 instead of PKCS#7 with key length 2048 bit instead of 1024.
According to your experience, what should I do to get the new requirements?
The only thing I'm sure is that I cannot use a NET Framework greater than this one because the devices are old XPs and they cannot be changed for company issues. So I cannot use new libraries released for .NET 4.7.1 to solve my problem.
I suppose that the new certificates will be released as the previous method, a file for sender, another one for receiver, to be installed in the machine.
Honestly I haven't a lot of experience with crypto and I would like to find an easy path which can follow the logic of the current methods, since the fact their scope wouldn't be changed, but just the signing approach.
I have no other limitations in using other libraries, pinvoke and other stuff, if it can really help.
Thanks to all of you for your support.
Any suggestion is appreciated. Regards
|
|
|
|
|
Im trying to publish a message that should communicate between classes:
public sealed class EventMessager : IEventMessager
{
private readonly Dictionary<Type, object> subscriberLookup = new Dictionary<Type, object>();
public IObservable<T> Observe<T>()
{
if (!subscriberLookup.TryGetValue(typeof(T), out object subject))
{
subject = new Subject<T>();
subscriberLookup.Add(typeof(T), subject);
}
return ((ISubject<T>)subject).AsObservable();
}
public void Publish<T>(T @event)
{
if (subscriberLookup.TryGetValue(@event.GetType(), out object subject))
{
((Subject<T>)subject).OnNext(@event);
}
}
}
This works well, except for the time I wanted to use Reflection in order to initate a new Message class like so:
eventMessager.Publish(Activator.CreateInstance(w[0]));
It complains that it cannot cast the subject of class type that w is as an Subject Object.
It will work as expected if I just say new MyClass instead of the activator.
I really cant find any solutions online either, so does anyone know what is going on here?
modified 6-Sep-18 2:05am.
|
|
|
|
|