Why RSA encryption can return different results with C# and Java?

Why RSA encryption can return different results with C# and Java?

By : Daniel Buckley
Date : November 20 2020, 07:00 PM
should help you out RSA encryption is randomized. For a given public key and a given message, each attempt at encryption yields a distinct sequence of bytes. This is normal and expected; random bytes are injected as part of the padding phase, and not injecting random bytes would result in a weak encryption system. During decryption, the padding bytes are located and removed, and the original message is recovered unscathed.
Hence it is expected that you will get distinct encrypted messages with Java and C#, but also if you run your Java or C# code twice.
code :

Share : facebook icon twitter icon
PHP Java AES CBC Encryption Different Results

PHP Java AES CBC Encryption Different Results

By : Ramdeo Angh
Date : March 29 2020, 07:55 AM
seems to work fine You'd have had a better idea of what was going on if you didn't simply swallow up possible Exceptions inside your encrypt() routine. If your function is returning null then clearly an exception happened and you need to know what it was.
In fact, the exception is:
code :
javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes
    at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:854)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:828)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
    at javax.crypto.Cipher.doFinal(Cipher.java:2087)
    at Encryption.encrypt(Encryption.java:20)
    at Encryption.main(Encryption.java:6)
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Encryption
    public static void main(String args[]) throws Exception {

    public static String encrypt() throws Exception {
        try {
            String data = "Test string";
            String key = "1234567812345678";
            String iv = "1234567812345678";

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            int blockSize = cipher.getBlockSize();

            // We need to pad with zeros to a multiple of the cipher block size,
            // so first figure out what the size of the plaintext needs to be.
            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            int remainder = plaintextLength % blockSize;
            if (remainder != 0) {
                plaintextLength += (blockSize - remainder);

            // In java, primitive arrays of integer types have all elements
            // initialized to zero, so no need to explicitly zero any part of
            // the array.
            byte[] plaintext = new byte[plaintextLength];

            // Copy our actual data into the beginning of the array.  The
            // rest of the array is implicitly zero-filled, as desired.
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);

            return new sun.misc.BASE64Encoder().encode(encrypted);

        } catch (Exception e) {
            return null;
return new sun.misc.BASE64Encoder().encode(encrypted);
return Base64.Encoder.encodeToString(encrypted);
Java SimpleCrypto Class for encryption / decryption producing different results in Coldfusion 9 and Java (Android)

Java SimpleCrypto Class for encryption / decryption producing different results in Coldfusion 9 and Java (Android)

By : Nev
Date : March 29 2020, 07:55 AM
This might help you The getRawKey() method is flawed. It uses an instance of SecureRandom instead of a key derivation function (KDF).
Depending on the implementation, the setSeed() method will either add the seed to the current state or it will use it as the only seed. The Oracle provider in Java SE 7 and before will use it as the single seed, other providers such as those based on OpenSSL in the latest versions of Android may simply add the seed to the state. This means that the retrieved key may indeed be entirely random; anything encrypted with it can therefore not be decrypted, ever.
C# & JS encryption methods return different results

C# & JS encryption methods return different results

By : initial ha
Date : March 29 2020, 07:55 AM
wish of those help Finally got a solution. to Luke Park who advised me that CryptoJS is using 4-byte (word) structure.
The issue which led to different results is that the key-size is (apparently) different. In C# 256/8 is actually 32 bytes length, however in CryptoJS, since using 4-bytes, 256/8 translates practically to 128 bytes length :(
C# & Javascript DES encryption return different results

C# & Javascript DES encryption return different results

By : mohammad barandjam
Date : March 29 2020, 07:55 AM
may help you . I have this legacy C# code that I need to move to Javascript: , This is what I found and solved the problem.
code :
function encodeDesCBC(textToEncode, keyString, ivString) {
    var key = new Buffer(keyString.substring(0, 8), 'utf8');
    var iv = new Buffer(ivString.substring(0,8), 'utf8');
    var cipher = crypto.createCipheriv('des-cbc', key, iv);
    var c = cipher.update(textToEncode, 'utf8', 'base64');
    c += cipher.final('base64');
    return c;
Different AES Encryption results in PHP and JAVA

Different AES Encryption results in PHP and JAVA

By : user5361678
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , It is hard to tell what the default options (padding, mode) are when they are not explicitly specified in the initialization string: Cipher.getInstance("AES").
AES has a block size of 128.bits so you need to specift that to mcrypt: MCRYPT_RIJNDAEL_128. MCRYPT_RIJNDAEL_256 is not AES.
Related Posts Related Posts :
  • How to use Selenium Grid with C#?
  • What is the best way to download files via HTTP using .NET?
  • How to get files from a device using USB
  • Given a user's SID, how do I get their userPrincipalName?
  • NHibernate mapping in Asp.Net using MySql
  • Why do some cookies have a '.' before the domain?
  • C# SqlDataReader = null?
  • InvalidCastException for two Objects of the same type
  • "The parameters dictionary contains a null entry for parameter" - How to fix?
  • Font family name from font file
  • What is the best way to generate KML files in C#?
  • How can I receive mail using .NET?
  • How to send raw data over a network?
  • meaning of '+='
  • Object reference not set to an instance of an object #5
  • C# Create "wireframe"/3D "map"
  • How to change size of database
  • Serialization problem
  • Using unmanaged code from managed code
  • Are there any bindings between .NET and TK
  • error with linq join
  • VB.NET equivalent to C# var keyword
  • Accessing object properties from string representations
  • Inheritance issue
  • C# timer won't tick
  • How to retrieve items from a database c#
  • Sending mail using SmtpClient in .net
  • Tag problem c# listbox
  • How to know if the Form App open or not c#
  • C# XPath id() not working?
  • Load PDF from Memory ASP.Net
  • C# ListView with a ProgressBar
  • Getting the right WPF dispatcher in a thread
  • How to create Pivot table using C#?
  • how to download a file from remote server using asp.net
  • Binding files in C#?
  • Copy one object to another
  • How to post on Google Buzz?
  • Generic <T> how cast?
  • Set global hotkeys using C#
  • Change the key being pressed with C#
  • Uploading Large Files
  • How do I get the duration of a video file using C#?
  • how to create instance for a generic type in c#
  • Drag and drop rectangle in C#
  • RSA Encryption C#
  • Title=
  • What is meant by Web Services?
  • The provided URI scheme 'https' is invalid; expected 'http'. Parameter name: via
  • Check if server exists
  • time interval in c#
  • Extracting a sub-string in C#
  • C# - Programmatically Log-off and Log-on a user
  • c# array vs generic list
  • TCPClient in C# (Error)
  • How can I know if a file has been changed in .NET C#?
  • New to C# and trying to use a global variable
  • Convert RGB color to CMYK?
  • Tesseract.NET in C#
  • Is it possible to Update Sharepoint List Without "ID"?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk