Different result Encrypt C# Java





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















When I try to convert code form Java to C#, i have a big problem with EncryptFunction.
I have this code in Java:



    private String KEY_PRIVATE_HP = "__hpcSecretKey__";

private IvParameterSpec getIvParameterSpec() {
byte empty = new byte[16];
return new IvParameterSpec(empty);
}

private String encryptBin(String value) {
SecretKeySpec secretKeySpec = new SecretKeySpec(KEY_PRIVATE_HP.getBytes("UTF-8"),"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, getIvParameterSpec());

byte encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeToString(encrypted, Base64.NO_WRAP);
}


And i converted it to C# like that:



        static string  KEY_PRIVATE_HP = "__hpcSecretKey__";

public static string encryptBin(string value)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;

rijndaelCipher.KeySize = 0x80;
rijndaelCipher.BlockSize = 0x80;
byte pwdBytes = Encoding.UTF8.GetBytes(KEY_PRIVATE_HP);
byte keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte plainText = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
}


But when i debug in C# and Java together, it have different result.
What's wrong?










share|improve this question


















  • 1





    You are using PKCS5 in Java and PKCS7 in C#.

    – iakovos Gurulian
    Jan 3 at 12:03








  • 3





    @iakovosGurulian Those are the same

    – Maarten Bodewes
    Jan 3 at 12:04






  • 1





    The IV is not set in JAVA.

    – kelalaka
    Jan 3 at 12:06











  • @kelalaka can i remove or set IV same with Java in C#?

    – Trần Nhật Trường
    Jan 3 at 12:27











  • Normally, as Maarten noted, the salt must be generated randomly and usually prepended to ciphertext. You can set but you have it manually. AFAIK, you are trying to C# to Java and Java to C#. Better first try encrypted Java and Decrypt in C#, then v.s. So that, you can extract the prepended IV form the ciphertext.

    – kelalaka
    Jan 3 at 12:31


















0















When I try to convert code form Java to C#, i have a big problem with EncryptFunction.
I have this code in Java:



    private String KEY_PRIVATE_HP = "__hpcSecretKey__";

private IvParameterSpec getIvParameterSpec() {
byte empty = new byte[16];
return new IvParameterSpec(empty);
}

private String encryptBin(String value) {
SecretKeySpec secretKeySpec = new SecretKeySpec(KEY_PRIVATE_HP.getBytes("UTF-8"),"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, getIvParameterSpec());

byte encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeToString(encrypted, Base64.NO_WRAP);
}


And i converted it to C# like that:



        static string  KEY_PRIVATE_HP = "__hpcSecretKey__";

public static string encryptBin(string value)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;

rijndaelCipher.KeySize = 0x80;
rijndaelCipher.BlockSize = 0x80;
byte pwdBytes = Encoding.UTF8.GetBytes(KEY_PRIVATE_HP);
byte keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte plainText = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
}


But when i debug in C# and Java together, it have different result.
What's wrong?










share|improve this question


















  • 1





    You are using PKCS5 in Java and PKCS7 in C#.

    – iakovos Gurulian
    Jan 3 at 12:03








  • 3





    @iakovosGurulian Those are the same

    – Maarten Bodewes
    Jan 3 at 12:04






  • 1





    The IV is not set in JAVA.

    – kelalaka
    Jan 3 at 12:06











  • @kelalaka can i remove or set IV same with Java in C#?

    – Trần Nhật Trường
    Jan 3 at 12:27











  • Normally, as Maarten noted, the salt must be generated randomly and usually prepended to ciphertext. You can set but you have it manually. AFAIK, you are trying to C# to Java and Java to C#. Better first try encrypted Java and Decrypt in C#, then v.s. So that, you can extract the prepended IV form the ciphertext.

    – kelalaka
    Jan 3 at 12:31














0












0








0








When I try to convert code form Java to C#, i have a big problem with EncryptFunction.
I have this code in Java:



    private String KEY_PRIVATE_HP = "__hpcSecretKey__";

private IvParameterSpec getIvParameterSpec() {
byte empty = new byte[16];
return new IvParameterSpec(empty);
}

