Posts

Go Language - RSA Encryption And Decryption PKCS Example

Image
The RSA algorithm (Rivest-Shamir-Adleman) is a cryptographic algorithm that is used for specific security services or purposes, which enables public-key encryption and is widely used to secure sensitive data, particularly when it is being sent over an insecure network such as the HTTP. A public key is shared publicly, while a private key is secret and must not be shared with anyone. The following illustration highlights how asymmetric cryptography works: RSA Encryption and Decryption with PKCS1v15 Example, package main //Required imports for Encryption & Decryption import ( "crypto/rand" "crypto/rsa" "encoding/base64" "fmt" ) func main() { //2048 is the number of bits for RSA bitSize := 2048 //Generate RSA keys privateKey, err := rsa. GenerateKey(rand.Reader, bitSize) if err != nil { panic(err) } publicKey := privateKey.PublicKey //Your secret text secretMessage := "

Go - Palindrome Program Example

Image
If a number/string, which when read in both forward and backward way is the same, then such a number is called a palindrome number. Example 1. Go Program to check whether the given number is Palindrome or not //Check Whether a Number is Palindrome or Not package main import "fmt" // Main function func main() { number1 := 121 number2 := 125 number3 := 125521 IsPalindrome(number1) IsPalindrome(number2) IsPalindrome(number3) } func IsPalindrome(number int ) { var lastDigit int sum := 0 a := number // Code to reverse a number for a != 0 { lastDigit = a % 10 //getting remainder sum = (sum * 10 ) + lastDigit a = a / 10 } /*If given number equal to sum than number is palindrome otherwise not palindrome*/ if sum == number { fmt.Println(number, " is palindrome " ) } else { fmt.Println(number, " is not palindrome" ) } } Output: Example 2. Go Progra

Go Arrays - Program to find the duplicate elements of an Array

Image
Go program to find the duplicate numbers in an Array // Go program to find duplicate numbers in an Array package main import "fmt" // Main function func main() { // Let’s define an array first arr := [10] int { 33 , 2 , 1 , 22 , 2 , 6 , 3 , 1 , 33 , 9 } for i := 0 ; i < len(arr); i++ { for j := i + 1 ; j < len(arr); j++ { if arr[i] == arr[j] { fmt.Println(arr[j]) } } } } Output: Go program to find the duplicate words in an Array // Go program to find duplicate words in an Array package main import "fmt" // Main function func main() { // Let’s define an array first arr := [ 10 ] string { "Go" , "Java" , "Javascript" , "Python" , "Php" , "Kotlin" , "Vue" , "C" , "Go" , "Python" , } for i := 0 ; i < len(arr); i++ { for j := i + 1 ; j

Go - Factorial Program using loop

Image
We can find the factorial of a number n by multiplying it with its predecessors up to 1. For example, if we have to find factorial of 4, the equation will look like this: 4 ! = 4*3*2*1 = 24 . We can find the factorial of a number n by multiplying it with its predecessors up to 1. For example, if we have to find factorial of 4, the equation will look like this: Example 1. Go program to find factorial of a given number, package main import "fmt" func main() { number := 5 fmt.Println( "Factorial of the number:" , Factorial(number)) } func Factorial(number int ) int { fact := 1 for i := 1 ; i <= number; i++ { fact *= i } return fact } Output: Example 2. Go program to find factorial of a number from standard input, package main import "fmt" func main() { var number int fmt.Println( "Enter an integer value : " ) _, err := fmt.Scanf( "%d" , &number) if err != nil {

Go Program to print Odd and Even Numbers from an Array

Image
We can print odd and even numbers from an array in Go by getting the remainder of each element and checking if it is divided by 2 or not. If it is divided by 2, it is an even number otherwise it is an odd number. Example 1. //Program to print odd & even numbers from an Array package main import "fmt" // Main function func main() { numbers := [8] int { 2 , 11 , 22 , 5 , 6 , 3 , 1 , 9 } fmt.Println( "Even Numbers:" ) for i := 0 ; i < len(numbers); i++ { if numbers[i]% 2 == 0 { fmt.Println(numbers[i]) } } fmt.Println( "Odd Numbers:" ) for i := 0 ; i < len(numbers); i++ { if numbers[i]% 2 != 0 { fmt.Println(numbers[i]) } } } Output: Example 2. Using Slices //Program to print odd & even numbers from an Array package main import "fmt" // Main function func main() { numbers := [8] int { 2 , 11 , 22 , 5 , 6 , 3 , 1 , 9 } //a slice of un