Programming

VB.Net : Check If Given String is EmailID

Public Function IsEmail(ByVal email As String) As Boolean
Dim emailRegex As New Regex(“^([\w-\.]+)@([\w-]+\.)+([\w-]{2,4})$”)

If emailRegex.IsMatch(email) Then
Return True
Else
Return False
End If
End Function

VB.NET : Get Random Number

This function creates a new instance of the Random class, then uses the Next method to generate a random integer between 999 and 9999. Finally, the function returns the generated random number.

You can call this function in your code to generate a random number within the specified range. For example:

Public Function GenerateRandomNumber() As Integer
Dim rnd As New Random()
Dim randomNumber As Integer = rnd.Next(1000, 9999)
Return randomNumber
End Function

vb.net : Handling JSON

Check If Valid JSON

Public Shared Function IsValidJSON(ByVal json As String) As Boolean
Try
JsonConvert.DeserializeObject(json)
Return True
Catch ex As Exception
Return False
End Try
End Function

Public Shared Function IsValidJSON(ByVal json As String) As Boolean
Try
JToken.Parse(json)
Return True
Catch ex As Exception
Return False
End Try
End Function

Check If A Key Exists

Public Shared Function KeyExists(ByVal json As String, ByVal key As String) As Boolean
Try
Dim obj As JObject = JObject.Parse(json)
Return obj.ContainsKey(key)
Catch ex As Exception
Return False
End Try
End Function

Get Value Of A Key if Valid JSON and Key Exists

Public Shared Async Function GetValueFromJSONAsync(ByVal json As String, ByVal key As String) As Task(Of String)
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Return prop.Value.ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function

Public Shared Async Function GetValueFromJSONAsync(ByVal json As String, ByVal key As String) As Task(Of String)
Dim Rtn$ = “”
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Rtn = prop.Value.ToString()
End If
Catch ex As Exception
Rtn = “”
End Try

Return Await Task.FromResult(Of String)(Rtn)
End Function

Public Shared Function GetValueFromJSON(ByVal json As String, ByVal key As String) As String
Try
Dim obj As JObject = JObject.Parse(json)
If obj.ContainsKey(key) Then
Return obj(key).ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function

Uses :

Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim result As String = Await JSONHelper.GetValueFromJSONAsync(json, “name”)
Console.WriteLine(result)

Public Shared Function GetValueFromJSON(ByVal json As String, ByVal key As String) As String
Try
Dim obj As JObject = JObject.Parse(json)
Dim prop As JProperty = obj.Property(key)
If prop IsNot Nothing Then
Return prop.Value.ToString()
Else
Return “Key does not exist”
End If
Catch ex As Exception
Return “Invalid JSON”
End Try
End Function

Dim jsonString As String = “{ “”body””:””om sai””, “”type””:””chat””, “”notifyName””:””US””, “”from””:””919407755200@c.us””, “”to””:””919406214451@c.us””}”
Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(jsonString)
Dim fromValue As String = jsonObject(“from”).ToString()
Console.WriteLine(fromValue)

Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim obj As Object = JsonConvert.DeserializeObject(json)

Console.WriteLine(“Name: ” & obj(“name”))
Console.WriteLine(“Age: ” & obj(“age”))
Console.WriteLine(“City: ” & obj(“city”))

Strongly Typed

Imports Newtonsoft.Json

Public Class Person
Public Property Name As String
Public Property Age As Integer
Public Property City As String
End Class

Dim json As String = “{ “”name””: “”John””, “”age””: 30, “”city””: “”New York”” }”
Dim person As Person = JsonConvert.DeserializeObject(Of Person)(json)

Console.WriteLine(“Name: ” & person.Name)
Console.WriteLine(“Age: ” & person.Age)
Console.WriteLine(“City: ” & person.City)

Generic Handler : Async ASHX File

Imports System.Web
Imports System.Data.SqlClient
Imports System.Threading.Tasks

Public Class AsyncHandler
Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = “text/plain”

Task.Run(Async Sub()
Await AddToLogAsync(context)
Await ReadQueryStringAsync(context)
Await ReadPostFormVariablesAsync(context)
Await CallDatabaseResultAsync(context)
End Sub).GetAwaiter().GetResult()
End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

Private Async Function AddToLogAsync(ByVal context As HttpContext) As Task
‘ Add to log
Await Task.Delay(100)
End Function

Private Async Function ReadQueryStringAsync(ByVal context As HttpContext) As Task
‘ Read query string variables
Await Task.Delay(100)
End Function

Private Async Function ReadPostFormVariablesAsync(ByVal context As HttpContext) As Task
‘ Read post form variables
Await Task.Delay(100)
End Function

Private Async Function CallDatabaseResultAsync(ByVal context As HttpContext) As Task
‘ Call database and get result
Await Task.Delay(100)
End Function
End Class

Encrypt server (asp.net core7) api result to client (flutter)

o encrypt the server API result on the ASP.NET Core side and decrypt it on the Flutter side, you can use a symmetric encryption algorithm, such as AES. Here’s a high-level overview of the process:

