473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

The Irish fada (αινσϊ/ΑΙΝΣΪ) and encryption/decryption problem!

Hi everyone,
Can you help me please? I am having a problem with the
encryption/decryption of words with the Irish fada in them. The Irish
fada is like this: αινσϊ/ΑΙΝΣΪ. It's kind of like the French
grave...

Anyway when I run encryption on a plaintext word like:

RΝΣS NΑ RΪN

I get the following:

UKFzsStlitpd/QhEjqm0eQA=

However, when I decrypt it I get the following:

RÍΓ"S NÁ RÚN
As you can see it does not match the original plaintext. This is weird
- no? I'm confused - I was expecting the same original plaintext.
Can anyone help me please? I have the encryption/decryprion code
below. I pulled it off the net. I would greatly appreciate any
suggestions/commends/omprovements. I want to get the original plaintext
when I decrypt - that's the naturte of the problem... please help me!

******* ENCRYPTION STUFF *******
Public Shared Function EncryptString(ByVal AString As String) As
String
If AString = String.Empty Then
Return AString
Else
Dim encryptedData() As Byte
Dim dataStream As MemoryStream
Dim encryptor As ICryptoTransform
encryptor = mProvider.CreateEncryptor()
Try
dataStream = New MemoryStream
Dim encryptedStream As CryptoStream
Try
'Create the encrypted stream
encryptedStream = New CryptoStream(dataStream,
encryptor, CryptoStreamMode.Write)
Dim theWriter As StreamWriter
Try
'Write the string to memory via the encryption
algorithm
theWriter = New StreamWriter(encryptedStream)
'Write the string to the memory stream
theWriter.Write(AString)
'End the writing
theWriter.Flush()
encryptedStream.FlushFinalBlock()
'Position back at start
dataStream.Position = 0
'Create area for data
ReDim encryptedData(CInt(dataStream.Length))
'Read data from memory
dataStream.Read(encryptedData, 0,
CInt(dataStream.Length))
'Convert to String
Return Convert.ToBase64String(encryptedData, 0,
encryptedData.Length)
Finally
theWriter.Close()
End Try
Finally
encryptedStream.Close()
End Try
Finally
dataStream.Close()
End Try
End If
End Function

******* DECRYPTION STUFF *******
Public Shared Function DecryptString(ByVal AString As String) As
String
If AString = String.Empty Then
Return AString
Else
Dim encryptedData() As Byte
Dim dataStream As MemoryStream
Dim encryptedStream As CryptoStream
Dim strLen As Integer
'Get the byte data
encryptedData = Convert.FromBase64String(AString)
Try
dataStream = New MemoryStream
Try
'Create decryptor and stream
Dim decryptor As ICryptoTransform
decryptor = mProvider.CreateDecryptor()
encryptedStream = New CryptoStream(dataStream,
decryptor, CryptoStreamMode.Write)
'Write the decrypted data to the memory stream
encryptedStream.Write(encryptedData, 0,
encryptedData.Length - 1)
encryptedStream.FlushFinalBlock()
'Position back at start
dataStream.Position = 0
'Determine length of decrypted string
strLen = CInt(dataStream.Length)
'Create area for data
ReDim encryptedData(strLen - 1)
'Read decrypted data to byte()
dataStream.Read(encryptedData, 0, strLen)
'Construct string from byte()
Dim retStr As String
Dim i As Integer
For i = 0 To strLen - 1
retStr += Chr(encryptedData(i))
Next
'Return result
Return retStr
Finally
encryptedStream.Close()
End Try
Finally
dataStream.Close()
End Try
End If
End Function

Jan 17 '06 #1
2 7122

al*****@altavista.com wrote:
Hi everyone,
Can you help me please? I am having a problem with the
encryption/decryption of words with the Irish fada in them. The Irish
fada is like this: αινσϊ/ΑΙΝΣΪ. It's kind of like theFrench
grave...
The French acute, but we get it :)

