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 8715735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:55:01+00:00 2026-06-13T05:55:01+00:00

I am trying to encrypt the same data using C# and Java. If the

  • 0

I am trying to encrypt the same data using C# and Java. If the data is more than 7 bytes then Java and C#’s encrypted value are not identical.

  • Input 1: a
    java output: FrOzOp/2Io8=
    C# output: FrOzOp/2Io8=

  • Input 2: abc
    j : H9A/ahl8K7I=
    c#: H9A/ahl8K7I=

  • Input 3: aaaaaaaa (Problem)
    j : Gxl7e0aWPd7j6l7uIEuMxA==
    c#: Gxl7e0aWPd7sf1xR6hK4VQ==

Here is the implementation of C# and Java methods.
C# code:

public String saltTxt = "12345678";
public String Encrypt(String txt)
{
        byte[] data = Encrypt(Encoding.UTF8.GetBytes(txt));
        
        DESCryptoServiceProvider alg = new DESCryptoServiceProvider();

        alg.Key = Encoding.UTF8.GetBytes(saltTxt.ToCharArray(), 0, cprovider.KeySize / 8);
        alg.IV = new byte[8];

        MemoryStream ms = new MemoryStream();
        CryptoStream stem = new CryptoStream( ms, cprovider.CreateEncryptor(),CryptoStreamMode.Write);

        stem.Write(txt, 0, txt.Length);
        stem.FlushFinalBlock();

        data = ms.ToArray();
        
        return Convert.ToBase64String(data);
 }

Java Code:

public String saltTxt = "12345678";
public String Encrypt(String str) {
    try {
        KeySpec myKey = new DESKeySpec(saltTxt.getBytes("UTF8"));
        SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(myKey);
        Cipher ecipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] data = str.getBytes("UTF8");

        byte[] crypt = ecipher.doFinal(data);
        
        return new BASE64Encoder().encode(crypt);
    } catch (Exception ex) {
    }

    return null;
} 

Any idea why it’s not working as expected?

  • 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-06-13T05:55:02+00:00Added an answer on June 13, 2026 at 5:55 am

    The problem was in mode of encryption.

    SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES, DES-EDE and Blowfish ciphers. (JCA Doc)

    and

    In .Net, The default operation mode for the symmetric algorithm is CipherMode.CBC and default padding is PaddingMode.PKCS7. (msdn..SymmetricAlgorithm)

    The following changes resolve the problem.

    // in C# 
    DESCryptoServiceProvider alg = new DESCryptoServiceProvider();
    alg.Mode = CipherMode.ECB;  // specified 
    

    or

    // in java
    chiper = Cipher.getInstance("DES/CBC/PKCS5Padding");
    

    don’t change in both sides.

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

Sidebar

Related Questions

I'm trying to encrypt an array of 256 bytes by using RSACryptoServiceProvider but I'm
Talking about javax.crypto.Cipher I was trying to encrypt data using Cipher.getInstance(RSA/None/NoPadding, BC) but I
I'm trying to encrypt a string, save it in a file and then later
I am trying to encrypt some data with the following code: public static byte[]
I'm trying to encrypt files using my private key (in ascii format) and any
I am trying to get 2 programs to share encrypted data over a network
Using CF8 and MySQL 5.1, I am trying to encrypt() a password upon creation
I have data encrypted in ColdFusion which I am trying to decrypt in Ruby.
I am trying encrypt and decrypt one column in table. My code is like
I am trying to encrypt and decrypt some simple text. But for some reason

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.