On the ASP.NET Core side, you’ll need to generate an AES key and initialize a cipher in encryption mode. Use this cipher to encrypt the API result before sending it to the client.

using System;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Mvc;

namespace EncryptedApiExample.Controllers
{
public class ApiController : Controller
{
[HttpGet]
public IActionResult GetData()
{
// The API result you want to send to the client
string apiResult = “secret data”;

// Generate a random AES key
byte[] key = GenerateAesKey();

// Initialize a cipher in encryption mode
Aes aes = Aes.Create();
aes.Key = key;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform encryptor = aes.CreateEncryptor();

// Encrypt the API result
byte[] encryptedResult = encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(apiResult), 0, apiResult.Length);

// Return the encrypted result and the AES key to the client
return Ok(new { encryptedResult, key });
}

private byte[] GenerateAesKey()
{
// Your implementation for generating a random AES key goes here
// For this example, let’s generate a random key of 128 bits (16 bytes)
byte[] key = new byte[16];
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(key);
}
return key;
}
}
}

On the Flutter side, you’ll need to receive the encrypted result and the AES key, then initialize a cipher in decryption mode to decrypt the result.

import ‘dart:convert’;
import ‘dart:typed_data’;

import ‘package:flutter/widgets.dart’;
import ‘package:pointycastle/pointycastle.dart’;

class DecryptedData with ChangeNotifier {
String _apiResult;

String get apiResult => _apiResult;

set apiResult(String apiResult) {
_apiResult = apiResult;
notifyListeners();
}
}

class DecryptApiResult with ChangeNotifier {
Future decryptResult(Uint8List encryptedResult, Uint8List key) async {
// Initialize a cipher in decryption mode
var aesParams = new KeyParameter(key);
var cipher = new StreamCipher(“AES/ECB/PKCS7”);
cipher.init(false, aesParams);

// Decrypt the API result
Uint8List decryptedResult = cipher.process(encryptedResult);

// Convert the decrypted result to a string
String apiResult = utf8.decode(decryptedResult);

// Update the decrypted data

Sending the encryption key over the network like this is not secure, as it can be intercepted by attackers. To securely encrypt the data, you should use a combination of asymmetric encryption and symmetric encryption.

Here’s a high-level overview of how it could be done:

On the server side, generate a public-private key pair using a secure asymmetric encryption algorithm such as RSA. Share the public key with the client.

On the client side, generate a random symmetric encryption key, such as an AES key, and use the public key to encrypt it.

On the server side, use the private key to decrypt the symmetric encryption key.

Use the symmetric encryption key to encrypt the API result on the server side and send it to the client.

On the client side, use the symmetric encryption key to decrypt the API result.

By using a combination of asymmetric and symmetric encryption, you can ensure that the encryption key is securely transmitted from the client to the server and that the data is encrypted and decrypted using a secure key. This makes it much more difficult for attackers to intercept the key and access the encrypted data.

For More Secureway

using System;
using System.Security.Cryptography;

namespace EncryptionExample
{
public static class RSAKeyGenerator
{
public static void GenerateKeys(out string publicKey, out string privateKey)
{
using (var rsa = RSA.Create())
{
publicKey = Convert.ToBase64String(rsa.ExportSubjectPublicKeyInfo());
privateKey = Convert.ToBase64String(rsa.ExportPkcs8PrivateKey());
}
}
}
}

using System;
using System.IO;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Mvc;

namespace EncryptionExample.Controllers
{
[Route(“api/[controller]”)]
[ApiController]
public class DataController : ControllerBase
{
private static readonly byte[] _symmetricKey = new byte[32];
private static readonly byte[] _iv = new byte[16];

[HttpPost]
public IActionResult GetEncryptedData([FromBody] byte[] encryptedSymmetricKey)
{
using (var rsa = RSA.Create())
{
var privateKey = Convert.FromBase64String(““);
rsa.ImportPkcs8PrivateKey(privateKey, out _);

encryptedSymmetricKey = rsa.Decrypt(encryptedSymmetricKey, RSAEncryptionPadding.OaepSHA256);
}

using (var aes = Aes.Create())
{
aes.Key = encryptedSymmetricKey;
aes.IV = _iv;

using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (var streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(““);
}

return Ok(memoryStream.ToArray());
}
}
}
}
}

In Flutter

import ‘dart:convert’;
import ‘dart:io’;

import ‘package:flutter/material.dart’;
import ‘package:pointycastle/asymmetric/api.dart’;
import ‘package:pointycastle/asymmetric/rsa.dart’;
import ‘package:pointycastle/block/aes_fast.dart’;
import ‘package:pointycastle/key_generators/api.dart’;
import ‘package:pointycastle/random/fortuna_random.dart’;
import ‘package:http/http.dart’ as http;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: RaisedButton(
onPressed: () async {
// Generate a random symmetric encryption key
var keyGenerator = KeyGenerator(“AES”);
keyGenerator.init(KeyParameter(“Fortuna”));
var key = keyGenerator.generateKey();

// Encrypt the symmetric encryption key using the public key
var rsa = RSAEngine();
rsa.init(true, PublicKeyParameter(PublicKey(““)));
var encryptedSymmetricKey = rsa.process(key.keyData);

// Send the encrypted symmetric encryption key to the server
var response = await http.post(““, body: encryptedSymmetricKey);

// Decrypt the API result
var aes = AESFastEngine();
aes.init(false, KeyParameter(key.keyData));
var decryptedData = aes.process(response.bodyBytes);

// Print the decrypted data
print(utf8.decode(decryptedData));
},
child: Text(“Get Encrypted Data”),
),
),
),
);
}
}

If Client is JAVA

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class Main {
public static void main(String[] args) throws Exception {
// Generate a random symmetric encryption key
KeyGenerator keyGenerator = KeyGenerator.getInstance(“AES”);
SecretKey secretKey = keyGenerator.generateKey();

// Encrypt the symmetric encryption key using the public key
byte[] publicKeyBytes = Base64.decodeBase64(““);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance(“RSA/ECB/OAEPWithSHA-1AndMGF1Padding”);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedSymmetricKey = cipher.doFinal(secretKey.getEncoded());

// Send the encrypted symmetric encryption key to the server
// …

// Decrypt the API result
cipher = Cipher.getInstance(“AES”);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), “AES”);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedData = cipher.doFinal();

// Print the decrypted data
System.out.println(new String(decryptedData));
}
}

VB.NET

Imports System.Security.Cryptography
Imports System.Text

Module Main
Sub Main()
Dim publicKey As String = “
Dim encryptedSymmetricKey As Byte() = EncryptSymmetricKey(publicKey)
Dim apiResult As Byte() =
Dim decryptedData As Byte() = DecryptApiResult(encryptedSymmetricKey, apiResult)
Console.WriteLine(Encoding.UTF8.GetString(decryptedData))
End Sub

Private Function EncryptSymmetricKey(publicKey As String) As Byte()
Dim symmetricKey As New RijndaelManaged
symmetricKey.KeySize = 256
symmetricKey.GenerateKey()

Dim publicKeyBytes As Byte() = Convert.FromBase64String(publicKey)
Dim publicKeyObject As New RSACryptoServiceProvider
publicKeyObject.ImportCspBlob(publicKeyBytes)

Return publicKeyObject.Encrypt(symmetricKey.Key, False)
End Function

Private Function DecryptApiResult(encryptedSymmetricKey As Byte(), apiResult As Byte()) As Byte()
Dim symmetricKey As New RijndaelManaged
symmetricKey.Key = encryptedSymmetricKey

Dim decryptor As ICryptoTransform = symmetricKey.CreateDecryptor(symmetricKey.Key, symmetricKey.IV)
Return decryptor.TransformFinalBlock(apiResult, 0, apiResult.Length)
End Function
End Module

Create an encrypted download link for a file in ASP.NET Core:

In your controller, you can generate a unique, encrypted identifier for the file:

using System;
using System.IO;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Mvc;

namespace DownloadFileExample.Controllers
{
public class DownloadController : Controller
{
[HttpGet]
public IActionResult Download(string id)
{
// Decrypt the identifier to get the file path
string filePath = DecryptFileIdentifier(id);

// Check if the file exists
if (!System.IO.File.Exists(filePath))
{
return NotFound();
}

// Return the file as a FileResult
return File(System.IO.File.OpenRead(filePath), “application/pdf”, Path.GetFileName(filePath));
}

private string DecryptFileIdentifier(string encryptedIdentifier)
{
// Your implementation for decrypting the encrypted identifier goes here

// For this example, let’s assume the encrypted identifier is just a base64-encoded string of the file path
return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(encryptedIdentifier));
}
}
}

To create an encrypted download link, you could generate an encrypted identifier for the file and include it in the link:

Download

@functions {
private string EncryptFileIdentifier(string filePath)
{
// Your implementation for encrypting the file path goes here

// For this example, let’s assume the encrypted identifier is just a base64-encoded string of the file path
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(filePath));
}
}

This is just one way to create an encrypted download link for a file in ASP.NET Core. You can use other encryption techniques to encrypt the identifier, such as AES encryption, if needed. The specific encryption technique you choose will depend on the level of security you require for your application.

The client/user will not know the actual web path or file name as the information is encrypted and can only be decrypted by the server. By encoding the file path in the download link, you are effectively obscuring the actual file location from the client/user. This can help to prevent unauthorized access to the file and protect sensitive information.

In the example I provided earlier, the file path is encrypted using a base64 encoding and included in the download link. When the user clicks the link, the encrypted identifier is passed to the Download action in the controller. The controller then uses the DecryptFileIdentifier method to decrypt the identifier and retrieve the actual file path. Finally, the file is returned to the client/user as a FileResult.

Scroll to Top