TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Austin Muts
1.4k
330
125.9k
AES 128 bit Encryption-Decryption in C#
Apr 16 2019 7:36 AM
My code does not produce the expected encrypted result of ad7a7a25828cd46c513369798a95de31 after encrypting 788359
Any help?
public
static
class
EncryptionService
{
private
static
byte
[] key;
static
EncryptionService()
{
// key here
key = UTF8Encoding.UTF8.GetBytes(
"f22704b8bc0dcc606303b5eb97332630"
);
}
public
static
string
ByteArrayToHexString(
byte
[] ba)
{
return
BitConverter.ToString(ba).Replace(
"-"
,
""
).ToLower();
}
public
static
string
EncryptAndEncode(
string
plaintext)
{
plaintext =
"788359"
;
return
ByteArrayToHexString(AesEncrypt(plaintext));
}
public
static
byte
[] AesEncrypt(
string
inputText)
{
byte
[] inputBytes = UTF8Encoding.UTF8.GetBytes(inputText);
byte
[] result =
null
;
using
(MemoryStream memoryStream =
new
MemoryStream())
{
using
(CryptoStream cryptoStream =
new
CryptoStream(memoryStream, GetCryptoAlgorithm().CreateEncryptor(key, key), CryptoStreamMode.Write))
{
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
result = memoryStream.ToArray();
}
}
return
result;
}
public
static
byte
[] StringToByteArray(
string
hex)
{
return
Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public
static
string
DecodeAndDecrypt(
string
cipherText)
{
string
DecodeAndDecrypt = AesDecrypt(StringToByteArray(cipherText));
return
(DecodeAndDecrypt);
}
public
static
string
AesDecrypt(Byte[] inputBytes)
{
Byte[] outputBytes = inputBytes;
string
plaintext =
string
.Empty;
using
(MemoryStream memoryStream =
new
MemoryStream(outputBytes))
{
using
(CryptoStream cryptoStream =
new
CryptoStream(memoryStream, GetCryptoAlgorithm().CreateDecryptor(key,key), CryptoStreamMode.Read))
{
using
(StreamReader srDecrypt =
new
StreamReader(cryptoStream))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
return
plaintext;
}
private
static
RijndaelManaged GetCryptoAlgorithm()
{
RijndaelManaged algorithm =
new
RijndaelManaged();
//set the mode, padding and block size
// algorithm.Padding = PaddingMode.ISO10126;
algorithm.Padding = PaddingMode.Zeros;
//algorithm.Mode = CipherMode.CBC;OFB
algorithm.Mode = CipherMode.ECB;
algorithm.KeySize = 256;
algorithm.BlockSize =128 ;
// IV = Encoding.UTF8.GetBytes(algorithm.BlockSize / 8);
// algorithm.Key = keyAndIvBytes
//algorithm.IV = Encoding.UTF8.GetBytes(128 / 8);
return
algorithm;
}
}
Reply
Answers (
3
)
When Create Dll
C# Smart Contracts with strongforce.io