Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Symmetric key encryption and decryption using Rijndael algorithm

 
anshu bajpai
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic