Comparison of encryption techniques on Java and. NET platforms

Source: Internet
Author: User
Tags modulus
Recently, I was writing a Java Message Server. At the same time, I need to build a. NET client. Secure Communication is required between them. Based on some simple cryptographic protocols, public key encryption, symmetric encryption, and Hash algorithms are used. In this process, I have some knowledge about the encryption of these two platforms. Here are some of my new understandings.

1. symmetric encryption
1) symmetric encryption in Java 1.5 is simple and provides many algorithms. It can be said that it is easy to use, silly, and fully functional.
For example: SecretKeySpec skeySpec = new SecretKeySpec (key, "AES ");

Cipher cipher = Cipher. getInstance ("AES ");
Cipher. init (Cipher. DECRYPT_MODE, skeySpec );
Byte [] decryptText = cipher. doFinal (data );

2 ). NET 2.0 symmetric encryption. The default encryption mode is CBC. When CBC is used for encryption, a key is required and the vector IV needs to be initialized, which makes it inconvenient for beginners to use, this problem is very easy to deal with, just modify the configuration. Using ricalgorithm algorithm = Using ricalgorithm. Create (algorithmName );
Algorithm. Mode = CipherMode. ECB;
Algorithm. Key = key;
Algorithm. Padding = PaddingMode. PKCS7;

With this setting, you can communicate with Java to encrypt and decrypt each other.

3) For. NET 2.0 and Java 1.5, the name of the encryption algorithm varies slightly.
AES <=> Rijndael
DESede <=> TripleDES
This seems to be common sense.

2. public key encryption algorithm RSA
1) in Java 1.5, RSAPublicKey performs getEncoded () to get the byte array which is ASN.1 encoded. X509EncodedKeySpec is required for reverse conversion. You need to read the document details or have a certain understanding of cryptography. For example: // Public key => bytes
PublicKey publicKey =
Byte [] rawPublicKey = publicKey. getEncoded ();

// Bytes => public key
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec (rawPublicKey );
KeyFactory keyFactory = KeyFactory. getInstance ("RSA ");
Key newPublicKey = keyFactory. generatePublic (x509KeySpec );

In addition, Java's public key encryption is quite easy to use. The style is still simple, simple, and fully functional.

Java supports ASN.1 encoding, but is invisible to users.

2) In. NET 2.0, the design is somewhat confusing and does not support ASN.1 encoding. However, Mono seems to be supporting ASN.1 encoding. For this reason, I used a Java $ JCE implementation to implement a. NET version of ASN Parser and ASN Builder, which took two days. As follows: Public static RSAParameters ASN1ToPublicKey (byte [] rawPublicKey)
{
ASN1InputStream asnInput = new ASN1InputStream (rawPublicKey );
ASN1Sequence asnSeq = (ASN1Sequence) asnInput. ReadObject ();
SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo (asnSeq );

DERObjectIdentifier algOid = subjectPublicKeyInfo. AlgorithmId. ObjectId;

RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure (
(ASN1Sequence) subjectPublicKeyInfo. PublicKey );

Byte [] modulus = pubKey. Modulus;
Byte [] publicExponent = pubKey. PublicExponent;

RSAParameters pram = new RSAParameters ();
Pram. Modulus = modulus;
Pram. Exponent = publicExponent;

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ();
Rsa. ImportParameters (pram );

Return pram;
}

Public static byte [] PublicKeyToASN1 (RSAParameters pram)
{
SubjectPublicKeyInfo info = new SubjectPublicKeyInfo (
New AlgorithmIdentifier (PKCSObjectIdentifiers. rsaEncryption,
New DERNull (), new RSAPublicKeyStructure (pram. Modulus, pram. Exponent). DERObject );

Byte [] rawPublicKey = info. GetDEREncoded ();
Return rawPublicKey;
}

3. Overall perception
1) the Java security module is well designed, easy to use, and has complete functions.
2). NET 2.0 is a bit messy. The naming style is slightly different from the system framework, the functions are lacking, and the code organization is not ideal.
3) in Mono, the security support is better than that released by Microsoft. from the Internet, we can see that some features of. NET Framework 2.0 are also borrowed from Mono.
4) The Development Team of the. NET encryption module may not be very competent. Just like the phrase "writing bad code is not a patent ".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.