private String encryptBin(String value) {
SecretKeySpec secretKeySpec = new SecretKeySpec(KEY_PRIVATE_HP.getBytes("UTF-8"),"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, getIvParameterSpec());

byte encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeToString(encrypted, Base64.NO_WRAP);
}


And i converted it to C# like that:



        static string  KEY_PRIVATE_HP = "__hpcSecretKey__";

public static string encryptBin(string value)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;

rijndaelCipher.KeySize = 0x80;
rijndaelCipher.BlockSize = 0x80;
byte pwdBytes = Encoding.UTF8.GetBytes(KEY_PRIVATE_HP);
byte keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte plainText = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
}


But when i debug in C# and Java together, it have different result.
What's wrong?










share|improve this question














When I try to convert code form Java to C#, i have a big problem with EncryptFunction.
I have this code in Java:



    private String KEY_PRIVATE_HP = "__hpcSecretKey__";

private IvParameterSpec getIvParameterSpec() {
byte empty = new byte[16];
return new IvParameterSpec(empty);
}

private String encryptBin(String value) {
SecretKeySpec secretKeySpec = new SecretKeySpec(KEY_PRIVATE_HP.getBytes("UTF-8"),"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, getIvParameterSpec());

byte encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeToString(encrypted, Base64.NO_WRAP);
}


And i converted it to C# like that:



        static string  KEY_PRIVATE_HP = "__hpcSecretKey__";

public static string encryptBin(string value)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;

rijndaelCipher.KeySize = 0x80;
rijndaelCipher.BlockSize = 0x80;
byte pwdBytes = Encoding.UTF8.GetBytes(KEY_PRIVATE_HP);
byte keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte plainText = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length));
}


But when i debug in C# and Java together, it have different result.
What's wrong?







java c# encryption






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 12:01









Trần Nhật TrườngTrần Nhật Trường

82




82








  • 1





    You are using PKCS5 in Java and PKCS7 in C#.

    – iakovos Gurulian
    Jan 3 at 12:03








  • 3





    @iakovosGurulian Those are the same

    – Maarten Bodewes
    Jan 3 at 12:04






  • 1





    The IV is not set in JAVA.

    – kelalaka
    Jan 3 at 12:06











  • @kelalaka can i remove or set IV same with Java in C#?

    – Trần Nhật Trường
    Jan 3 at 12:27











  • Normally, as Maarten noted, the salt must be generated randomly and usually prepended to ciphertext. You can set but you have it manually. AFAIK, you are trying to C# to Java and Java to C#. Better first try encrypted Java and Decrypt in C#, then v.s. So that, you can extract the prepended IV form the ciphertext.

    – kelalaka
    Jan 3 at 12:31














  • 1





    You are using PKCS5 in Java and PKCS7 in C#.

    – iakovos Gurulian
    Jan 3 at 12:03








  • 3





    @iakovosGurulian Those are the same

    – Maarten Bodewes
    Jan 3 at 12:04






  • 1





    The IV is not set in JAVA.

    – kelalaka
    Jan 3 at 12:06











  • @kelalaka can i remove or set IV same with Java in C#?

    – Trần Nhật Trường
    Jan 3 at 12:27











  • Normally, as Maarten noted, the salt must be generated randomly and usually prepended to ciphertext. You can set but you have it manually. AFAIK, you are trying to C# to Java and Java to C#. Better first try encrypted Java and Decrypt in C#, then v.s. So that, you can extract the prepended IV form the ciphertext.

    – kelalaka
    Jan 3 at 12:31








1




1





You are using PKCS5 in Java and PKCS7 in C#.

– iakovos Gurulian
Jan 3 at 12:03







You are using PKCS5 in Java and PKCS7 in C#.

– iakovos Gurulian
Jan 3 at 12:03






3




3





@iakovosGurulian Those are the same

– Maarten Bodewes
Jan 3 at 12:04





@iakovosGurulian Those are the same

– Maarten Bodewes
Jan 3 at 12:04




1




1





The IV is not set in JAVA.

– kelalaka
Jan 3 at 12:06





