├── .classpath
├── .project
├── AndroidManifest.xml
├── README.md
├── bin
├── AndroidManifest.xml
├── classes
│ └── com
│ │ └── xcj
│ │ └── android
│ │ └── ende
│ │ ├── AES.class
│ │ ├── Base64.class
│ │ ├── BuildConfig.class
│ │ ├── DES.class
│ │ ├── Encoder.class
│ │ ├── GeneralDigest.class
│ │ ├── Hex.class
│ │ ├── HexEncoder.class
│ │ ├── HexUtil.class
│ │ ├── Md5.class
│ │ ├── R$attr.class
│ │ ├── R$drawable.class
│ │ ├── R$string.class
│ │ ├── R$style.class
│ │ ├── R.class
│ │ ├── SHA1.class
│ │ ├── SHA1Digest.class
│ │ └── package-info.class
├── jarlist.cache
├── library_ende.jar
└── res
│ ├── drawable-hdpi
│ ├── ic_action_search.png
│ └── ic_launcher.png
│ ├── drawable-mdpi
│ ├── ic_action_search.png
│ └── ic_launcher.png
│ └── drawable-xhdpi
│ ├── ic_action_search.png
│ └── ic_launcher.png
├── gen
└── com
│ └── xcj
│ └── android
│ └── ende
│ ├── BuildConfig.java
│ └── R.java
├── hs_err_pid9316.log
├── libs
└── android-support-v4.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ ├── ic_action_search.png
│ └── ic_launcher.png
├── drawable-mdpi
│ ├── ic_action_search.png
│ └── ic_launcher.png
├── drawable-xhdpi
│ ├── ic_action_search.png
│ └── ic_launcher.png
├── values-v11
│ └── styles.xml
├── values-v14
│ └── styles.xml
└── values
│ ├── strings.xml
│ └── styles.xml
└── src
└── com
└── xcj
└── android
└── ende
├── AES.java
├── Base64.java
├── DES.java
├── Encoder.java
├── GeneralDigest.java
├── Hex.java
├── HexEncoder.java
├── HexUtil.java
├── Md5.java
├── SHA1.java
├── SHA1Digest.java
└── package-info.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | library_ende
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/README.md
--------------------------------------------------------------------------------
/bin/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/AES.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/AES.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/Base64.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/Base64.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/BuildConfig.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/BuildConfig.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/DES.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/DES.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/Encoder.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/Encoder.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/GeneralDigest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/GeneralDigest.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/Hex.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/Hex.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/HexEncoder.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/HexEncoder.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/HexUtil.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/HexUtil.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/Md5.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/Md5.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/R$attr.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/R$attr.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/R$drawable.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/R$drawable.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/R$string.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/R$string.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/R$style.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/R$style.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/R.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/R.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/SHA1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/SHA1.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/SHA1Digest.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/SHA1Digest.class
--------------------------------------------------------------------------------
/bin/classes/com/xcj/android/ende/package-info.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/classes/com/xcj/android/ende/package-info.class
--------------------------------------------------------------------------------
/bin/jarlist.cache:
--------------------------------------------------------------------------------
1 | # cache for current jar dependecy. DO NOT EDIT.
2 | # format is
3 | # Encoding is UTF-8
4 |
--------------------------------------------------------------------------------
/bin/library_ende.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/library_ende.jar
--------------------------------------------------------------------------------
/bin/res/drawable-hdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/res/drawable-hdpi/ic_action_search.png
--------------------------------------------------------------------------------
/bin/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/drawable-mdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/res/drawable-mdpi/ic_action_search.png
--------------------------------------------------------------------------------
/bin/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/bin/res/drawable-xhdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/res/drawable-xhdpi/ic_action_search.png
--------------------------------------------------------------------------------
/bin/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/bin/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/gen/com/xcj/android/ende/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /** Automatically generated file. DO NOT MODIFY */
2 | package com.xcj.android.ende;
3 |
4 | public final class BuildConfig {
5 | public final static boolean DEBUG = true;
6 | }
--------------------------------------------------------------------------------
/gen/com/xcj/android/ende/R.java:
--------------------------------------------------------------------------------
1 | /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 | *
3 | * This class was automatically generated by the
4 | * aapt tool from the resource data it found. It
5 | * should not be modified by hand.
6 | */
7 |
8 | package com.xcj.android.ende;
9 |
10 | public final class R {
11 | public static final class attr {
12 | }
13 | public static final class drawable {
14 | public static int ic_action_search=0x7f020000;
15 | public static int ic_launcher=0x7f020001;
16 | }
17 | public static final class string {
18 | public static int app_name=0x7f030000;
19 | }
20 | public static final class style {
21 | public static int AppTheme=0x7f040000;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/hs_err_pid9316.log:
--------------------------------------------------------------------------------
1 | #
2 | # An unexpected error has been detected by Java Runtime Environment:
3 | #
4 | # Internal Error (classFileParser.cpp:2923), pid=9316, tid=9360
5 | # Error: ShouldNotReachHere()
6 | #
7 | # Java VM: Java HotSpot(TM) Client VM (11.0-b15 mixed mode windows-x86)
8 | # If you would like to submit a bug report, please visit:
9 | # http://java.sun.com/webapps/bugreport/crash.jsp
10 | #
11 |
12 | --------------- T H R E A D ---------------
13 |
14 | Current thread (0x012b9800): JavaThread "Unknown thread" [_thread_in_vm, id=9360, stack(0x00140000,0x00190000)]
15 |
16 | Stack: [0x00140000,0x00190000]
17 |
18 | [error occurred during error reporting (printing native stack), id 0xc0000005]
19 |
20 |
21 | --------------- P R O C E S S ---------------
22 |
23 | Java Threads: ( => current thread )
24 |
25 | Other Threads:
26 |
27 | =>0x012b9800 (exited) JavaThread "Unknown thread" [_thread_in_vm, id=9360, stack(0x00140000,0x00190000)]
28 |
29 | VM state:not at safepoint (not fully initialized)
30 |
31 | VM Mutex/Monitor currently owned by a thread: None
32 |
33 | Dynamic libraries:
34 | 0x00400000 - 0x00424000 D:\Development\Java\jre6\bin\javaw.exe
35 | 0x775a0000 - 0x776dc000 C:\Windows\SYSTEM32\ntdll.dll
36 | 0x776f0000 - 0x777c4000 C:\Windows\system32\kernel32.dll
37 | 0x755a0000 - 0x755eb000 C:\Windows\system32\KERNELBASE.dll
38 | 0x759c0000 - 0x75a60000 C:\Windows\system32\ADVAPI32.dll
39 | 0x75910000 - 0x759bc000 C:\Windows\system32\msvcrt.dll
40 | 0x77310000 - 0x77329000 C:\Windows\SYSTEM32\sechost.dll
41 | 0x75780000 - 0x75821000 C:\Windows\system32\RPCRT4.dll
42 | 0x75c20000 - 0x75ce9000 C:\Windows\system32\USER32.dll
43 | 0x75bd0000 - 0x75c1e000 C:\Windows\system32\GDI32.dll
44 | 0x776e0000 - 0x776ea000 C:\Windows\system32\LPK.dll
45 | 0x76960000 - 0x769fd000 C:\Windows\system32\USP10.dll
46 | 0x76940000 - 0x7695f000 C:\Windows\system32\IMM32.DLL
47 | 0x77240000 - 0x7730c000 C:\Windows\system32\MSCTF.dll
48 | 0x7c340000 - 0x7c396000 D:\Development\Java\jre6\bin\msvcr71.dll
49 | 0x6d800000 - 0x6da56000 D:\Development\Java\jre6\bin\client\jvm.dll
50 | 0x75460000 - 0x75492000 C:\Windows\system32\WINMM.dll
51 | 0x75310000 - 0x7535c000 C:\Windows\system32\apphelp.dll
52 | 0x6d280000 - 0x6d288000 D:\Development\Java\jre6\bin\hpi.dll
53 | 0x75a60000 - 0x75a65000 C:\Windows\system32\PSAPI.DLL
54 | 0x6d7b0000 - 0x6d7bc000 D:\Development\Java\jre6\bin\verify.dll
55 | 0x6d320000 - 0x6d33f000 D:\Development\Java\jre6\bin\java.dll
56 | 0x6d7f0000 - 0x6d7ff000 D:\Development\Java\jre6\bin\zip.dll
57 |
58 | VM Arguments:
59 | jvm_args: -Dfile.encoding=UTF-8 -Xbootclasspath:D:\Development\android-sdk\platforms\android-14\android.jar
60 | java_command: com.xcj.android.ende.DES
61 | Launcher Type: SUN_STANDARD
62 |
63 | Environment Variables:
64 | JAVA_HOME=D:\Development\Java\jdk1.6.0_10
65 | CLASSPATH=.;D:\Development\Java\jdk1.6.0_10\lib\dt.jar;D:\Development\Java\jdk1.6.0_10\lib\tools.jar
66 | PATH=D:/Development/Java/jre6/bin/client;D:/Development/Java/jre6/bin;D:/Development/Java/jre6/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files\TortoiseSVN\bin;D:\Development\android-sdk\tools;D:\Development\android-sdk\platform-tools;D:\Program Files\Git\cmd;D:\Development\Java\jdk1.6.0_10\bin;D:\Development\Java\jdk1.6.0_10\jre\bin;D:\Development\apache-ant-1.8.3\bin;D:\Development\apache-ant-1.8.3\lib;D:\Program Files\Rational\common;D:\eclipse_java;
67 | USERNAME=chunjiang.shieh
68 | OS=Windows_NT
69 | PROCESSOR_IDENTIFIER=x86 Family 6 Model 42 Stepping 7, GenuineIntel
70 |
71 |
72 |
73 | --------------- S Y S T E M ---------------
74 |
75 | OS: Windows NT 6.1 Build 7601 Service Pack 1
76 |
77 | CPU:total 2 (8 cores per cpu, 2 threads per core) family 6 model 10 stepping 7, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, ht
78 |
79 | Memory: 4k page, physical 2097151k(858760k free), swap 4194303k(2865392k free)
80 |
81 | vm_info: Java HotSpot(TM) Client VM (11.0-b15) for windows-x86 JRE (1.6.0_10-rc2-b32), built on Sep 12 2008 00:52:11 by "java_re" with MS VC++ 7.1
82 |
83 | time: Fri Nov 09 14:29:11 2012
84 | elapsed time: 0 seconds
85 |
86 |
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-14
15 | android.library=true
16 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/res/drawable-hdpi/ic_action_search.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/res/drawable-mdpi/ic_action_search.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_action_search.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/res/drawable-xhdpi/ic_action_search.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chunjiangshieh/android-library-crypto/95f32497272d1cce09b021587e960c2b0d6cc786/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidEnDe
3 |
4 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/AES.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 | import java.security.SecureRandom;
4 |
5 | import javax.crypto.Cipher;
6 | import javax.crypto.KeyGenerator;
7 | import javax.crypto.SecretKey;
8 | import javax.crypto.spec.SecretKeySpec;
9 | /**
10 | * AES 128bits密钥 加解密
11 | * @author chunjiang.shieh
12 | *
13 | */
14 | public class AES {
15 |
16 | /**
17 | *
18 | * @param seed 种子
19 | * @param cleartext 需要加密的明文
20 | * @return
21 | * @throws Exception
22 | */
23 | public static String encrypt(String seed, String cleartext) throws Exception {
24 | byte[] rawKey = getRawKey(seed.getBytes());
25 | byte[] result = encrypt(rawKey, cleartext.getBytes());
26 | return HexUtil.toHex(result);
27 | }
28 |
29 | /**
30 | *
31 | * @param seed 种子
32 | * @param encrypted 需要解密的密文(16进制数据)
33 | * @return
34 | * @throws Exception
35 | */
36 | public static String decrypt(String seed, String encrypted) throws Exception {
37 | byte[] rawKey = getRawKey(seed.getBytes());
38 | byte[] enc = HexUtil.hextoBytes(encrypted);
39 | byte[] result = decrypt(rawKey, enc);
40 | return new String(result);
41 | }
42 |
43 |
44 | /**
45 | * 根据种子生产128bit的密钥
46 | * @param seed
47 | * @return
48 | * @throws Exception
49 | */
50 | private static byte[] getRawKey(byte[] seed) throws Exception {
51 | /**
52 | * 创建一个新的KeyGenerator实例提供指定的加密算法、
53 | * 此类提供(对称)密钥生成器的功能。
54 | */
55 | KeyGenerator kgen = KeyGenerator.getInstance("AES");
56 | /**
57 | * SecureRandom 实现作为随机源
58 | * 返回实现指定随机数生成器 (RNG) 算法的 SecureRandom 对象
59 | */
60 | SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
61 | //设置实际随机种子 然后生成伪随机序列
62 | sr.setSeed(seed);
63 | //使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥大小
64 | kgen.init(128, sr); // 192 and 256 bits may not be available
65 | // 生成一个密钥
66 | SecretKey skey = kgen.generateKey();
67 | //返回基本编码格式的密钥,如果此密钥不支持编码,则返回 null
68 | byte[] raw = skey.getEncoded();
69 | return raw;
70 | }
71 |
72 |
73 | /**
74 | * AES 加密
75 | * @param raw AES 加密的KEY
76 | * @param clear AES 加密的内容 (128位的明文)
77 | * @return 返回 128位的密文
78 | * @throws Exception
79 | */
80 | public static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
81 | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
82 | Cipher cipher = Cipher.getInstance("AES");
83 | cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
84 | byte[] encrypted = cipher.doFinal(clear);
85 | return encrypted;
86 | }
87 |
88 | /**
89 | * AES 解密
90 | * @param raw AES 解密的KEY
91 | * @param encrypted AES 解密的内容 (128位的密文)
92 | * @return 返回 (128位的明文)
93 | * @throws Exception
94 | */
95 | public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
96 | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
97 | Cipher cipher = Cipher.getInstance("AES");
98 | cipher.init(Cipher.DECRYPT_MODE, skeySpec);
99 | byte[] decrypted = cipher.doFinal(encrypted);
100 | return decrypted;
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/Base64.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.IOException;
5 | import java.io.OutputStream;
6 |
7 | /**
8 | * Base64 加解密工具类
9 | *
10 | * @author chunjiang.shieh
11 | *
12 | */
13 | public class Base64 {
14 |
15 | private static final char[] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
16 | /**
17 | * data[]进行编码
18 | * @param data
19 | * @return
20 | */
21 | public static String encode(byte[] data) {
22 | int start = 0;
23 | int len = data.length;
24 | StringBuffer buf = new StringBuffer(data.length * 3 / 2);
25 | int end = len - 3;
26 | int i = start;
27 | int n = 0;
28 | while (i <= end) {
29 | int d = ((((int) data[i]) & 0x0ff) << 16)
30 | | ((((int) data[i + 1]) & 0x0ff) << 8)
31 | | (((int) data[i + 2]) & 0x0ff);
32 | buf.append(legalChars[(d >> 18) & 63]);
33 | buf.append(legalChars[(d >> 12) & 63]);
34 | buf.append(legalChars[(d >> 6) & 63]);
35 | buf.append(legalChars[d & 63]);
36 | i += 3;
37 | if (n++ >= 14) {
38 | n = 0;
39 | buf.append(" ");
40 | }
41 | }
42 | if (i == start + len - 2) {
43 | int d = ((((int) data[i]) & 0x0ff) << 16)
44 | | ((((int) data[i + 1]) & 255) << 8);
45 | buf.append(legalChars[(d >> 18) & 63]);
46 | buf.append(legalChars[(d >> 12) & 63]);
47 | buf.append(legalChars[(d >> 6) & 63]);
48 | buf.append("=");
49 | } else if (i == start + len - 1) {
50 | int d = (((int) data[i]) & 0x0ff) << 16;
51 | buf.append(legalChars[(d >> 18) & 63]);
52 | buf.append(legalChars[(d >> 12) & 63]);
53 | buf.append("==");
54 | }
55 | return buf.toString();
56 | }
57 |
58 | private static int decode(char c) {
59 | if (c >= 'A' && c <= 'Z')
60 | return ((int) c) - 65;
61 | else if (c >= 'a' && c <= 'z')
62 | return ((int) c) - 97 + 26;
63 | else if (c >= '0' && c <= '9')
64 | return ((int) c) - 48 + 26 + 26;
65 | else
66 | switch (c) {
67 | case '+':
68 | return 62;
69 | case '/':
70 | return 63;
71 | case '=':
72 | return 0;
73 | default:
74 | throw new RuntimeException("unexpected code: " + c);
75 | }
76 | }
77 | /**
78 | * Decodes the given Base64 encoded String to a new byte array. The byte
79 | * array holding the decoded data is returned.
80 | */
81 | public static byte[] decode(String s) {
82 | ByteArrayOutputStream bos = new ByteArrayOutputStream();
83 | try {
84 | decode(s, bos);
85 | } catch (IOException e) {
86 | throw new RuntimeException();
87 | }
88 | byte[] decodedBytes = bos.toByteArray();
89 | try {
90 | bos.close();
91 | bos = null;
92 | } catch (IOException ex) {
93 | System.err.println("Error while decoding BASE64: " + ex.toString());
94 | }
95 | return decodedBytes;
96 | }
97 | private static void decode(String s, OutputStream os) throws IOException {
98 | int i = 0;
99 | int len = s.length();
100 | while (true) {
101 | while (i < len && s.charAt(i) <= ' ')
102 | i++;
103 | if (i == len)
104 | break;
105 | int tri = (decode(s.charAt(i)) << 18)
106 | + (decode(s.charAt(i + 1)) << 12)
107 | + (decode(s.charAt(i + 2)) << 6)
108 | + (decode(s.charAt(i + 3)));
109 | os.write((tri >> 16) & 255);
110 | if (s.charAt(i + 2) == '=')
111 | break;
112 | os.write((tri >> 8) & 255);
113 | if (s.charAt(i + 3) == '=')
114 | break;
115 | os.write(tri & 255);
116 | i += 4;
117 | }
118 | }
119 |
120 |
121 | }
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/DES.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 |
4 | import javax.crypto.Cipher;
5 | import javax.crypto.spec.IvParameterSpec;
6 | import javax.crypto.spec.SecretKeySpec;
7 |
8 | /**
9 | * DES 加解密的算法
10 | * 加密的key必须为8位或者8的倍数,否则在java加密过程中会报错
11 | * @author chunjiang.shieh
12 | *
13 | */
14 | public class DES {
15 |
16 |
17 | private static byte[] iv = {1,2,3,4,5,6,7,8};
18 |
19 | /**
20 | * CBC是工作模式
21 | * PKCS5Padding是填充模式,还有其它的填充模式
22 | */
23 | private static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
24 |
25 | /**
26 | * DES算法,加密
27 | * @param encryptKey 加密私钥,长度不能够小于8位
28 | * @param encryptString 待加密字符串
29 | * @return 加密后的结果一般都会用base64编码进行传输
30 | * @throws CryptException 异常
31 | */
32 | public static String encryptDES(String encryptKey,String encryptString) throws Exception{
33 | IvParameterSpec zeroIv = new IvParameterSpec(iv);
34 | SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(),
35 | "DES");
36 | Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
37 | /**
38 | * zeroIv初始化向量,注意:必须设置,否则会调用平台默认的 不通平台不一样
39 | */
40 | cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
41 | byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
42 | return Base64.encode(encryptedData);
43 | }
44 |
45 |
46 |
47 | /**
48 | * DES算法,解密
49 | * @param decryptString 解密私钥,长度不能够小于8位
50 | * @param decryptKey 待解密字符串
51 | * @return 解密后的字符串(明文)
52 | * @throws Exception
53 | */
54 | public static String decryptDES(String decryptKey,String decryptString) throws Exception {
55 | byte[] byteMi = Base64.decode(decryptString);
56 | IvParameterSpec zeroIv = new IvParameterSpec(iv);
57 | SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
58 | Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
59 | cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
60 | byte decryptedData[] = cipher.doFinal(byteMi);
61 | return new String(decryptedData);
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/Encoder.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 |
4 | import java.io.IOException;
5 | import java.io.OutputStream;
6 |
7 | /**
8 | * Encode and decode byte arrays (typically from binary to 7-bit ASCII
9 | * encodings).
10 | */
11 | public interface Encoder
12 | {
13 | int encode(byte[] data, int off, int length, OutputStream out) throws IOException;
14 |
15 | int decode(byte[] data, int off, int length, OutputStream out) throws IOException;
16 |
17 | int decode(String data, OutputStream out) throws IOException;
18 | }
19 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/GeneralDigest.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 |
4 |
5 | /**
6 | * base implementation of MD4 family style digest as outlined in
7 | * "Handbook of Applied Cryptography", pages 344 - 347.
8 | */
9 | public abstract class GeneralDigest
10 | {
11 | private byte[] xBuf;
12 | private int xBufOff;
13 |
14 | private long byteCount;
15 |
16 | /**
17 | * Standard constructor
18 | */
19 | protected GeneralDigest()
20 | {
21 | xBuf = new byte[4];
22 | xBufOff = 0;
23 | }
24 |
25 | /**
26 | * Copy constructor. We are using copy constructors in place
27 | * of the Object.clone() interface as this interface is not
28 | * supported by J2ME.
29 | */
30 | protected GeneralDigest(GeneralDigest t)
31 | {
32 | xBuf = new byte[t.xBuf.length];
33 | System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length);
34 |
35 | xBufOff = t.xBufOff;
36 | byteCount = t.byteCount;
37 | }
38 |
39 | public void update(
40 | byte in)
41 | {
42 | xBuf[xBufOff++] = in;
43 |
44 | if (xBufOff == xBuf.length)
45 | {
46 | processWord(xBuf, 0);
47 | xBufOff = 0;
48 | }
49 |
50 | byteCount++;
51 | }
52 |
53 | public void update(
54 | byte[] in,
55 | int inOff,
56 | int len)
57 | {
58 | //
59 | // fill the current word
60 | //
61 | while ((xBufOff != 0) && (len > 0))
62 | {
63 | update(in[inOff]);
64 |
65 | inOff++;
66 | len--;
67 | }
68 |
69 | //
70 | // process whole words.
71 | //
72 | while (len > xBuf.length)
73 | {
74 | processWord(in, inOff);
75 |
76 | inOff += xBuf.length;
77 | len -= xBuf.length;
78 | byteCount += xBuf.length;
79 | }
80 |
81 | //
82 | // load in the remainder.
83 | //
84 | while (len > 0)
85 | {
86 | update(in[inOff]);
87 |
88 | inOff++;
89 | len--;
90 | }
91 | }
92 |
93 | public void finish()
94 | {
95 | long bitLength = (byteCount << 3);
96 |
97 | //
98 | // add the pad bytes.
99 | //
100 | update((byte)128);
101 |
102 | while (xBufOff != 0)
103 | {
104 | update((byte)0);
105 | }
106 |
107 | processLength(bitLength);
108 |
109 | processBlock();
110 | }
111 |
112 | public void reset()
113 | {
114 | byteCount = 0;
115 |
116 | xBufOff = 0;
117 | for (int i = 0; i < xBuf.length; i++)
118 | {
119 | xBuf[i] = 0;
120 | }
121 | }
122 |
123 | protected abstract void processWord(byte[] in, int inOff);
124 |
125 | protected abstract void processLength(long bitLength);
126 |
127 | protected abstract void processBlock();
128 | }
129 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/Hex.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 |
4 |
5 | import java.io.ByteArrayOutputStream;
6 | import java.io.IOException;
7 | import java.io.OutputStream;
8 |
9 | public class Hex
10 | {
11 | private static final Encoder encoder = new HexEncoder();
12 |
13 | /**
14 | * encode the input data producing a Hex encoded byte array.
15 | *
16 | * @return a byte array containing the Hex encoded data.
17 | */
18 | public static byte[] encode(
19 | byte[] data)
20 | {
21 | return encode(data, 0, data.length);
22 | }
23 |
24 | /**
25 | * encode the input data producing a Hex encoded byte array.
26 | *
27 | * @return a byte array containing the Hex encoded data.
28 | */
29 | public static byte[] encode(
30 | byte[] data,
31 | int off,
32 | int length)
33 | {
34 | ByteArrayOutputStream bOut = new ByteArrayOutputStream();
35 |
36 | try
37 | {
38 | encoder.encode(data, off, length, bOut);
39 | }
40 | catch (IOException e)
41 | {
42 | throw new RuntimeException("exception encoding Hex string: " + e);
43 | }
44 |
45 | return bOut.toByteArray();
46 | }
47 |
48 | /**
49 | * Hex encode the byte data writing it to the given output stream.
50 | *
51 | * @return the number of bytes produced.
52 | */
53 | public static int encode(
54 | byte[] data,
55 | OutputStream out)
56 | throws IOException
57 | {
58 | return encoder.encode(data, 0, data.length, out);
59 | }
60 |
61 | /**
62 | * Hex encode the byte data writing it to the given output stream.
63 | *
64 | * @return the number of bytes produced.
65 | */
66 | public static int encode(
67 | byte[] data,
68 | int off,
69 | int length,
70 | OutputStream out)
71 | throws IOException
72 | {
73 | return encoder.encode(data, off, length, out);
74 | }
75 |
76 | /**
77 | * decode the Hex encoded input data. It is assumed the input data is valid.
78 | *
79 | * @return a byte array representing the decoded data.
80 | */
81 | public static byte[] decode(
82 | byte[] data)
83 | {
84 | ByteArrayOutputStream bOut = new ByteArrayOutputStream();
85 |
86 | try
87 | {
88 | encoder.decode(data, 0, data.length, bOut);
89 | }
90 | catch (IOException e)
91 | {
92 | throw new RuntimeException("exception decoding Hex string: " + e);
93 | }
94 |
95 | return bOut.toByteArray();
96 | }
97 |
98 | /**
99 | * decode the Hex encoded String data - whitespace will be ignored.
100 | *
101 | * @return a byte array representing the decoded data.
102 | */
103 | public static byte[] decode(
104 | String data)
105 | {
106 | ByteArrayOutputStream bOut = new ByteArrayOutputStream();
107 |
108 | try
109 | {
110 | encoder.decode(data, bOut);
111 | }
112 | catch (IOException e)
113 | {
114 | throw new RuntimeException("exception decoding Hex string: " + e);
115 | }
116 |
117 | return bOut.toByteArray();
118 | }
119 |
120 | /**
121 | * decode the Hex encoded String data writing it to the given output stream,
122 | * whitespace characters will be ignored.
123 | *
124 | * @return the number of bytes produced.
125 | */
126 | public static int decode(
127 | String data,
128 | OutputStream out)
129 | throws IOException
130 | {
131 | return encoder.decode(data, out);
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/HexEncoder.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 |
4 |
5 | import java.io.IOException;
6 | import java.io.OutputStream;
7 |
8 | public class HexEncoder
9 | implements Encoder
10 | {
11 | protected final byte[] encodingTable =
12 | {
13 | (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
14 | (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
15 | };
16 |
17 | /*
18 | * set up the decoding table.
19 | */
20 | protected final byte[] decodingTable = new byte[128];
21 |
22 | protected void initialiseDecodingTable()
23 | {
24 | for (int i = 0; i < encodingTable.length; i++)
25 | {
26 | decodingTable[encodingTable[i]] = (byte)i;
27 | }
28 |
29 | decodingTable['A'] = decodingTable['a'];
30 | decodingTable['B'] = decodingTable['b'];
31 | decodingTable['C'] = decodingTable['c'];
32 | decodingTable['D'] = decodingTable['d'];
33 | decodingTable['E'] = decodingTable['e'];
34 | decodingTable['F'] = decodingTable['f'];
35 | }
36 |
37 | public HexEncoder()
38 | {
39 | initialiseDecodingTable();
40 | }
41 |
42 | /**
43 | * encode the input data producing a Hex output stream.
44 | *
45 | * @return the number of bytes produced.
46 | */
47 | public int encode(
48 | byte[] data,
49 | int off,
50 | int length,
51 | OutputStream out)
52 | throws IOException
53 | {
54 | for (int i = off; i < (off + length); i++)
55 | {
56 | int v = data[i] & 0xff;
57 |
58 | out.write(encodingTable[(v >>> 4)]);
59 | out.write(encodingTable[v & 0xf]);
60 | }
61 |
62 | return length * 2;
63 | }
64 |
65 | private boolean ignore(
66 | char c)
67 | {
68 | return (c == '\n' || c =='\r' || c == '\t' || c == ' ');
69 | }
70 |
71 | /**
72 | * decode the Hex encoded byte data writing it to the given output stream,
73 | * whitespace characters will be ignored.
74 | *
75 | * @return the number of bytes produced.
76 | */
77 | public int decode(
78 | byte[] data,
79 | int off,
80 | int length,
81 | OutputStream out)
82 | throws IOException
83 | {
84 | byte b1, b2;
85 | int outLen = 0;
86 |
87 | int end = off + length;
88 |
89 | while (end > off)
90 | {
91 | if (!ignore((char)data[end - 1]))
92 | {
93 | break;
94 | }
95 |
96 | end--;
97 | }
98 |
99 | int i = off;
100 | while (i < end)
101 | {
102 | while (i < end && ignore((char)data[i]))
103 | {
104 | i++;
105 | }
106 |
107 | b1 = decodingTable[data[i++]];
108 |
109 | while (i < end && ignore((char)data[i]))
110 | {
111 | i++;
112 | }
113 |
114 | b2 = decodingTable[data[i++]];
115 |
116 | out.write((b1 << 4) | b2);
117 |
118 | outLen++;
119 | }
120 |
121 | return outLen;
122 | }
123 |
124 | /**
125 | * decode the Hex encoded String data writing it to the given output stream,
126 | * whitespace characters will be ignored.
127 | *
128 | * @return the number of bytes produced.
129 | */
130 | public int decode(
131 | String data,
132 | OutputStream out)
133 | throws IOException
134 | {
135 | byte b1, b2;
136 | int length = 0;
137 |
138 | int end = data.length();
139 |
140 | while (end > 0)
141 | {
142 | if (!ignore(data.charAt(end - 1)))
143 | {
144 | break;
145 | }
146 |
147 | end--;
148 | }
149 |
150 | int i = 0;
151 | while (i < end)
152 | {
153 | while (i < end && ignore(data.charAt(i)))
154 | {
155 | i++;
156 | }
157 |
158 | b1 = decodingTable[data.charAt(i++)];
159 |
160 | while (i < end && ignore(data.charAt(i)))
161 | {
162 | i++;
163 | }
164 |
165 | b2 = decodingTable[data.charAt(i++)];
166 |
167 | out.write((b1 << 4) | b2);
168 |
169 | length++;
170 | }
171 |
172 | return length;
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/HexUtil.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 | /**
4 | * 16进制相关的算法工具类
5 | * @author chunjiang.shieh
6 | *
7 | */
8 | public class HexUtil {
9 |
10 |
11 | /**
12 | * 将字符内容转化为16进制字符串
13 | * @param txt
14 | * @return
15 | */
16 | public static String toHex(String txt) {
17 | return toHex(txt.getBytes());
18 | }
19 |
20 |
21 | /**
22 | * 转成16进制字符串 打印
23 | * @param buf
24 | * @return
25 | */
26 | public static String toHex(byte[] buf) {
27 | if (buf == null)
28 | return "";
29 | StringBuffer result = new StringBuffer(2 * buf.length);
30 | for (int i = 0; i < buf.length; i++) {
31 | appendHex(result, buf[i]);
32 | }
33 | return result.toString();
34 | }
35 |
36 |
37 | /**
38 | * 将16进制字符串转化成字符串
39 | * @param hex
40 | * @return
41 | */
42 | public static String fromHex(String hex) {
43 | return new String(hextoBytes(hex));
44 | }
45 |
46 |
47 |
48 | /**
49 | * 将16进制的字符(如:AFD0123)串转成字节数组
50 | * 一个16进制的字符占4bit
51 | * @param hexString
52 | * @return
53 | */
54 | public static byte[] hextoBytes(String hexString) {
55 | int len = hexString.length()/2;
56 | byte[] result = new byte[len];
57 | for (int i = 0; i < len; i++)
58 | result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
59 | return result;
60 | }
61 |
62 | private final static String HEX = "0123456789abcdef";
63 |
64 | private static void appendHex(StringBuffer sb, byte b) {
65 | sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
66 | }
67 |
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/Md5.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.security.MessageDigest;
5 | import java.security.NoSuchAlgorithmException;
6 |
7 | /**
8 | * MD5加密
9 | * 非对称加密,无法解密
10 | * @author chunjiang.shieh
11 | *
12 | */
13 | public class Md5 {
14 |
15 |
16 | /**
17 | * MD5加密后的字节以十六进制形式字符串返回
18 | * @param cleartext
19 | * @return
20 | */
21 | public static String toMd5(String cleartext) {
22 | return toMd5(cleartext.getBytes());
23 | }
24 |
25 | public static String toMd5(ByteArrayOutputStream baos){
26 | return toMd5(baos.toByteArray());
27 | }
28 |
29 | public static String toMd5(byte[] data){
30 | try {
31 | // Create MD5 Hash
32 | MessageDigest digest = MessageDigest.getInstance("MD5");
33 | digest.update(data);
34 | byte messageDigest[] = digest.digest();
35 | return HexUtil.toHex(messageDigest);
36 | } catch (NoSuchAlgorithmException e) {
37 | // TODO Auto-generated catch block
38 | e.printStackTrace();
39 | }
40 | return "";
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/SHA1.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 | /**
3 | * SHA1 hash(SHA1 校验码)
4 | * 保证文件完整性
5 | * @author chunjiang.shieh
6 | *
7 | */
8 | public class SHA1 {
9 |
10 | public static String digest(String content) {
11 | try {
12 | byte[] b = content.getBytes("utf-8");
13 | SHA1Digest digest = new SHA1Digest();
14 | byte[] out = new byte[digest.getDigestSize()];
15 | digest.update(b, 0, b.length);
16 | digest.doFinal(out,0);
17 | String shaHaxStr = new String(Hex.encode(out));
18 | return shaHaxStr;
19 | } catch (Exception e) {
20 | // TODO: handle exception
21 | e.printStackTrace();
22 | }
23 | return null;
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/SHA1Digest.java:
--------------------------------------------------------------------------------
1 | package com.xcj.android.ende;
2 |
3 | public class SHA1Digest extends GeneralDigest {
4 | private static final int DIGEST_LENGTH = 20;
5 |
6 | private int H1, H2, H3, H4, H5;
7 |
8 | private int[] X = new int[80];
9 | private int xOff;
10 |
11 | /**
12 | * Standard constructor
13 | */
14 | public SHA1Digest()
15 | {
16 | reset();
17 | }
18 |
19 | /**
20 | * Copy constructor. This will copy the state of the provided
21 | * message digest.
22 | */
23 | public SHA1Digest(SHA1Digest t)
24 | {
25 | super(t);
26 |
27 | H1 = t.H1;
28 | H2 = t.H2;
29 | H3 = t.H3;
30 | H4 = t.H4;
31 | H5 = t.H5;
32 |
33 | System.arraycopy(t.X, 0, X, 0, t.X.length);
34 | xOff = t.xOff;
35 | }
36 |
37 | public String getAlgorithmName()
38 | {
39 | return "SHA-1";
40 | }
41 |
42 | public int getDigestSize()
43 | {
44 | return DIGEST_LENGTH;
45 | }
46 |
47 | protected void processWord(
48 | byte[] in,
49 | int inOff)
50 | {
51 | X[xOff++] = ((in[inOff] & 0xff) << 24) | ((in[inOff + 1] & 0xff) << 16)
52 | | ((in[inOff + 2] & 0xff) << 8) | ((in[inOff + 3] & 0xff));
53 |
54 | if (xOff == 16)
55 | {
56 | processBlock();
57 | }
58 | }
59 |
60 | private void unpackWord(
61 | int word,
62 | byte[] out,
63 | int outOff)
64 | {
65 | out[outOff] = (byte)(word >>> 24);
66 | out[outOff + 1] = (byte)(word >>> 16);
67 | out[outOff + 2] = (byte)(word >>> 8);
68 | out[outOff + 3] = (byte)word;
69 | }
70 |
71 | protected void processLength(
72 | long bitLength)
73 | {
74 | if (xOff > 14)
75 | {
76 | processBlock();
77 | }
78 |
79 | X[14] = (int)(bitLength >>> 32);
80 | X[15] = (int)(bitLength & 0xffffffff);
81 | }
82 |
83 | public int doFinal(
84 | byte[] out,
85 | int outOff)
86 | {
87 | finish();
88 |
89 | unpackWord(H1, out, outOff);
90 | unpackWord(H2, out, outOff + 4);
91 | unpackWord(H3, out, outOff + 8);
92 | unpackWord(H4, out, outOff + 12);
93 | unpackWord(H5, out, outOff + 16);
94 |
95 | reset();
96 |
97 | return DIGEST_LENGTH;
98 | }
99 |
100 | /**
101 | * reset the chaining variables
102 | */
103 | public void reset()
104 | {
105 | super.reset();
106 |
107 | H1 = 0x67452301;
108 | H2 = 0xefcdab89;
109 | H3 = 0x98badcfe;
110 | H4 = 0x10325476;
111 | H5 = 0xc3d2e1f0;
112 |
113 | xOff = 0;
114 | for (int i = 0; i != X.length; i++)
115 | {
116 | X[i] = 0;
117 | }
118 | }
119 |
120 | //
121 | // Additive constants
122 | //
123 | private static final int Y1 = 0x5a827999;
124 | private static final int Y2 = 0x6ed9eba1;
125 | private static final int Y3 = 0x8f1bbcdc;
126 | private static final int Y4 = 0xca62c1d6;
127 |
128 | private int f(
129 | int u,
130 | int v,
131 | int w)
132 | {
133 | return ((u & v) | ((~u) & w));
134 | }
135 |
136 | private int h(
137 | int u,
138 | int v,
139 | int w)
140 | {
141 | return (u ^ v ^ w);
142 | }
143 |
144 | private int g(
145 | int u,
146 | int v,
147 | int w)
148 | {
149 | return ((u & v) | (u & w) | (v & w));
150 | }
151 |
152 | private int rotateLeft(
153 | int x,
154 | int n)
155 | {
156 | return (x << n) | (x >>> (32 - n));
157 | }
158 |
159 | protected void processBlock()
160 | {
161 | //
162 | // expand 16 word block into 80 word block.
163 | //
164 | for (int i = 16; i <= 79; i++)
165 | {
166 | X[i] = rotateLeft((X[i - 3] ^ X[i - 8] ^ X[i - 14] ^ X[i - 16]), 1);
167 | }
168 |
169 | //
170 | // set up working variables.
171 | //
172 | int A = H1;
173 | int B = H2;
174 | int C = H3;
175 | int D = H4;
176 | int E = H5;
177 |
178 | //
179 | // round 1
180 | //
181 | for (int j = 0; j <= 19; j++)
182 | {
183 | int t = rotateLeft(A, 5) + f(B, C, D) + E + X[j] + Y1;
184 |
185 | E = D;
186 | D = C;
187 | C = rotateLeft(B, 30);
188 | B = A;
189 | A = t;
190 | }
191 |
192 | //
193 | // round 2
194 | //
195 | for (int j = 20; j <= 39; j++)
196 | {
197 | int t = rotateLeft(A, 5) + h(B, C, D) + E + X[j] + Y2;
198 |
199 | E = D;
200 | D = C;
201 | C = rotateLeft(B, 30);
202 | B = A;
203 | A = t;
204 | }
205 |
206 | //
207 | // round 3
208 | //
209 | for (int j = 40; j <= 59; j++)
210 | {
211 | int t = rotateLeft(A, 5) + g(B, C, D) + E + X[j] + Y3;
212 |
213 | E = D;
214 | D = C;
215 | C = rotateLeft(B, 30);
216 | B = A;
217 | A = t;
218 | }
219 |
220 | //
221 | // round 4
222 | //
223 | for (int j = 60; j <= 79; j++)
224 | {
225 | int t = rotateLeft(A, 5) + h(B, C, D) + E + X[j] + Y4;
226 |
227 | E = D;
228 | D = C;
229 | C = rotateLeft(B, 30);
230 | B = A;
231 | A = t;
232 | }
233 |
234 | H1 += A;
235 | H2 += B;
236 | H3 += C;
237 | H4 += D;
238 | H5 += E;
239 |
240 | //
241 | // reset the offset and clean out the word buffer.
242 | //
243 | xOff = 0;
244 | for (int i = 0; i != X.length; i++)
245 | {
246 | X[i] = 0;
247 | }
248 | }
249 | }
250 |
--------------------------------------------------------------------------------
/src/com/xcj/android/ende/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Android 常用的一些加解密算法
3 | */
4 | /**
5 | * @author chunjiang.shieh
6 | *
7 | */
8 | package com.xcj.android.ende;
--------------------------------------------------------------------------------