I need to create an encryption class that does not accept special characters,that uses letters and numbers only. Does anyone have one or can you help me develop it?
-
2No, we can't. But you can do some [research](https://msdn.microsoft.com/en-us/library/as0w18af(v=vs.110).aspx) into encryption and come back when you encounter a problem we can help you with. – ThePerplexedOne Dec 01 '16 at 16:44
-
You can use regex to make restrictions. it has nothing to do with encryption method (I think). http://stackoverflow.com/questions/3617797/regex-to-match-only-letters – M.kazem Akhgary Dec 01 '16 at 16:45
-
2It's not clear exactly what help you're asking for at this point. Can you be more specific? Please also be more specific about what you're trying to do - encryption can mean a lot of things depending on what you're trying to accomplish. Also, this seems like a homework problem; if so, please read [this article](http://meta.programmers.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) on how to ask for help with homework. – EJoshuaS - Stand with Ukraine Dec 01 '16 at 16:45
-
3Modern cryptography is inherently a binary process. The input is binary and the output is binary. I can't think of any current encryption that accepts only letters and numbers as input. If you go all the way back to the character substitution cyphers of the 16th, 17th, and 18th century you could make the case for allowing only letters and numbers as input. – Kevin Dec 01 '16 at 16:50
-
@Kevin That's a good point - it's unclear from the OP's question whether he wants to use a modern or classical cipher though - we definitely need more information on what he's trying to do – EJoshuaS - Stand with Ukraine Dec 01 '16 at 16:55
-
As for a classical cipher, have a look at https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher . All modern symmetric and asymmetric cryptography is bit-based and will produce binary results, unless you cheat by applying Base64 encoding to in- and output and calling it "alphanumerical". – Maximilian Gerhardt Dec 01 '16 at 17:01
-
@Kevin I have a class of encryption, but it is not working when storing it in SQLite, so I thought I would propose the question, in case someone has the same problem, or if there is a more efficient solution. – Arnold Ulate Segura Dec 01 '16 at 17:59
-
1@ArnoldUlateSegura So your question is why your encryption is breaking when you try to store the results in SQLite? Can you edit your question to reflect that? Also, please post the offending code and a description of what you mean by "not working" - what is going wrong? – EJoshuaS - Stand with Ukraine Dec 01 '16 at 18:41
2 Answers
You don't need to create a separate encryption algorithm to do this. Just use the following regex to validate your input:
[A-Za-z0-9]+
This will validate that the input only contains alphanumeric charactrs. After that, you can perform whatever encryption you want.
So, basically, you could have a class like the following (and I'm mixing C# and pseudocode here):
public class Encryption {
private readonly Regex regex = new Regex("[A-Za-z0-9]+");
public string Encrypt(string toEncrypt) {
if (toEncrypt == null)
throw new ArgumentNullException("toEncrypt");
// This check is very important
// If toEncrypt contains non-alphanumeric characters, is an empty string, or consists only of whitespace don't accept it
if (!regex.IsMatch(toEncrypt))
throw new ArgumentException("String is not alphanumeric");
// Obviously pseudocode
// Replace this line with a call to AES, 3DES, Twofish, Caesar Cipher, or whatever encryption algorithm you want
return EncryptionAlgorithm.Encrypt(toEncrypt, whateverKey);
}
}
The term "encryption" is actually pretty vague in this case. If you're looking for "serious" encryption, you probably want to use either Twofish or the Advanced Encryption Standard (AES); both are highly secure at this point and have good library support, but AES is somewhat better studied. The other AES contest finalists (such as Serpent) are also highly secure, but they tend not to be studied as thoroughly or have as good of library support as Twofish and AES. Unless you have a good reason to use Twofish or one of the other AES contest finalists instead of AES, it's probably better to just use AES.
I say "highly secure at this point" because security's intrinsically relative to current technology; for example, public-key cryptography would be broken by quantum computers. DES was considered secure until hardware became fast enough to brute-force it. (In fact, it's still considered relatively secure against other kinds of attacks, such as differential crypanalysis).
Note that if you're serious about security it's rarely a good idea to try to design your own algorithm.
There are also classical ciphers like the Caesar Cipher available. You should only use these if you don't care about security at all as they're all badly broken at this point. They aren't built into the .NET framework but most of them are pretty easy to implement yourself.

- 11,977
- 56
- 49
- 78
-
@zaph Yeah, you probably would want to just use AES, I'll edit. – EJoshuaS - Stand with Ukraine Dec 01 '16 at 18:38
Encryption produces binary output, not characters, if you need a character representation use Base64 or hexadecimal encoding.

- 111,848
- 21
- 189
- 228