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
Faizal I
NA
54
798
Need equivalent c# code for implementation
Jul 6 2020 5:04 AM
I have one .net application where I need to call an API. That API needs value to be encrypted with AES algorithm with Hashing. That API is developed in Java. I am not able to find equivalent .net code to encrypt and decrypt as done in Java..
Attached Java code here.
Need equivalent c# code.
I was trying below code but it didnt work
public
byte
[] AES_Encrypt(
byte
[] bytesToBeEncrypted,
byte
[] passwordBytes) {
byte
[] encryptedBytes =
null
;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte
[] saltBytes =
new
byte
[] {
1,
2,
3,
4,
5,
6,
7,
8
};
using
(MemoryStream ms =
new
MemoryStream()) {
using
(RijndaelManaged AES =
new
RijndaelManaged()) {
AES.KeySize = 256;
AES.BlockSize = 128;
var key =
new
Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using
(var cs =
new
CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write)) {
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return
encryptedBytes;
}
public
byte
[] AES_Decrypt(
byte
[] bytesToBeDecrypted,
byte
[] passwordBytes) {
byte
[] decryptedBytes =
null
;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte
[] saltBytes =
new
byte
[] {
1,
2,
3,
4,
5,
6,
7,
8
};
using
(MemoryStream ms =
new
MemoryStream()) {
using
(RijndaelManaged AES =
new
RijndaelManaged()) {
AES.KeySize = 256;
AES.BlockSize = 128;
var key =
new
Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using
(var cs =
new
CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) {
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return
decryptedBytes;
}
Attachment:
EncryptorDecryptorUtil.zip
Reply
Answers (
1
)
How to convert Byte array of Image into .docx file?
Best Bang for the buck Hosting for MVC .net?