Anyway when I run encryption on a plaintext word like:

RΝΣS NΑ RΪN

I get the following:

UKFzsStlitpd/QhEjqm0eQA=

However, when I decrypt it I get the following:

RÍΓ"S NÁ RÚN
Almost immediately I suspect a problem with encoding. We shall see if I
am right...
As you can see it does not match the original plaintext. This is weird
- no? I'm confused - I was expecting the same original plaintext.
Can anyone help me please? I have the encryption/decryprion code
below. I pulled it off the net. I would greatly appreciate any
suggestions/commends/omprovements. I want to get the original plaintext
when I decrypt - that's the naturte of the problem... please help me!
[code snipped]

Your source omits to mention what type of CSP mProvider is, but I
suspect it doesn't matter. These are the interesting parts:
Public Shared Function EncryptString(ByVal AString As String) As
String .... Dim theWriter As StreamWriter
Try
'Write the string to memory via the encryption
algorithm
theWriter = New StreamWriter(encryptedStream)
'Write the string to the memory stream
theWriter.Write(AString)
and
Public Shared Function DecryptString(ByVal AString As String) As
String .... 'Construct string from byte()
Dim retStr As String
Dim i As Integer
For i = 0 To strLen - 1
retStr += Chr(encryptedData(i))
Next


Whenever we convert between bytes and characters, we must make sure to
think carefully about the *encoding*. For any byte value between 128
and 255, the character mapped to will depend on the encoding; to get
correct roundtrip results, we must be sure to use the same encoding
both ways.

When you write your input string to the cryptostream, your StreamWriter
(as per the docs) defaults to UTF8Encoding. But when you read the bytes
back and convert back to a string, you use the legacy Chr() function,
which uses the current thread's default ANSI code page, which is a
different encoding than UTF8. Hence the difference in output from
input.

The quick fix is to use UTF8 on the way out as well as the way in:
replace the For loop in the above snippet from Decrypt with

retStr = System.Text.Encoding.UTF8.GetString(encryptedData)

and you're done.
The more enlightened (but longer) path is to revise the code to have a
better distinction between character data and byte data. I realise it's
not your code but understanding is always worthwhile :)

To that end, ideally I would like to see the input string explicitly
converted to a byte() (with an explicit encoding), and that byte()
written to the CryptoStream; rather than (as at present) the character
-> byte conversion being hidden in the default behaviour of a
StreamWriter. Indeed, StreamWriter is meant for character data, but
encryption is done on byte data; this is the warning flag that
something is wrong.

I am sure you will be happy with just the quick fix, however :)

--
Larry Lard
Replies to group please

Jan 17 '06 #2
Larry - merci beaucoup mon ami!

Perfect - its working - that a million mate!

Al.
The happy enlightened one.

Jan 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Barry | last post by:
Hi all, I'd like to implement a Gaelic tutorial with the inclusion of Gaelic fonts. Irish Gaelic has 18 letters - abcdefghilmnoprstu but vowels can also take an acute accent, called a fada...
1
by: Jase H | last post by:
Hello, I have a ASP.NET web application problem involving the data encryption and decryption assembly(DLL) used on the connection string value that is set in the webconfig file. The problem...
2
by: Dave Bailey | last post by:
I have developed a web app using DPAPI to encrypt a connection string in the web.config file. The application works perfectly on the development machine but when deployed to the server when...
4
by: hohans | last post by:
Hi all, I have an encryption class that encrypts and decrypts password using TripleDESCryptoServiceProvider. It was written originally in framework 1.0 and been working fine. And those...
0
by: Dipanwita | last post by:
I have written a RSA encryption/decryption function in c using the formula a = b^c %n. For solving the equation I have used Squaring and multiplying method for modulo exponentiation . These...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
13
by: Tom Andrecht | last post by:
I'm trying to get some encryption/decryption routines going to take care of my data, and while the encryption is working great, I keep running into the message "Padding is invalid and cannot be...
5
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.