This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Services and the fly likes Symmetric key encryption and decryption using Rijndael algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Symmetric key encryption and decryption using Rijndael algorithm" Watch "Symmetric key encryption and decryption using Rijndael algorithm" New topic
Author

Symmetric key encryption and decryption using Rijndael algorithm

anshu bajpai
Greenhorn

Joined: Jan 02, 2008
Posts: 5
kindly provide me the equivalent java implementation of the following Symmetric key encryption and decryption using Rijndael algorithm implemented in VB

Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class crypto
Private Const passPhrase As String = "Pas5pr@sekishan"
Private Const saltValue As String = "s@1tValuekishan"
Private Const hashAlgorithm As String = "SHA1"
Private Const passwordIterations As Integer = 2
Private Const initVector As String = "@1B2c3D4e5F6kishan"
Private Const keySize As Integer = 256

Private Function Encrypt(ByVal plainText As String, _
ByVal passPhrase As String, _
ByVal saltValue As String, _
ByVal hashAlgorithm As String, _
ByVal passwordIterations As Integer, _
ByVal initVector As String, _
ByVal keySize As Integer) _
As String

Dim initVectorBytes As Byte()
initVectorBytes = Encoding.ASCII.GetBytes(initVector)

Dim saltValueBytes As Byte()
saltValueBytes = Encoding.ASCII.GetBytes(saltValue)

Dim plainTextBytes As Byte()
plainTextBytes = Encoding.UTF8.GetBytes(plainText)

Dim password As PasswordDeriveBytes
password = New PasswordDeriveBytes(passPhrase, _
saltValueBytes, _
hashAlgorithm, _
passwordIterations)

Dim keyBytes As Byte()
keyBytes = password.GetBytes(keySize / 8)

Dim symmetricKey As RijndaelManaged
symmetricKey = New RijndaelManaged

symmetricKey.Mode = CipherMode.CBC

Dim encryptor As ICryptoTransform
encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)

Dim memoryStream As MemoryStream
memoryStream = New MemoryStream

Dim cryptoStream As CryptoStream
cryptoStream = New CryptoStream(memoryStream, _
encryptor, _
CryptoStreamMode.Write)
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)

cryptoStream.FlushFinalBlock()

Dim cipherTextBytes As Byte()
cipherTextBytes = memoryStream.ToArray()

memoryStream.Close()
cryptoStream.Close()

Dim cipherText As String
cipherText = Convert.ToBase64String(cipherTextBytes)
cipherText = cipherText.Replace("+", "~")
Encrypt = cipherText
End Function

Public Function Encrypt(ByVal Data As String)
Try
Return Encrypt(Data, _
passPhrase, _
saltValue, _
hashAlgorithm, _
passwordIterations, _
initVector, _
keySize)
Catch ex As Exception
Throw ex
End Try
End Function

Private Function Decrypt(ByVal cipherText As String, _
ByVal passPhrase As String, _
ByVal saltValue As String, _
ByVal hashAlgorithm As String, _
ByVal passwordIterations As Integer, _
ByVal initVector As String, _
ByVal keySize As Integer) _
As String

cipherText = cipherText.Replace("~", "+")

Dim initVectorBytes As Byte()
initVectorBytes = Encoding.ASCII.GetBytes(initVector)

Dim saltValueBytes As Byte()
saltValueBytes = Encoding.ASCII.GetBytes(saltValue)

Dim cipherTextBytes As Byte()
cipherTextBytes = Convert.FromBase64String(cipherText)

Dim password As PasswordDeriveBytes
password = New PasswordDeriveBytes(passPhrase, _
saltValueBytes, _
hashAlgorithm, _
passwordIterations)

Dim keyBytes As Byte()
keyBytes = password.GetBytes(keySize / 8)

Dim symmetricKey As RijndaelManaged
symmetricKey = New RijndaelManaged

symmetricKey.Mode = CipherMode.CBC

Dim decryptor As ICryptoTransform
decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

Dim memoryStream As MemoryStream
memoryStream = New MemoryStream(cipherTextBytes)

Dim cryptoStream As CryptoStream
cryptoStream = New CryptoStream(memoryStream, _
decryptor, _
CryptoStreamMode.Read)

Dim plainTextBytes As Byte()
ReDim plainTextBytes(cipherTextBytes.Length)

Dim decryptedByteCount As Integer
decryptedByteCount = cryptoStream.Read(plainTextBytes, _
0, _
plainTextBytes.Length)

memoryStream.Close()
cryptoStream.Close()

Dim plainText As String
plainText = Encoding.UTF8.GetString(plainTextBytes, _
0, _
decryptedByteCount)

Decrypt = plainText
End Function

Public Function Decrypt(ByVal Data As String) As String
Try
Return Decrypt(Data, _
passPhrase, _
saltValue, _
hashAlgorithm, _
passwordIterations, _
initVector, _
keySize)
Catch ex As Exception
Throw ex
End Try
End Function
End Class
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Symmetric key encryption and decryption using Rijndael algorithm
 
Similar Threads
Sending a string from vb to java servlet
AES decryption problem
javax.crypto.BadPaddingException: Given final block not properly padded
Encrypting Strings
RSA Cipher