The IV is not set in JAVA.

– kelalaka
Jan 3 at 12:06













@kelalaka can i remove or set IV same with Java in C#?

– Trần Nhật Trường
Jan 3 at 12:27





@kelalaka can i remove or set IV same with Java in C#?

– Trần Nhật Trường
Jan 3 at 12:27













Normally, as Maarten noted, the salt must be generated randomly and usually prepended to ciphertext. You can set but you have it manually. AFAIK, you are trying to C# to Java and Java to C#. Better first try encrypted Java and Decrypt in C#, then v.s. So that, you can extract the prepended IV form the ciphertext.

– kelalaka
Jan 3 at 12:31





Normally, as Maarten noted, the salt must be generated randomly and usually prepended to ciphertext. You can set but you have it manually. AFAIK, you are trying to C# to Java and Java to C#. Better first try encrypted Java and Decrypt in C#, then v.s. So that, you can extract the prepended IV form the ciphertext.

– kelalaka
Jan 3 at 12:31












1 Answer
1






active

oldest

votes


















3














In one code you are using an all zero byte IV, in the other you copy the key bytes.



Both are insecure, for CBC mode the IV should be unpredictable; usually secure random values are used and prefixed to the ciphertext.



You may want to have a look at authenticated ciphers such as GCM. Using CBC between two programs for transport security is insecure.






share|improve this answer
























  • I want to change code in C#, i try to set rijndaelCipher.IV = new byte[0x00] but it go to Exception: Specified initialization vector (IV) does not match the block size for this algorithm. How can i fix?

    – Trần Nhật Trường
    Jan 3 at 12:16











  • You need 16 bytes (the block size in bytes) of zeros. new byte[0x00] creates a zero length (or empty) byte array. Note that you can use back-ticks `code` to indicate inline code.

    – Maarten Bodewes
    Jan 3 at 12:17













  • I'm not clearly, what exactly i will do? Because the first time i code with Encrypt

    – Trần Nhật Trường
    Jan 3 at 12:24











  • Well, new byte[16] possibly? C# arrays are initialized to zero.

    – Maarten Bodewes
    Jan 7 at 8:47












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021907%2fdifferent-result-encrypt-c-sharp-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














In one code you are using an all zero byte IV, in the other you copy the key bytes.



Both are insecure, for CBC mode the IV should be unpredictable; usually secure random values are used and prefixed to the ciphertext.



You may want to have a look at authenticated ciphers such as GCM. Using CBC between two programs for transport security is insecure.






share|improve this answer
























  • I want to change code in C#, i try to set rijndaelCipher.IV = new byte[0x00] but it go to Exception: Specified initialization vector (IV) does not match the block size for this algorithm. How can i fix?

    – Trần Nhật Trường
    Jan 3 at 12:16











  • You need 16 bytes (the block size in bytes) of zeros. new byte[0x00] creates a zero length (or empty) byte array. Note that you can use back-ticks `code` to indicate inline code.

    – Maarten Bodewes
    Jan 3 at 12:17













  • I'm not clearly, what exactly i will do? Because the first time i code with Encrypt

    – Trần Nhật Trường
    Jan 3 at 12:24











  • Well, new byte[16] possibly? C# arrays are initialized to zero.

    – Maarten Bodewes
    Jan 7 at 8:47
















3














In one code you are using an all zero byte IV, in the other you copy the key bytes.



Both are insecure, for CBC mode the IV should be unpredictable; usually secure random values are used and prefixed to the ciphertext.



You may want to have a look at authenticated ciphers such as GCM. Using CBC between two programs for transport security is insecure.






share|improve this answer
























  • I want to change code in C#, i try to set rijndaelCipher.IV = new byte[0x00] but it go to Exception: Specified initialization vector (IV) does not match the block size for this algorithm. How can i fix?

    – Trần Nhật Trường
    Jan 3 at 12:16











  • You need 16 bytes (the block size in bytes) of zeros. new byte[0x00] creates a zero length (or empty) byte array. Note that you can use back-ticks `code` to indicate inline code.

    – Maarten Bodewes
    Jan 3 at 12:17













  • I'm not clearly, what exactly i will do? Because the first time i code with Encrypt

    – Trần Nhật Trường
    Jan 3 at 12:24











  • Well, new byte[16] possibly? C# arrays are initialized to zero.

    – Maarten Bodewes
    Jan 7 at 8:47














