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,038

HOME > Mobile > Mobile Forum > สอบถามเรื่อง Encryption และ Decryption (Android) !!


[Mobile] สอบถามเรื่อง Encryption และ Decryption (Android) !!

 
Topic : 121103



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



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



อยากสอบถามเกี่ยวกับการ Encrypt / Decrypt ครับ สมมุติว่า Web Service ทำการ Decryption ข้อมูลแล้วส่งมาที่ตัว Mobile
วิธีการ Decryption ยังไงครับ รวมถึงตัวอย่างการ Encryption จาก Web Service ด้วยครับ

ขอบคุณครับ :)



Tag : Mobile, WebService, Android, JAVA

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-01-28 14:17:15 By : d10s View : 1475 Reply : 4
 

 

No. 1



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

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

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

หา function ไว้สำหรับ encrypt และ decrypt ก็น่าจะได้แล้วครับ

Code (Java)
01.    public String encrypt(String str) {
02.        try {
03.            // Encode the string into bytes using utf-8
04.            byte[] utf8 = str.getBytes("UTF8");
05. 
06.            // Encrypt
07.            byte[] enc = ecipher.doFinal(utf8);
08. 
09.            // Encode bytes to base64 to get a string
10.            return new sun.misc.BASE64Encoder().encode(enc);
11.        } catch (javax.crypto.BadPaddingException e) {
12.        } catch (IllegalBlockSizeException e) {
13.        } catch (UnsupportedEncodingException e) {
14.        } catch (java.io.IOException e) {
15.        }
16.        return null;
17.    }
18. 
19.    public String decrypt(String str) {
20.        try {
21.            // Decode base64 to get bytes
22.            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
23. 
24.            // Decrypt
25.            byte[] utf8 = dcipher.doFinal(dec);
26. 
27.            // Decode using utf-8
28.            return new String(utf8, "UTF8");
29.        } catch (javax.crypto.BadPaddingException e) {
30.        } catch (IllegalBlockSizeException e) {
31.        } catch (UnsupportedEncodingException e) {
32.        } catch (java.io.IOException e) {
33.        }
34.        return null;
35.    }
36.}


Code (Java)
01.try {
02.    // Generate a temporary key. In practice, you would save this key.
03.    // See also Encrypting with DES Using a Pass Phrase.
04.    SecretKey key = KeyGenerator.getInstance("DES").generateKey();
05. 
06.    // Create encrypter/decrypter class
07.    DesEncrypter encrypter = new DesEncrypter(key);
08. 
09.    // Encrypt
10.    String encrypted = encrypter.encrypt("Don't tell anybody!");
11. 
12.    // Decrypt
13.    String decrypted = encrypter.decrypt(encrypted);
14.} catch (Exception e) {
15.}

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-01-28 14:30:13 By : mr.win
 

 

No. 2



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

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

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

ตัวนี้ก็น่าสนครับ

Code (Java)
01.public class EncryptUtils {
02.   public static final String DEFAULT_ENCODING="UTF-8";
03.   static BASE64Encoder enc=new BASE64Encoder();
04.   static BASE64Decoder dec=new BASE64Decoder();
05. 
06.   public static String base64encode(String text){
07.      try {
08.         String rez = enc.encode( text.getBytes( DEFAULT_ENCODING ) );
09.         return rez;        
10.      }
11.      catch ( UnsupportedEncodingException e ) {
12.         return null;
13.      }
14.   }//base64encode
15. 
16.   public static String base64decode(String text){
17. 
18.         try {
19.            return new String(dec.decodeBuffer( text ),DEFAULT_ENCODING);
20.         }
21.         catch ( IOException e ) {
22.           return null;
23.         }
24. 
25.      }//base64decode
26. 
27.      public static void main(String[] args){
28.       String txt="some text to be encrypted" ;
29.       String key="key phrase used for XOR-ing";
30.       System.out.println(txt+" XOR-ed to: "+(txt=xorMessage( txt, key )));
31.       String encoded=base64encode( txt );      
32.       System.out.println( " is encoded to: "+encoded+" and that is decoding to: "+ (txt=base64decode( encoded )));
33.       System.out.print( "XOR-ing back to original: "+xorMessage( txt, key ) );
34. 
35.      }
36. 
37.      public static String xorMessage(String message, String key){
38.       try {
39.          if (message==null || key==null ) return null;
40. 
41.         char[] keys=key.toCharArray();
42.         char[] mesg=message.toCharArray();
43. 
44.         int ml=mesg.length;
45.         int kl=keys.length;
46.         char[] newmsg=new char[ml];
47. 
48.         for (int i=0; i<ml; i++){
49.            newmsg[i]=(char)(mesg[i]^keys[i%kl]);
50.         }//for i
51.         mesg=null; keys=null;
52.         return new String(newmsg);
53.      }
54.      catch ( Exception e ) {
55.         return null;
56.       
57.      }//xorMessage
58. 
59.}//class

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-01-28 14:30:44 By : mr.win
 

 

No. 3



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



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


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2016-01-28 14:30:13
รายละเอียดของการตอบ ::
แล้วกรณีการ Encrypt / Decrypt ฝั่ง Web Service ทำแบบเดียวกันไหมครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-01-28 14:32:11 By : d10s
 

 

No. 4



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

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

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

ครับ การเข้ารหัสและถอดรหัสจะต้องใช้ function เดียวกัน
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-01-28 14:44:25 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามเรื่อง Encryption และ Decryption (Android) !!
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





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