C2java Article 0th

Source: Internet
Author: User
Tags array definition

Recently, due to business relationships, we started learning java. Because of the background of C, it is preemptible and compared with C.

/* Struct. four elements of java -- how C to java * OO: abstraction, encapsulation, modularization, and layering * Java advantages: * Security: * 1. the reference must be initialized. * 2. array subscript check; * 3. stackTrace is printed elegantly when a machine crashes. * Other advantages will be discussed later. ** Author: ludi 2014.03 */import java. lang. system;/* directory hierarchy, C does not support the */class Type {/* Struct abstract encapsulation in C */public int a, B; public void change () {/* Struct */this in C ++. a + = 1; this. B + = 2 ;}} public class Struct {/* must be the same as the file name: the compiler enforces modularity */static void change (Type t) {/* The function irrelevant to the object instance must be static */t. a + = 1;/* Reference transfer in C ++ */t. B + = 2;/* how do I print the referenced memory address? */Int [] x = {1, 2, 3, 4};/* the array definition is more consistent than int [4] in C */Type [] y = {null, null, null};/* is the array y allocated on the stack? How to release? */} Public static void main (String [] args) {Type t = new Type ();/* the compiler forces the reference to be initialized * // System. printf ("a = % d B = % d \ n", t. a, t. b);/**/System. out. println (t. a + "" + t. b);/* secure printing */change (t);/* Reference is Pointer */t. change ();/* advocate OO style */System. out. println (t. a + "" + t. B );}}


In the past, I followed the trend when others said that the java syntax was too long-winded, especially when I first read some of them and wrote Mo's design pattern. It seems that the person who writes the code has not completed the abstraction step. Of course, you have to check that hello world is the shortest, and C is not as good as perl.

From a pragmatic point of view, give the first application example RSA encryption and decryption:

/* RSA. java -- using RSA do encryption and decryption * java advantages: * standard package: BigInteger ** author: ludi 2014.03 **/import java. math. *; import java. util. *; public class RSA {public static BigInteger relativePrime (BigInteger t) {/* return number k, such that gcd (k, t) = 1. */BigInteger k = null; Random rnd = new Random (); do {k = new BigInteger (64, rnd);/* generate a random k \ in (0, 2 ^ 64) */} while (! T. gcd (k ). equals (BigInteger. ONE); return k;} public static void main (String [] args) {if (args. length <1) {System. out. println ("usage: RSA message \ n"); return;} Random rnd = new Random (); BigInteger p = null, q = null, n = null, t = null, e = null, d = null, one = BigInteger. ONE, raw = null, encrypted = null, decrypted = null; String msg = null; p = BigInteger. probablePrime (128, rnd);/* bit length = 128 */q = BigInteger. probablePrime (128, rnd); n = p. multiply (q); t = (p. subtract (one )). multiply (q. subtract (one); e = relativePrime (t); d = e. modInverse (t); raw = new BigInteger (args [0]. getBytes ();/* bit stream */encrypted = raw. modPow (e, n); decrypted = encrypted. modPow (d, n); // System. out. println ("raw integer:" + raw // + "\ np =" + p + "\ nq =" + q); System. out. println ("raw message: '" + args [0] + "'"); msg = new String (encrypted. toByteArray (); System. out. println ("encrypted: '" + msg + "'"); msg = new String (decrypted. toByteArray (); System. out. println ("decrypted: '" + msg + "'"); System. out. println ("public key = (n, e), private key = (n, d)"); System. out. println ("n =" + n); System. out. println ("e =" + e); System. out. println ("d =" + d) ;}/ * ludi @ ubun :~ $ Java RSA 'pay $123.45 'raw message: 'pay $123.45' encrypted: 'e ????? Xs> ????? -W? G ?? R branch ?? H] 'crypted: 'pay $123.45 'public key = (n, e), private key = (n, d) n = clerk = 7724646048891_363d = clerk @ ubun :~ $ Because it is random, the n, e, and d of each operation may be different. What is the proper length of p, q, and k? RSA theorem: set n = p * q, t = (p-1) * (q-1), e and t, 1 = e * d (mod t ), for any 0 <a <n, There Is a = a ^ (e * d) (mod n ). The length of n in the Code is 128 + 128-1 = 255. If "12" is input, a = 0x3132. That is, a character corresponds to 8-bit length, so the length of the input string should be less than 255/8 = 36. E is limited to 64bit. It should be acceptable (Theorem ?). Generally, the server generates a key, sends the Public Key (n, e) to the client, and encrypts the client. A smaller e makes the client less complex. */


I plan to continue to paste it later. I will learn about the data structure of java.

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.