3












3








3







In one code you are using an all zero byte IV, in the other you copy the key bytes.



Both are insecure, for CBC mode the IV should be unpredictable; usually secure random values are used and prefixed to the ciphertext.



You may want to have a look at authenticated ciphers such as GCM. Using CBC between two programs for transport security is insecure.






share|improve this answer













In one code you are using an all zero byte IV, in the other you copy the key bytes.



Both are insecure, for CBC mode the IV should be unpredictable; usually secure random values are used and prefixed to the ciphertext.



You may want to have a look at authenticated ciphers such as GCM. Using CBC between two programs for transport security is insecure.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 12:07









Maarten BodewesMaarten Bodewes

63.6k1185178




63.6k1185178













  • I want to change code in C#, i try to set rijndaelCipher.IV = new byte[0x00] but it go to Exception: Specified initialization vector (IV) does not match the block size for this algorithm. How can i fix?

    – Trần Nhật Trường
    Jan 3 at 12:16











  • You need 16 bytes (the block size in bytes) of zeros. new byte[0x00] creates a zero length (or empty) byte array. Note that you can use back-ticks `code` to indicate inline code.

    – Maarten Bodewes
    Jan 3 at 12:17













  • I'm not clearly, what exactly i will do? Because the first time i code with Encrypt

    – Trần Nhật Trường
    Jan 3 at 12:24











  • Well, new byte[16] possibly? C# arrays are initialized to zero.

    – Maarten Bodewes
    Jan 7 at 8:47



















  • I want to change code in C#, i try to set rijndaelCipher.IV = new byte[0x00] but it go to Exception: Specified initialization vector (IV) does not match the block size for this algorithm. How can i fix?

    – Trần Nhật Trường
    Jan 3 at 12:16











  • You need 16 bytes (the block size in bytes) of zeros. new byte[0x00] creates a zero length (or empty) byte array. Note that you can use back-ticks `code` to indicate inline code.

    – Maarten Bodewes
    Jan 3 at 12:17













  • I'm not clearly, what exactly i will do? Because the first time i code with Encrypt

    – Trần Nhật Trường
    Jan 3 at 12:24











  • Well, new byte[16] possibly? C# arrays are initialized to zero.

    – Maarten Bodewes
    Jan 7 at 8:47

















I want to change code in C#, i try to set rijndaelCipher.IV = new byte[0x00] but it go to Exception: Specified initialization vector (IV) does not match the block size for this algorithm. How can i fix?

– Trần Nhật Trường
Jan 3 at 12:16





I want to change code in C#, i try to set rijndaelCipher.IV = new byte[0x00] but it go to Exception: Specified initialization vector (IV) does not match the block size for this algorithm. How can i fix?

– Trần Nhật Trường
Jan 3 at 12:16













You need 16 bytes (the block size in bytes) of zeros. new byte[0x00] creates a zero length (or empty) byte array. Note that you can use back-ticks `code` to indicate inline code.

– Maarten Bodewes
Jan 3 at 12:17







You need 16 bytes (the block size in bytes) of zeros. new byte[0x00] creates a zero length (or empty) byte array. Note that you can use back-ticks `code` to indicate inline code.

– Maarten Bodewes
Jan 3 at 12:17















I'm not clearly, what exactly i will do? Because the first time i code with Encrypt

– Trần Nhật Trường
Jan 3 at 12:24





I'm not clearly, what exactly i will do? Because the first time i code with Encrypt

– Trần Nhật Trường
Jan 3 at 12:24













Well, new byte[16] possibly? C# arrays are initialized to zero.

– Maarten Bodewes
Jan 7 at 8:47





Well, new byte[16] possibly? C# arrays are initialized to zero.

– Maarten Bodewes
Jan 7 at 8:47




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021907%2fdifferent-result-encrypt-c-sharp-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory