Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6779165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:21:10+00:00 2026-05-26T16:21:10+00:00

What I’m trying to do is use SHA1 UTF-8 encryption and then base64 encoding

  • 0

What I’m trying to do is use SHA1 UTF-8 encryption and then base64 encoding and on a password string value. However, I needed to do the encryption first, then the encoding, but I did it the other way around.

Here is the code:

# Create Input Data 
$enc = [system.Text.Encoding]::UTF8
$string1 = "This is a string to hash" 
$data1 = $enc.GetBytes($string1) 

# Create a New SHA1 Crypto Provider 
$sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 

$# Now hash and display results 
$result1 = $sha.ComputeHash($data1) 

So, when I went to do the hashing I realized I had to have a byte[] from the string and I’m not sure how to do that. I’m thinking there is a simple way from the .Net libraries, but couldn’t find an example.

So if I have a string, like:

$string = "password"

How do I convert that into a byte array that I can use on :: ComputeHash($string)?

So what I have to end up with is an encrypted SHA-1 and base 64 encoded UTF-8 password, which the code above does, but it’s coming back different than when I coded this same thing in java, where I encrypted it first, then converted that result to base 64 encoding.

I’m making the assumption that while encrypting a string directly isn’t supported in the api, there may be a work-around that will allow you to do this. That is what I’m attempting to do.

So I’m assuming my issue with the code is that I had to encrypt it first and then encode it to get the correct value. Correct or am I missing something here?

Here is the pertinent java code that does work:

//First method call uses a swing component to get the user entered password.
String password = getPassword(); 

//This line is where the work starts with the second and third methods below.
String hashed = byteToBase64(getHash(password));

//The second method call here gets the encryption.
public static byte[] getHash(String password) {
      MessageDigest digest = null;
      byte[] input = null;
      try {
             digest = MessageDigest.getInstance("SHA-1");
      } catch (NoSuchAlgorithmException e1) {
             e1.printStackTrace();
      }
      digest.reset();
      try {
             input = digest.digest(password.getBytes("UTF-8"));
      } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
      }
      return input;
}

//Then the third method call here gets the encoding, FROM THE ENCRYPTED STRING.
public static String byteToBase64(byte[] data){
    return new String(Base64.encodeBase64(data));

When I run the java code with the password string of “password” I get

[91, -86, 97, -28, -55, -71, 63, 63, 6, -126, 37, 11, 108, -8, 51, 27, 126, -26, -113, -40]
which is the encryption.

Then I when the encoding in java I get this:
W6ph5Mm5Pz8GgiULbPgzG37mj9g=

but when I run it in PowerShell I get this because it’s encoded first for UTF8:

91 170 97 228 201 185 63 63 6 130 37 11 108 248 51 27 126 230 143 216

Then when I run this line of code to convert it I get an error:

$base64 = [System.Convert]::FromBase64String($result)

Exception calling “FromBase64String” with “1” argument(s): “Invalid length for a Base-64 char array.”
At line:1 char:45

However, if I run the new line of code to make it hex from below I get:

$hexResult = [String]::Join("", ($result | % { "{0:X2}" -f $_}))
PS C:\Program Files (x86)\PowerGUI> Write-Host $hexResult

5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8

but I need to end up with this value:

W6ph5Mm5Pz8GgiULbPgzG37mj9g=

Again, this may not even be possible to do, but I’m trying to find a work-around to see.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:21:11+00:00Added an answer on May 26, 2026 at 4:21 pm

    You most likely just need to convert your hash to base64 after the last line.

    $enc = [system.Text.Encoding]::UTF8
    $string1 = "This is a string to hash" 
    $data1 = $enc.GetBytes($string1) 
    
    # Create a New SHA1 Crypto Provider 
    $sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 
    
    # Now hash and display results 
    $result1 = $sha.ComputeHash($data1)
    [System.Convert]::ToBase64String($result1)
    

    Text->Bytes->Encrypt/Hash->Base64

    That’s a very common pattern for sending cryptographic data in a text format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.