Android 非對稱加解密,採用 lazysodium

HKT 講師,相關課程

HKT 講師,相關課程:
🎬 從零開始學 SwiftUI 程式設計 免費講義
https://bit.ly/3xCbUUp
🎬 從零開始學 Swift 程式設計 免費講義
https://bit.ly/3ekjsEP
🎬Android 入門開發實戰:口罩地圖(Kotlin) 免費講義
https://bit.ly/2KKZcju
🎬從零開始學 Dart 程式設計 免費講義
https://bit.ly/2OJW6hl
🎬 Flutter 程式設計入門實戰 免費講義
https://bit.ly/37L47Ij
🎬從零開始學 Java 程式設計 免費講義
https://bit.ly/2WlCn6y
🎬 從零開始學 kotlin 程式設計 免費講義
https://bit.ly/2Kx7GrM

lazysodium

libsodium 是一套新的更方便、更容易用來處理資料加解密的軟體包,支援多種平台應用。而 lazysodium 讓實作 libsodium 更方便。

添加 lazysodium 依賴庫 (dependencies)

dependencies {
    implementation "com.goterl:lazysodium-android:5.0.2@aar"
    implementation "net.java.dev.jna:jna:5.8.0@aar"
}

lazysodium 實作資料加解密方式

public static LazySodiumAndroid lazySodium;
private Box.Lazy cryptoBoxLazy;

...
...
...


try {
    String msg = "HKT線上教室:Sodium 加解密測試文字!@#$%^&*()_+ABCDE=";

    lazySodium = new LazySodiumAndroid(new SodiumAndroid());
    cryptoBoxLazy = (Box.Lazy) lazySodium;

    // App 的 keypair (一對私鑰與公鑰)
    KeyPair appKeys = cryptoBoxLazy.cryptoBoxKeypair();
    String appPrivateKey = appKeys.getSecretKey().getAsHexString();
    String appPublicKey = appKeys.getPublicKey().getAsHexString();

    Log.d("HKT", "appPrivateKey: " + appPrivateKey);
    Log.d("HKT", "appPublicKey: " + appPublicKey);

    // Server 的 keypair (一對私鑰與公鑰)
    KeyPair serverKeys = cryptoBoxLazy.cryptoBoxKeypair();
    String serverPrivateKey = serverKeys.getSecretKey().getAsHexString();
    String serverPublicKey = serverKeys.getPublicKey().getAsHexString();
    byte[] nonce = lazySodium.nonce(Box.NONCEBYTES);//加密格式
    //byte[] nonce = lazySodium.nonce(AEAD.CHACHA20POLY1305_NPUBBYTES);//加密格式
    Log.d("HKT", "serverPrivateKey: " + serverPrivateKey);
    Log.d("HKT", "serverPublicKey: " + serverPublicKey);


    //===============================
    //===APP加密資料、後端解密APP資料===
    //===============================
    // APP加密資料
    KeyPair appEncryptionKeyPair = new KeyPair(serverKeys.getPublicKey(), appKeys.getSecretKey());
    String appEncryptedMsg = cryptoBoxLazy.cryptoBoxEasy(msg, nonce, appEncryptionKeyPair);
    Log.d("HKT", "APP 加密後的資料: " + appEncryptedMsg);

    // 後端解密APP資料
    KeyPair serverDecryptionKeyPair = new KeyPair(appKeys.getPublicKey(), serverKeys.getSecretKey());
    String serverDecryptedMsg = cryptoBoxLazy.cryptoBoxOpenEasy(appEncryptedMsg, nonce, serverDecryptionKeyPair);
    Log.d("HKT", "後端解密APP資料: " + serverDecryptedMsg);

    //===============================
    //===後端加密資料、APP解密後端資料===
    //===============================
    // 後端加密資料
    KeyPair serverEncryptionKeyPair = new KeyPair(appKeys.getPublicKey(), serverKeys.getSecretKey());
    String serverEncryptedMsg = cryptoBoxLazy.cryptoBoxEasy(msg, nonce, serverEncryptionKeyPair);
    Log.d("HKT", "後端加密資料: " + serverEncryptedMsg);

    // APP解密後端資料
    KeyPair appDecryptionKeyPair = new KeyPair(serverKeys.getPublicKey(), appKeys.getSecretKey());
    String appDecryptedMsg = cryptoBoxLazy.cryptoBoxOpenEasy(serverEncryptedMsg, nonce, appDecryptionKeyPair);
    Log.d("HKT", "APP解密後端資料: " + appDecryptedMsg);
} catch (Exception e) {
    Log.e("HKT", "Exception: " + e.toString());
}

