Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java Forum > Java - ช่วยด้วยครับ ผมมึนมากเลยครับ ผมกำลังศึกษาวิธีการเข้ารหัสของ Java เพื่อนำไปทำโปรเจคจบน่ะครับ



 

Java - ช่วยด้วยครับ ผมมึนมากเลยครับ ผมกำลังศึกษาวิธีการเข้ารหัสของ Java เพื่อนำไปทำโปรเจคจบน่ะครับ

 



Topic : 117250



โพสกระทู้ ( 47 )
บทความ ( 0 )



สถานะออฟไลน์




ผมได้ลองศึกษาจากเว็บ http://www.java2s.com/Tutorial/Java/0490__Security/Catalog0490__Security.htm
และที่อื่นๆ และได้ลองก๊อปโค้ดของคนอื่นมารันดูว่าได้หรือไม่ ผต่ปลที่ได้คือแบบนี้ครับ

กกก

และนี้คือ code
Code (Java)
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package authentication;

/**
 *
 * @author mrkpetvisoot
 */
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AES {
        /**
         * Turns array of bytes into string
         * 
         * @param buf
         *                      Array of bytes to convert to hex string
         * @return Generated hex string
         */
        public static String asHex(byte buf[]) {
                StringBuffer strbuf = new StringBuffer(buf.length * 2);
                int i;

                for (i = 0; i < buf.length; i++) {
                        if (((int) buf[i] & 0xff) < 0x10)
                                strbuf.append("0");

                        strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
                }

                return strbuf.toString();
        }

        public static byte[] hexStringToByteArray(String s) {
                int len = s.length();
                byte[] data = new byte[len / 2];
                for (int i = 0; i < len; i += 2) {
                        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                                        .digit(s.charAt(i + 1), 16));
                }
                return data;
        }

        public static String stringToHex(String base) {
                StringBuffer buffer = new StringBuffer();
                int intValue;
                for (int x = 0; x < base.length(); x++) {
                        int cursor = 0;
                        intValue = base.charAt(x);
                        String binaryChar = new String(Integer.toBinaryString(base
                                        .charAt(x)));
                        for (int i = 0; i < binaryChar.length(); i++) {
                                if (binaryChar.charAt(i) == '1') {
                                        cursor += 1;
                                }
                        }
                        if ((cursor % 2) > 0) {
                                intValue += 128;
                        }
                        buffer.append(Integer.toHexString(intValue));
                }
                return buffer.toString();
        }

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

                String plainText = null;
                String key = null;
                
                if (args.length == 2) {
                        plainText = args[0];
                        key = args[1];
                } else {
                        System.err.println("params text key");
                        System.exit(1);
                }

                if (key.length() != 16) {
                        System.err.println("key length must be 16 characters.");
                        System.exit(1);
                }

                System.out.println("Key: " + key);
                System.out.println("Key (HEX): " + stringToHex(key));
                
                byte[] raw = hexStringToByteArray(stringToHex(key));

                SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

                // Instantiate the cipher
                Cipher cipher = Cipher.getInstance("AES");

                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

                System.out.println("Key (Byte to Hex): " +asHex(raw));

                byte[] encrypted = cipher
                                .doFinal((args.length == 0 ? "This is just an example"
                                                : args[0]).getBytes());
                System.out.println("encrypted string: " + asHex(encrypted));

                cipher.init(Cipher.DECRYPT_MODE, skeySpec);
                byte[] original = cipher.doFinal(encrypted);
                String originalString = new String(original);
                System.out.println("Original string: " + originalString);
        }

}



รบกวนช่วยอธิบายหน่อยน่ะครับ และหากต้องการให้มันรันผ่านเราต้องแก้ไขยังไง และเป็นการรับค่ามาจาก Form Java GUI



Tag : Java, MySQL









ประวัติการแก้ไข
2015-06-19 10:15:39
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-06-19 10:13:12 By : illmndraft View : 1615 Reply : 4
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ตรงนี้ครับเรียกใช้


Code
String plainText = null; String key = null; if (args.length == 2) { plainText = args[0]; key = args[1]; } else { System.err.println("params text key"); System.exit(1); } if (key.length() != 16) { System.err.println("key length must be 16 characters."); System.exit(1); } System.out.println("Key: " + key); System.out.println("Key (HEX): " + stringToHex(key));







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-20 15:28:36 By : mr.win
 


 

No. 2



โพสกระทู้ ( 47 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2015-06-20 15:28:36
รายละเอียดของการตอบ ::
ก่อนหน้านี้ผมก็ลองใส่แล้วน่ะครับมันก็ยังขึ้นแบบเดิมน่ะครับ


String plainText = "test";
String key = "1234567890123456";



ผมได้ลองใส่ตามนี้ละครับ มันก็ยังเหมือนเดิม

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-20 17:09:19 By : illmndraft
 

 

No. 3



โพสกระทู้ ( 192 )
บทความ ( 0 )



สถานะออฟไลน์


Code
ถ้าจะ copy มาต้องมีพื้นฐาน java สักหน่อยนะจ๊ะ

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package authentication;

/**
 *
 * @author mrkpetvisoot
 */
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AES {
        /**
         * Turns array of bytes into string
         * 
         * @param buf
         *                      Array of bytes to convert to hex string
         * @return Generated hex string
         */
        public static String asHex(byte buf[]) {
                StringBuffer strbuf = new StringBuffer(buf.length * 2);
                int i;

                for (i = 0; i < buf.length; i++) {
                        if (((int) buf[i] & 0xff) < 0x10)
                                strbuf.append("0");

                        strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
                }

                return strbuf.toString();
        }

        public static byte[] hexStringToByteArray(String s) {
                int len = s.length();
                byte[] data = new byte[len / 2];
                for (int i = 0; i < len; i += 2) {
                        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                                        .digit(s.charAt(i + 1), 16));
                }
                return data;
        }

        public static String stringToHex(String base) {
                StringBuffer buffer = new StringBuffer();
                int intValue;
                for (int x = 0; x < base.length(); x++) {
                        int cursor = 0;
                        intValue = base.charAt(x);
                        String binaryChar = new String(Integer.toBinaryString(base
                                        .charAt(x)));
                        for (int i = 0; i < binaryChar.length(); i++) {
                                if (binaryChar.charAt(i) == '1') {
                                        cursor += 1;
                                }
                        }
                        if ((cursor % 2) > 0) {
                                intValue += 128;
                        }
                        buffer.append(Integer.toHexString(intValue));
                }
                return buffer.toString();
        }

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

               String plainText = "test";
               String key = "1234567890123456";
                /*
                if (args.length == 2) {
                        plainText = args[0];
                        key = args[1];
                } else {
                        System.err.println("params text key");
                        System.exit(1);
                }
                */
                if (key.length() != 16) {
                        System.err.println("key length must be 16 characters.");
                        System.exit(1);
                }

                System.out.println("Key: " + key);
                System.out.println("Key (HEX): " + stringToHex(key));
                
                byte[] raw = hexStringToByteArray(stringToHex(key));

                SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

                // Instantiate the cipher
                Cipher cipher = Cipher.getInstance("AES");

                cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

                System.out.println("Key (Byte to Hex): " +asHex(raw));

                byte[] encrypted = cipher
                                .doFinal((args.length == 0 ? "This is just an example"
                                                : args[0]).getBytes());
                System.out.println("encrypted string: " + asHex(encrypted));

                cipher.init(Cipher.DECRYPT_MODE, skeySpec);
                byte[] original = cipher.doFinal(encrypted);
                String originalString = new String(original);
                System.out.println("Original string: " + originalString);
        }

}


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-21 14:51:33 By : ipstarone
 


 

No. 4



โพสกระทู้ ( 47 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณมากๆๆน่ครับ แต่อาจารย์ที่ปรึกษาผมไม่ยอมให้ใช้งาน AES แล้วน่ะครับ เขาต้องการเป็นกุญแจคู่ เช่น RSA ผมเลยต้องไปศึกษาเกี่ยวกับ RSA แล้วน่ะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-06-21 18:14:15 By : illmndraft
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Java - ช่วยด้วยครับ ผมมึนมากเลยครับ ผมกำลังศึกษาวิธีการเข้ารหัสของ Java เพื่อนำไปทำโปรเจคจบน่ะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่