• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Symmetric key encryption and decryption using Rijndael algorithm

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Right! We're on it! Let's get to work tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic