├── .gitignore ├── Android-AES-Test ├── .gitignore ├── .idea │ ├── caches │ │ └── build_file_checksums.ser │ ├── codeStyles │ │ └── Project.xml │ ├── dbnavigator.xml │ ├── encodings.xml │ ├── gradle.xml │ ├── misc.xml │ └── runConfigurations.xml ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── org │ │ │ └── skyfox │ │ │ └── android_aes_test │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── org │ │ │ │ └── skyfox │ │ │ │ └── android_aes_test │ │ │ │ ├── AESTool.java │ │ │ │ └── MainActivity.java │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── org │ │ └── skyfox │ │ └── android_aes_test │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── Java-AES-Test ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── bin │ └── .gitignore └── src │ └── AESTool.java ├── LICENSE ├── README.md └── iOS-AES-Test ├── iOS-AES-Test.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist └── iOS-AES-Test ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── ViewController.h ├── ViewController.m └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots/**/*.png 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /Android-AES-Test/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /Android-AES-Test/.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /Android-AES-Test/.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 36 | 37 | 38 | 39 | 40 | 46 | 47 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /Android-AES-Test/.idea/dbnavigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | -------------------------------------------------------------------------------- /Android-AES-Test/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Android-AES-Test/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /Android-AES-Test/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /Android-AES-Test/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /Android-AES-Test/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /Android-AES-Test/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "org.skyfox.android_aes_test" 7 | minSdkVersion 23 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:27.1.1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | } 29 | -------------------------------------------------------------------------------- /Android-AES-Test/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/androidTest/java/org/skyfox/android_aes_test/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package org.skyfox.android_aes_test; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("org.skyfox.android_aes_test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/java/org/skyfox/android_aes_test/AESTool.java: -------------------------------------------------------------------------------- 1 | package org.skyfox.android_aes_test; 2 | 3 | 4 | import javax.crypto.*; 5 | import javax.crypto.spec.IvParameterSpec; 6 | import javax.crypto.spec.SecretKeySpec; 7 | import java.io.UnsupportedEncodingException; 8 | import java.security.InvalidAlgorithmParameterException; 9 | import java.security.InvalidKeyException; 10 | import java.security.NoSuchAlgorithmException; 11 | import java.security.SecureRandom; 12 | 13 | 14 | public class AESTool { 15 | /** 16 | * AES加密字符串 17 | * 18 | * @param content 19 | * 需要被加密的字符串 20 | * @param password 21 | * 加密需要的密码 22 | * @return 密文 23 | */ 24 | public static byte[] encrypt(String content, String password) { 25 | try { 26 | if (password == null) { 27 | System.out.print("Key为空null"); 28 | return null; 29 | } 30 | // 判断Key是否为16位 31 | if (password.length() != 16) { 32 | System.out.print("Key长度不是16位"); 33 | return null; 34 | } 35 | byte[] raw = password.getBytes("utf-8"); 36 | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 37 | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式" 38 | cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 39 | byte[] result = cipher.doFinal(content.getBytes("utf-8")); 40 | 41 | // SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); 42 | // random.setSeed(password.getBytes()); 43 | // 44 | // KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者 45 | // 46 | // kgen.init(128, random);// 利用用户密码作为随机数初始化出 47 | // // 128位的key生产者 48 | // //加密没关系,SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以解密只要有password就行 49 | // 50 | // SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥 51 | // 52 | // byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 53 | // // null。 54 | // 55 | // SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥 56 | // 57 | // Cipher cipher = Cipher.getInstance("AES");// 创建密码器 58 | // 59 | // byte[] byteContent = content.getBytes("utf-8"); 60 | // 61 | // cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式的密码器 62 | // 63 | // byte[] result = cipher.doFinal(byteContent);// 加密 64 | 65 | return result; 66 | 67 | } catch (NoSuchPaddingException e) { 68 | e.printStackTrace(); 69 | } catch (NoSuchAlgorithmException e) { 70 | e.printStackTrace(); 71 | } catch (UnsupportedEncodingException e) { 72 | e.printStackTrace(); 73 | } catch (InvalidKeyException e) { 74 | e.printStackTrace(); 75 | } catch (IllegalBlockSizeException e) { 76 | e.printStackTrace(); 77 | } catch (BadPaddingException e) { 78 | e.printStackTrace(); 79 | } 80 | return null; 81 | } 82 | /** 83 | * 解密AES加密过的字符串 84 | * 85 | * @param content 86 | * AES加密过过的内容 87 | * @param password 88 | * 加密时的密码 89 | * @return 明文 90 | */ 91 | public static byte[] decrypt(byte[] content, String password) { 92 | try { 93 | // 判断Key是否正确 94 | if (password == null) { 95 | System.out.print("Key为空null"); 96 | return null; 97 | } 98 | // 判断Key是否为16位 99 | if (password.length() != 16) { 100 | System.out.print("Key长度不是16位"); 101 | return null; 102 | } 103 | byte[] raw = password.getBytes("utf-8"); 104 | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 105 | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 106 | cipher.init(Cipher.DECRYPT_MODE, skeySpec); 107 | 108 | try { 109 | byte[] original = cipher.doFinal(content); 110 | return original; 111 | } catch (Exception e) { 112 | System.out.println(e.toString()); 113 | return null; 114 | } 115 | 116 | // SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); 117 | // random.setSeed(password.getBytes()); 118 | // 119 | // KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者 120 | // 121 | // kgen.init(128, random);// 利用用户密码作为随机数初始化出 122 | // 123 | // SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥 124 | // byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥 125 | // 126 | // SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥 127 | // Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器 128 | // cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器 129 | // byte[] result = cipher.doFinal(content); 130 | // return null; // 明文 131 | 132 | } catch (NoSuchAlgorithmException e) { 133 | e.printStackTrace(); 134 | } catch (NoSuchPaddingException e) { 135 | e.printStackTrace(); 136 | } catch (InvalidKeyException e) { 137 | e.printStackTrace(); 138 | } catch (UnsupportedEncodingException e1) { 139 | // TODO Auto-generated catch block 140 | e1.printStackTrace(); 141 | } 142 | return null; 143 | } 144 | 145 | public static String decrypt(String content, String password) { 146 | byte[] decryptFrom = parseHexStr2Byte(content); 147 | byte[] decrypt = decrypt(decryptFrom, password); 148 | if (decrypt!=null&&decrypt.length!=0){ 149 | return new String(decrypt); 150 | } 151 | return null; 152 | } 153 | 154 | public static String parseByte2HexStr(byte buf[]) { 155 | StringBuffer sb = new StringBuffer(); 156 | for (int i = 0; i < buf.length; i++) { 157 | String hex = Integer.toHexString(buf[i] & 0xFF); 158 | if (hex.length() == 1) { 159 | hex = '0' + hex; 160 | } 161 | sb.append(hex.toUpperCase()); 162 | } 163 | return sb.toString(); 164 | } 165 | public static byte[] parseHexStr2Byte(String hexStr) { 166 | if (hexStr.length() < 1) 167 | return null; 168 | byte[] result = new byte[hexStr.length() / 2]; 169 | for (int i = 0; i < hexStr.length() / 2; i++) { 170 | int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); 171 | int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); 172 | result[i] = (byte) (high * 16 + low); 173 | } 174 | return result; 175 | } 176 | 177 | public static void test(){ 178 | String password = "1234512345123456"; 179 | 180 | String str = "12382929sadadasd"; 181 | 182 | byte[] encryptResult = encrypt(str, password); 183 | String encryptResultStr = parseByte2HexStr(encryptResult); 184 | System.out.println("加密后:" + encryptResultStr); 185 | 186 | 187 | 188 | 189 | 190 | byte[] decryptFrom = parseHexStr2Byte(encryptResultStr); 191 | byte[] decryptResult = decrypt(decryptFrom, password); 192 | System.out.println("解密后:" + new String(decryptResult)); 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/java/org/skyfox/android_aes_test/MainActivity.java: -------------------------------------------------------------------------------- 1 | package org.skyfox.android_aes_test; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | public class MainActivity extends AppCompatActivity { 7 | 8 | @Override 9 | protected void onCreate(Bundle savedInstanceState) { 10 | super.onCreate(savedInstanceState); 11 | setContentView(R.layout.activity_main); 12 | 13 | AESTool.test(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Android-AES-Test 3 | 4 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Android-AES-Test/app/src/test/java/org/skyfox/android_aes_test/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package org.skyfox.android_aes_test; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /Android-AES-Test/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | 5 | repositories { 6 | google() 7 | jcenter() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.1.2' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /Android-AES-Test/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /Android-AES-Test/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shaojiankui/AES-Java-iOS-Android/2b13d81357ee0f72a860dd924c043e68c223b015/Android-AES-Test/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Android-AES-Test/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Sep 27 16:11:14 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /Android-AES-Test/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /Android-AES-Test/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /Android-AES-Test/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /Java-AES-Test/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Java-AES-Test/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | AES-Test 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Java-AES-Test/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Java-AES-Test/bin/.gitignore: -------------------------------------------------------------------------------- 1 | /AESTool.class 2 | -------------------------------------------------------------------------------- /Java-AES-Test/src/AESTool.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | import javax.crypto.*; 5 | import javax.crypto.spec.IvParameterSpec; 6 | import javax.crypto.spec.SecretKeySpec; 7 | import java.io.UnsupportedEncodingException; 8 | import java.security.InvalidAlgorithmParameterException; 9 | import java.security.InvalidKeyException; 10 | import java.security.NoSuchAlgorithmException; 11 | import java.security.SecureRandom; 12 | 13 | 14 | public class AESTool { 15 | /** 16 | * AES加密字符串 17 | * 18 | * @param content 19 | * 需要被加密的字符串 20 | * @param password 21 | * 加密需要的密码 22 | * @return 密文 23 | */ 24 | public static byte[] encrypt(String content, String password) { 25 | try { 26 | if (password == null) { 27 | System.out.print("Key为空null"); 28 | return null; 29 | } 30 | // 判断Key是否为16位 31 | if (password.length() != 16) { 32 | System.out.print("Key长度不是16位"); 33 | return null; 34 | } 35 | byte[] raw = password.getBytes("utf-8"); 36 | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 37 | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式" 38 | cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 39 | byte[] result = cipher.doFinal(content.getBytes("utf-8")); 40 | 41 | // SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); 42 | // random.setSeed(password.getBytes()); 43 | // 44 | // KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者 45 | // 46 | // kgen.init(128, random);// 利用用户密码作为随机数初始化出 47 | // // 128位的key生产者 48 | // //加密没关系,SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以解密只要有password就行 49 | // 50 | // SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥 51 | // 52 | // byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥,如果此密钥不支持编码,则返回 53 | // // null。 54 | // 55 | // SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥 56 | // 57 | // Cipher cipher = Cipher.getInstance("AES");// 创建密码器 58 | // 59 | // byte[] byteContent = content.getBytes("utf-8"); 60 | // 61 | // cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化为加密模式的密码器 62 | // 63 | // byte[] result = cipher.doFinal(byteContent);// 加密 64 | 65 | return result; 66 | 67 | } catch (NoSuchPaddingException e) { 68 | e.printStackTrace(); 69 | } catch (NoSuchAlgorithmException e) { 70 | e.printStackTrace(); 71 | } catch (UnsupportedEncodingException e) { 72 | e.printStackTrace(); 73 | } catch (InvalidKeyException e) { 74 | e.printStackTrace(); 75 | } catch (IllegalBlockSizeException e) { 76 | e.printStackTrace(); 77 | } catch (BadPaddingException e) { 78 | e.printStackTrace(); 79 | } 80 | return null; 81 | } 82 | /** 83 | * 解密AES加密过的字符串 84 | * 85 | * @param content 86 | * AES加密过过的内容 87 | * @param password 88 | * 加密时的密码 89 | * @return 明文 90 | */ 91 | public static byte[] decrypt(byte[] content, String password) { 92 | try { 93 | // 判断Key是否正确 94 | if (password == null) { 95 | System.out.print("Key为空null"); 96 | return null; 97 | } 98 | // 判断Key是否为16位 99 | if (password.length() != 16) { 100 | System.out.print("Key长度不是16位"); 101 | return null; 102 | } 103 | byte[] raw = password.getBytes("utf-8"); 104 | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 105 | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 106 | cipher.init(Cipher.DECRYPT_MODE, skeySpec); 107 | 108 | try { 109 | byte[] original = cipher.doFinal(content); 110 | return original; 111 | } catch (Exception e) { 112 | System.out.println(e.toString()); 113 | return null; 114 | } 115 | 116 | // SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); 117 | // random.setSeed(password.getBytes()); 118 | // 119 | // KeyGenerator kgen = KeyGenerator.getInstance("AES");// 创建AES的Key生产者 120 | // 121 | // kgen.init(128, random);// 利用用户密码作为随机数初始化出 122 | // 123 | // SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥 124 | // byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥 125 | // 126 | // SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥 127 | // Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器 128 | // cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器 129 | // byte[] result = cipher.doFinal(content); 130 | // return null; // 明文 131 | 132 | } catch (NoSuchAlgorithmException e) { 133 | e.printStackTrace(); 134 | } catch (NoSuchPaddingException e) { 135 | e.printStackTrace(); 136 | } catch (InvalidKeyException e) { 137 | e.printStackTrace(); 138 | } catch (UnsupportedEncodingException e1) { 139 | // TODO Auto-generated catch block 140 | e1.printStackTrace(); 141 | } 142 | return null; 143 | } 144 | 145 | public static String decrypt(String content, String password) { 146 | byte[] decryptFrom = parseHexStr2Byte(content); 147 | byte[] decrypt = decrypt(decryptFrom, password); 148 | if (decrypt!=null&&decrypt.length!=0){ 149 | return new String(decrypt); 150 | } 151 | return null; 152 | } 153 | 154 | public static String parseByte2HexStr(byte buf[]) { 155 | StringBuffer sb = new StringBuffer(); 156 | for (int i = 0; i < buf.length; i++) { 157 | String hex = Integer.toHexString(buf[i] & 0xFF); 158 | if (hex.length() == 1) { 159 | hex = '0' + hex; 160 | } 161 | sb.append(hex.toUpperCase()); 162 | } 163 | return sb.toString(); 164 | } 165 | public static byte[] parseHexStr2Byte(String hexStr) { 166 | if (hexStr.length() < 1) 167 | return null; 168 | byte[] result = new byte[hexStr.length() / 2]; 169 | for (int i = 0; i < hexStr.length() / 2; i++) { 170 | int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); 171 | int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); 172 | result[i] = (byte) (high * 16 + low); 173 | } 174 | return result; 175 | } 176 | 177 | public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { 178 | String password = "1234512345123456"; 179 | 180 | String str = "12382929sadadasd"; 181 | 182 | byte[] encryptResult = encrypt(str, password); 183 | String encryptResultStr = parseByte2HexStr(encryptResult); 184 | System.out.println("加密后:" + encryptResultStr); 185 | 186 | 187 | 188 | byte[] decryptFrom = parseHexStr2Byte(encryptResultStr); 189 | byte[] decryptResult = decrypt(decryptFrom, password); 190 | System.out.println("解密后:" + new String(decryptResult)); 191 | } 192 | } 193 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 skyfox 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AES-Java-iOS-Android 2 | AES-Java-iOS-Android,兼容Java,iOS,Android三端的AES-128-ECB加密算法 3 | 4 | 代码逻辑中最后的data byte,转换成了16进制(HEX)返回最终加密串,当然使用base64也是可以的。 5 | 6 | 代码中的password必须为16位,低于16位其实不是不可以,得进行补全操作,demo中未处理。 7 | 8 | password:1234512345123456 9 | 10 | 未加密字符串:12382929sadadasd 11 | 12 | 加密后的HEX字符串:071ec0de1f3bf09643aca32c743a9c07a5030bc46cfad652b45cc8dbeaea0eee 13 | 14 | http://tool.chacuo.net/cryptaes 可以进行在线测试(ECB/ PKCS5/7 /数据块128 / 输出 hex) -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B818CDEC215CC1E60035262C /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = B818CDEB215CC1E60035262C /* AppDelegate.m */; }; 11 | B818CDEF215CC1E60035262C /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B818CDEE215CC1E60035262C /* ViewController.m */; }; 12 | B818CDF2215CC1E60035262C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B818CDF0215CC1E60035262C /* Main.storyboard */; }; 13 | B818CDF4215CC1EA0035262C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B818CDF3215CC1EA0035262C /* Assets.xcassets */; }; 14 | B818CDF7215CC1EB0035262C /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B818CDF5215CC1EA0035262C /* LaunchScreen.storyboard */; }; 15 | B818CDFA215CC1EB0035262C /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = B818CDF9215CC1EB0035262C /* main.m */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | B818CDE7215CC1E60035262C /* iOS-AES-Test.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "iOS-AES-Test.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | B818CDEA215CC1E60035262C /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 21 | B818CDEB215CC1E60035262C /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 22 | B818CDED215CC1E60035262C /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 23 | B818CDEE215CC1E60035262C /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 24 | B818CDF1215CC1E60035262C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 25 | B818CDF3215CC1EA0035262C /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 26 | B818CDF6215CC1EB0035262C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 27 | B818CDF8215CC1EB0035262C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 28 | B818CDF9215CC1EB0035262C /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | B818CDE4215CC1E60035262C /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | B818CDDE215CC1E60035262C = { 43 | isa = PBXGroup; 44 | children = ( 45 | B818CDE9215CC1E60035262C /* iOS-AES-Test */, 46 | B818CDE8215CC1E60035262C /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | B818CDE8215CC1E60035262C /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | B818CDE7215CC1E60035262C /* iOS-AES-Test.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | B818CDE9215CC1E60035262C /* iOS-AES-Test */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | B818CDEA215CC1E60035262C /* AppDelegate.h */, 62 | B818CDEB215CC1E60035262C /* AppDelegate.m */, 63 | B818CDED215CC1E60035262C /* ViewController.h */, 64 | B818CDEE215CC1E60035262C /* ViewController.m */, 65 | B818CDF0215CC1E60035262C /* Main.storyboard */, 66 | B818CDF3215CC1EA0035262C /* Assets.xcassets */, 67 | B818CDF5215CC1EA0035262C /* LaunchScreen.storyboard */, 68 | B818CDF8215CC1EB0035262C /* Info.plist */, 69 | B818CDF9215CC1EB0035262C /* main.m */, 70 | ); 71 | path = "iOS-AES-Test"; 72 | sourceTree = ""; 73 | }; 74 | /* End PBXGroup section */ 75 | 76 | /* Begin PBXNativeTarget section */ 77 | B818CDE6215CC1E60035262C /* iOS-AES-Test */ = { 78 | isa = PBXNativeTarget; 79 | buildConfigurationList = B818CDFD215CC1EB0035262C /* Build configuration list for PBXNativeTarget "iOS-AES-Test" */; 80 | buildPhases = ( 81 | B818CDE3215CC1E60035262C /* Sources */, 82 | B818CDE4215CC1E60035262C /* Frameworks */, 83 | B818CDE5215CC1E60035262C /* Resources */, 84 | ); 85 | buildRules = ( 86 | ); 87 | dependencies = ( 88 | ); 89 | name = "iOS-AES-Test"; 90 | productName = "iOS-AES-Test"; 91 | productReference = B818CDE7215CC1E60035262C /* iOS-AES-Test.app */; 92 | productType = "com.apple.product-type.application"; 93 | }; 94 | /* End PBXNativeTarget section */ 95 | 96 | /* Begin PBXProject section */ 97 | B818CDDF215CC1E60035262C /* Project object */ = { 98 | isa = PBXProject; 99 | attributes = { 100 | LastUpgradeCheck = 1000; 101 | ORGANIZATIONNAME = Jakey; 102 | TargetAttributes = { 103 | B818CDE6215CC1E60035262C = { 104 | CreatedOnToolsVersion = 10.0; 105 | }; 106 | }; 107 | }; 108 | buildConfigurationList = B818CDE2215CC1E60035262C /* Build configuration list for PBXProject "iOS-AES-Test" */; 109 | compatibilityVersion = "Xcode 9.3"; 110 | developmentRegion = en; 111 | hasScannedForEncodings = 0; 112 | knownRegions = ( 113 | en, 114 | Base, 115 | ); 116 | mainGroup = B818CDDE215CC1E60035262C; 117 | productRefGroup = B818CDE8215CC1E60035262C /* Products */; 118 | projectDirPath = ""; 119 | projectRoot = ""; 120 | targets = ( 121 | B818CDE6215CC1E60035262C /* iOS-AES-Test */, 122 | ); 123 | }; 124 | /* End PBXProject section */ 125 | 126 | /* Begin PBXResourcesBuildPhase section */ 127 | B818CDE5215CC1E60035262C /* Resources */ = { 128 | isa = PBXResourcesBuildPhase; 129 | buildActionMask = 2147483647; 130 | files = ( 131 | B818CDF7215CC1EB0035262C /* LaunchScreen.storyboard in Resources */, 132 | B818CDF4215CC1EA0035262C /* Assets.xcassets in Resources */, 133 | B818CDF2215CC1E60035262C /* Main.storyboard in Resources */, 134 | ); 135 | runOnlyForDeploymentPostprocessing = 0; 136 | }; 137 | /* End PBXResourcesBuildPhase section */ 138 | 139 | /* Begin PBXSourcesBuildPhase section */ 140 | B818CDE3215CC1E60035262C /* Sources */ = { 141 | isa = PBXSourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | B818CDEF215CC1E60035262C /* ViewController.m in Sources */, 145 | B818CDFA215CC1EB0035262C /* main.m in Sources */, 146 | B818CDEC215CC1E60035262C /* AppDelegate.m in Sources */, 147 | ); 148 | runOnlyForDeploymentPostprocessing = 0; 149 | }; 150 | /* End PBXSourcesBuildPhase section */ 151 | 152 | /* Begin PBXVariantGroup section */ 153 | B818CDF0215CC1E60035262C /* Main.storyboard */ = { 154 | isa = PBXVariantGroup; 155 | children = ( 156 | B818CDF1215CC1E60035262C /* Base */, 157 | ); 158 | name = Main.storyboard; 159 | sourceTree = ""; 160 | }; 161 | B818CDF5215CC1EA0035262C /* LaunchScreen.storyboard */ = { 162 | isa = PBXVariantGroup; 163 | children = ( 164 | B818CDF6215CC1EB0035262C /* Base */, 165 | ); 166 | name = LaunchScreen.storyboard; 167 | sourceTree = ""; 168 | }; 169 | /* End PBXVariantGroup section */ 170 | 171 | /* Begin XCBuildConfiguration section */ 172 | B818CDFB215CC1EB0035262C /* Debug */ = { 173 | isa = XCBuildConfiguration; 174 | buildSettings = { 175 | ALWAYS_SEARCH_USER_PATHS = NO; 176 | CLANG_ANALYZER_NONNULL = YES; 177 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 178 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 179 | CLANG_CXX_LIBRARY = "libc++"; 180 | CLANG_ENABLE_MODULES = YES; 181 | CLANG_ENABLE_OBJC_ARC = YES; 182 | CLANG_ENABLE_OBJC_WEAK = YES; 183 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 184 | CLANG_WARN_BOOL_CONVERSION = YES; 185 | CLANG_WARN_COMMA = YES; 186 | CLANG_WARN_CONSTANT_CONVERSION = YES; 187 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 188 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 189 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 190 | CLANG_WARN_EMPTY_BODY = YES; 191 | CLANG_WARN_ENUM_CONVERSION = YES; 192 | CLANG_WARN_INFINITE_RECURSION = YES; 193 | CLANG_WARN_INT_CONVERSION = YES; 194 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 195 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 196 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 197 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 198 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 199 | CLANG_WARN_STRICT_PROTOTYPES = YES; 200 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 201 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 202 | CLANG_WARN_UNREACHABLE_CODE = YES; 203 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 204 | CODE_SIGN_IDENTITY = "iPhone Developer"; 205 | COPY_PHASE_STRIP = NO; 206 | DEBUG_INFORMATION_FORMAT = dwarf; 207 | ENABLE_STRICT_OBJC_MSGSEND = YES; 208 | ENABLE_TESTABILITY = YES; 209 | GCC_C_LANGUAGE_STANDARD = gnu11; 210 | GCC_DYNAMIC_NO_PIC = NO; 211 | GCC_NO_COMMON_BLOCKS = YES; 212 | GCC_OPTIMIZATION_LEVEL = 0; 213 | GCC_PREPROCESSOR_DEFINITIONS = ( 214 | "DEBUG=1", 215 | "$(inherited)", 216 | ); 217 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 218 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 219 | GCC_WARN_UNDECLARED_SELECTOR = YES; 220 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 221 | GCC_WARN_UNUSED_FUNCTION = YES; 222 | GCC_WARN_UNUSED_VARIABLE = YES; 223 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 224 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 225 | MTL_FAST_MATH = YES; 226 | ONLY_ACTIVE_ARCH = YES; 227 | SDKROOT = iphoneos; 228 | }; 229 | name = Debug; 230 | }; 231 | B818CDFC215CC1EB0035262C /* Release */ = { 232 | isa = XCBuildConfiguration; 233 | buildSettings = { 234 | ALWAYS_SEARCH_USER_PATHS = NO; 235 | CLANG_ANALYZER_NONNULL = YES; 236 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 237 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 238 | CLANG_CXX_LIBRARY = "libc++"; 239 | CLANG_ENABLE_MODULES = YES; 240 | CLANG_ENABLE_OBJC_ARC = YES; 241 | CLANG_ENABLE_OBJC_WEAK = YES; 242 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 243 | CLANG_WARN_BOOL_CONVERSION = YES; 244 | CLANG_WARN_COMMA = YES; 245 | CLANG_WARN_CONSTANT_CONVERSION = YES; 246 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 247 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 248 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 249 | CLANG_WARN_EMPTY_BODY = YES; 250 | CLANG_WARN_ENUM_CONVERSION = YES; 251 | CLANG_WARN_INFINITE_RECURSION = YES; 252 | CLANG_WARN_INT_CONVERSION = YES; 253 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 254 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 255 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 256 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 257 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 258 | CLANG_WARN_STRICT_PROTOTYPES = YES; 259 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 260 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 261 | CLANG_WARN_UNREACHABLE_CODE = YES; 262 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 263 | CODE_SIGN_IDENTITY = "iPhone Developer"; 264 | COPY_PHASE_STRIP = NO; 265 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 266 | ENABLE_NS_ASSERTIONS = NO; 267 | ENABLE_STRICT_OBJC_MSGSEND = YES; 268 | GCC_C_LANGUAGE_STANDARD = gnu11; 269 | GCC_NO_COMMON_BLOCKS = YES; 270 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 271 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 272 | GCC_WARN_UNDECLARED_SELECTOR = YES; 273 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 274 | GCC_WARN_UNUSED_FUNCTION = YES; 275 | GCC_WARN_UNUSED_VARIABLE = YES; 276 | IPHONEOS_DEPLOYMENT_TARGET = 12.0; 277 | MTL_ENABLE_DEBUG_INFO = NO; 278 | MTL_FAST_MATH = YES; 279 | SDKROOT = iphoneos; 280 | VALIDATE_PRODUCT = YES; 281 | }; 282 | name = Release; 283 | }; 284 | B818CDFE215CC1EB0035262C /* Debug */ = { 285 | isa = XCBuildConfiguration; 286 | buildSettings = { 287 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 288 | CODE_SIGN_STYLE = Manual; 289 | DEVELOPMENT_TEAM = ""; 290 | INFOPLIST_FILE = "iOS-AES-Test/Info.plist"; 291 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 292 | LD_RUNPATH_SEARCH_PATHS = ( 293 | "$(inherited)", 294 | "@executable_path/Frameworks", 295 | ); 296 | PRODUCT_BUNDLE_IDENTIFIER = "org.skyfox.iOS-AES-Test"; 297 | PRODUCT_NAME = "$(TARGET_NAME)"; 298 | PROVISIONING_PROFILE_SPECIFIER = ""; 299 | TARGETED_DEVICE_FAMILY = "1,2"; 300 | }; 301 | name = Debug; 302 | }; 303 | B818CDFF215CC1EB0035262C /* Release */ = { 304 | isa = XCBuildConfiguration; 305 | buildSettings = { 306 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 307 | CODE_SIGN_STYLE = Manual; 308 | DEVELOPMENT_TEAM = ""; 309 | INFOPLIST_FILE = "iOS-AES-Test/Info.plist"; 310 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 311 | LD_RUNPATH_SEARCH_PATHS = ( 312 | "$(inherited)", 313 | "@executable_path/Frameworks", 314 | ); 315 | PRODUCT_BUNDLE_IDENTIFIER = "org.skyfox.iOS-AES-Test"; 316 | PRODUCT_NAME = "$(TARGET_NAME)"; 317 | PROVISIONING_PROFILE_SPECIFIER = ""; 318 | TARGETED_DEVICE_FAMILY = "1,2"; 319 | }; 320 | name = Release; 321 | }; 322 | /* End XCBuildConfiguration section */ 323 | 324 | /* Begin XCConfigurationList section */ 325 | B818CDE2215CC1E60035262C /* Build configuration list for PBXProject "iOS-AES-Test" */ = { 326 | isa = XCConfigurationList; 327 | buildConfigurations = ( 328 | B818CDFB215CC1EB0035262C /* Debug */, 329 | B818CDFC215CC1EB0035262C /* Release */, 330 | ); 331 | defaultConfigurationIsVisible = 0; 332 | defaultConfigurationName = Release; 333 | }; 334 | B818CDFD215CC1EB0035262C /* Build configuration list for PBXNativeTarget "iOS-AES-Test" */ = { 335 | isa = XCConfigurationList; 336 | buildConfigurations = ( 337 | B818CDFE215CC1EB0035262C /* Debug */, 338 | B818CDFF215CC1EB0035262C /* Release */, 339 | ); 340 | defaultConfigurationIsVisible = 0; 341 | defaultConfigurationName = Release; 342 | }; 343 | /* End XCConfigurationList section */ 344 | }; 345 | rootObject = B818CDDF215CC1E60035262C /* Project object */; 346 | } 347 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // iOS-AES-Test 4 | // 5 | // Created by Jakey on 2018/9/27. 6 | // Copyright © 2018 Jakey. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // iOS-AES-Test 4 | // 5 | // Created by Jakey on 2018/9/27. 6 | // Copyright © 2018 Jakey. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // iOS-AES-Test 4 | // 5 | // Created by Jakey on 2018/9/27. 6 | // Copyright © 2018 Jakey. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // iOS-AES-Test 4 | // 5 | // Created by Jakey on 2018/9/27. 6 | // Copyright © 2018 Jakey. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import 11 | 12 | @interface ViewController () 13 | 14 | @end 15 | 16 | @implementation ViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view, typically from a nib. 21 | NSString *password = @"1234512345123456"; 22 | NSString *str = @"12382929sadadasd"; 23 | 24 | 25 | 26 | NSData *dataEncrypt = [self CCCryptData:[str dataUsingEncoding:NSUTF8StringEncoding] operation:kCCEncrypt key:password]; 27 | NSString *hex = [self convertDataToHexStr:dataEncrypt]; 28 | NSLog(@"加密后:%@",hex); 29 | 30 | NSData *dataDecrypt = [self CCCryptData:[self convertHexStrToData:hex] operation:kCCDecrypt key:password]; 31 | NSString *decrypt = [[NSString alloc] initWithData:dataDecrypt encoding:NSUTF8StringEncoding]; 32 | 33 | NSLog(@"解密后:%@",decrypt); 34 | 35 | } 36 | 37 | - (NSData *)CCCryptData:(NSData *)data 38 | operation:(CCOperation)operation 39 | key:(NSString *)key { 40 | NSMutableData *keyData = [[key dataUsingEncoding:NSUTF8StringEncoding] mutableCopy]; 41 | 42 | size_t dataMoved; 43 | int size = kCCBlockSizeAES128; 44 | 45 | NSMutableData *decryptedData = [NSMutableData dataWithLength:data.length + size]; 46 | 47 | int option = kCCOptionPKCS7Padding | kCCOptionECBMode; 48 | 49 | 50 | CCCryptorStatus result = CCCrypt(operation, // kCCEncrypt or kCCDecrypt 51 | kCCAlgorithmAES128, 52 | option, // Padding option for CBC Mode 53 | keyData.bytes, 54 | keyData.length, 55 | NULL, 56 | data.bytes, 57 | data.length, 58 | decryptedData.mutableBytes, // encrypted data out 59 | decryptedData.length, 60 | &dataMoved); // total data moved 61 | 62 | if (result == kCCSuccess) { 63 | decryptedData.length = dataMoved; 64 | return decryptedData; 65 | } 66 | return nil; 67 | } 68 | 69 | 70 | 71 | 72 | - (NSData *)convertHexStrToData:(NSString *)str { 73 | if (!str || [str length] == 0) { 74 | return nil; 75 | 76 | } 77 | NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8]; 78 | NSRange range; 79 | if ([str length] % 2 == 0) { 80 | range = NSMakeRange(0, 2); 81 | 82 | } else { 83 | range = NSMakeRange(0, 1); 84 | 85 | } 86 | for (NSInteger i = range.location; i < [str length]; i += 2) { 87 | unsigned int anInt; 88 | NSString *hexCharStr = [str substringWithRange:range]; 89 | NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr]; 90 | [scanner scanHexInt:&anInt]; 91 | NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1]; 92 | [hexData appendData:entity]; 93 | range.location += range.length; 94 | range.length = 2; 95 | } 96 | return hexData; 97 | } 98 | 99 | - (NSString *)convertDataToHexStr:(NSData *)data { 100 | if (!data || [data length] == 0) { 101 | return @""; 102 | 103 | } 104 | NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]]; 105 | 106 | [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) { 107 | unsigned char *dataBytes = (unsigned char*)bytes; 108 | for (NSInteger i = 0; i < byteRange.length; i++) { 109 | NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff]; if ([hexStr length] == 2) { 110 | [string appendString:hexStr]; 111 | } else { 112 | [string appendFormat:@"0%@", hexStr]; 113 | } 114 | } 115 | }]; 116 | return string; 117 | } 118 | 119 | @end 120 | -------------------------------------------------------------------------------- /iOS-AES-Test/iOS-AES-Test/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // iOS-AES-Test 4 | // 5 | // Created by Jakey on 2018/9/27. 6 | // Copyright © 2018 Jakey. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------