AESTools.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // AESTools.m
  3. // Showroom
  4. //
  5. // Created by RD on 2021/3/8.
  6. //
  7. #import "AESTools.h"
  8. #import "AESEncrypt.h"
  9. #import "NSString+safe.h"
  10. #import "NSString+convert.h"
  11. typedef NS_ENUM(NSUInteger, AESType) {
  12. AES_ECB = 0,
  13. AES_CBC = 1
  14. };
  15. typedef NS_ENUM(NSUInteger, AESNum) {
  16. AES_128 = 0,
  17. AES_256 = 1
  18. };
  19. @interface AESTools ()
  20. @property (nonatomic, copy) NSString *AESKey; // 确定使用的密钥
  21. @property (nonatomic, copy) NSString *defaultKey; // 固定秘钥
  22. @property (nonatomic, copy) NSString *randomKey; // 随机密钥
  23. @property (nonatomic, copy) NSString *iv; // 向量 偏移量
  24. @property (nonatomic, assign) AESType aesType;
  25. @property (nonatomic, assign) AESNum aesNum;
  26. @end
  27. @implementation AESTools
  28. DEF_SINGLETON(AESTools)
  29. - (instancetype)init{
  30. self = [super init];
  31. if (self) {
  32. _defaultKey = @"31323334353631323334353637383930";
  33. _iv = @"22222222222222222222222222222222";
  34. //_randomKey = [self createRandomKey];
  35. }
  36. return self;
  37. }
  38. - (NSString *)encrypt:(NSString *)content{
  39. NSString *result = @"";
  40. if (_aesType == AES_ECB) {
  41. result = [self ecbEncrypt:content];
  42. }else{
  43. result = [self cbcEncrypt:content];
  44. }
  45. return result;
  46. }
  47. - (NSString *)decrypt:(NSString *)content{
  48. NSString *result = @"";
  49. if (_aesType == AES_ECB) {
  50. result = [self ecbDecrypt:content];
  51. }else{
  52. result = [self cbcDecrypt:content];
  53. }
  54. return result;
  55. }
  56. // cbc加密
  57. -(NSString *)cbcEncrypt:(NSString *)content{
  58. NSString *result = [AESEncrypt cbcEncrypt:content key:_AESKey];
  59. return result;
  60. }
  61. // ecb加密
  62. -(NSString *)ecbEncrypt:(NSString *)content{
  63. NSString *result = [AESEncrypt ecbEncrypt:content key:_AESKey];
  64. return result;
  65. }
  66. // cbc解密
  67. -(NSString *)cbcDecrypt:(NSString *)content{
  68. NSString *result = [AESEncrypt cbcDecrypt:content key:_AESKey];
  69. return result;
  70. }
  71. // ecb解密
  72. -(NSString *)ecbDecrypt:(NSString *)content{
  73. NSString *result = [AESEncrypt ecbDecrypt:content key:_AESKey];
  74. return result;
  75. }
  76. #pragma mark - -
  77. - (void)initFornoWorld{
  78. _AESKey = @"1234561234567890";
  79. _aesType = AES_ECB;
  80. _aesNum = AES_128;
  81. }
  82. - (void)setVersion:(NSString *)version{
  83. int v1 = [version rds_getSubStringSafetyWithRange:NSMakeRange(0, 4)].intValue;
  84. int v2 = [version rds_getSubStringSafetyWithRange:NSMakeRange(4, 2)].intValue;
  85. int v3 = [version rds_getSubStringSafetyWithRange:NSMakeRange(6, 2)].intValue;
  86. // 确定是用固定密钥还是随机密钥
  87. if (v1 == 1) {
  88. _AESKey = _defaultKey;
  89. }
  90. else if (v1 == 2){
  91. _AESKey = _randomKey;
  92. }
  93. else if (v1 == 3){
  94. // 未实现
  95. }
  96. // 1 ecb加密 2cbc加密
  97. if (v2 == 1) {
  98. _aesType = AES_ECB;
  99. }else if (v2 == 2){
  100. _aesType = AES_CBC;
  101. }
  102. if (v3 == 1) {
  103. // 128位
  104. _aesNum = AES_128;
  105. } else if (v3 == 2){
  106. // 256位
  107. _aesNum = AES_256;
  108. _AESKey = [_AESKey stringByAppendingString:_AESKey];
  109. }
  110. }
  111. // 加密后的随机密钥
  112. - (NSString *)getRandomKeyString{
  113. NSString *result = @"";
  114. NSString *key = _defaultKey;
  115. if (_aesNum == AES_128) {
  116. }else{
  117. key = [key stringByAppendingString:key];
  118. }
  119. if (_aesType == AES_ECB) {
  120. result = [AESEncrypt ecbEncrypt:_randomKey key:key];
  121. }else{
  122. result = [AESEncrypt cbcEncrypt:_randomKey key:key];
  123. }
  124. return result;
  125. }
  126. /// 生成随机密钥
  127. - (NSString *)createRandomKey{
  128. // 随机生成16字节字符串
  129. char data[32];
  130. for (int x=0;x<32;data[x++] = (char)('A' + (arc4random_uniform(26))));
  131. NSString *randomStr = [[NSString alloc] initWithBytes:data length:32 encoding:NSUTF8StringEncoding];
  132. NSLog(@"随机密钥:%@",randomStr);
  133. // 转16进制字符串
  134. //randomStr = [randomStr rds_stringTohexString];
  135. return randomStr;
  136. }
  137. @end