輸出結果

D/HKT: appPrivateKey: 63C128AF6FD0064FAC52712C1A71A205000682EEB513609973B039790091AEEC
D/HKT: appPublicKey: 98F8689DB70537B93193778BEE48CFC3EE14868E25E7C4D9A6A16A65388E0778

D/HKT: serverPrivateKey: 5B8BE43208F32B387FA77F989C6551043EDD2D050625E0EEA1F5DAE741D73F7A
D/HKT: serverPublicKey: 999C92DA0BD3F7CA34FC67A987C2D870E7E7B8A75113713068F2C9D16F5EB91E

D/HKT: APP 加密後的資料: 3EEADA0F0B84FCD3C12C6AB1EFFC50E75ED80B0FB619D27425C6863603ECD5F3E6FDCC6E02CE27DA6BC27518429728CB2A3852F7518DF07786E8E1D3FC9D7919CC21C38654EC7689CD155BB1E4EAE2DB

D/HKT: 後端解密APP資料: HKT線上教室:Sodium 加解密測試文字!@#$%^&*()_+ABCDE=

D/HKT: 後端加密資料: 3EEADA0F0B84FCD3C12C6AB1EFFC50E75ED80B0FB619D27425C6863603ECD5F3E6FDCC6E02CE27DA6BC27518429728CB2A3852F7518DF07786E8E1D3FC9D7919CC21C38654EC7689CD155BB1E4EAE2DB

D/HKT: APP解密後端資料: HKT線上教室:Sodium 加解密測試文字!@#$%^&*()_+ABCDE=

參考資料

lazysodium-android
https://github.com/terl/lazysodium-android

lazysodium-docs/usage/getting-started
https://github.com/terl/lazysodium-docs/blob/master/usage/getting-started.md

libsodium
https://libsodium.gitbook.io/doc/

Bindings for other languages
https://doc.libsodium.org/bindings_for_other_languages

公開金鑰加密
https://zh.wikipedia.org/wiki/公开密钥加密

Asymmetric Encryption - Simply explained
https://www.youtube.com/watch?v=AQDCe585Lnc&feature=emb_title&ab_channel=SimplyExplained

贊助我們

創作不易,知識無價,免費線上教學就像顆種子,希望藉由您的支持與贊助,能夠無後顧之憂的日漸茁壯,努力前行堅持下去。不論捐贈金額的大小,我們都由衷的感謝每位贊助者,都是我們推廣知識、開放共享知識最大的動力!

您的捐贈將用於:請作者喝杯咖啡,鼓勵繼續創作,持續上傳教學影片與更多新技術文章。

Line Pay 打賞


(由 Line Pay 支付平台,提供一卡通轉帳服務)

街口打賞


(由街口行動支付平台,提供轉帳服務)

超商代碼繳費打賞

(由綠界科技支付平台,提供超商繳費代碼)

相關連結

HKT 線上教室 每週六日 更新影片
▶ YouTube 頻道
https://goo.gl/3f2pJi
▶ KT 線上教室 臉書粉絲團
https://goo.gl/27H9Li
▶ Udemy 頻道
http://bit.ly/2ZNdnrt
▶ 贊助我們
https://goo.gl/FiKXAu

關鍵字

#AndroidStudio #Xcode
#Java #Kotlin #Dart #Flutter
#APP #AppDevloper
#Android #AndroidDevloper
#iOS #iOSDevlope #Swift #SwiftUI
#Programer

這個網誌中的熱門文章

16天記下7000單字

nano 文字編輯器

2023 最新入門零基礎 Kotlin教學【從零開始學 Kotlin 程式設計】Kotlin 教學課程目錄 (Android Kotlin, IntelliJ IDEA, Android Studio, Android APP 開發教學)

最新入門零基礎 Java 教學【從零開始學 Java 程式設計】Java教學課程目錄 (IntelliJ IDEA 開發教學)

2022 最新入門零基礎 Flutter教學 【Flutter 程式設計入門實戰 30 天】Flutter 教學課程目錄 (IntelliJ IDEA 開發教學)