Java中的AES加密(SHA1PRNG)转换为C#

Java代码:

1 KeyGenerator aesGen = KeyGenerator.getInstance("AES");
2 SecureRandom secureRadmon= new SecureRandom().getInstance("SHA1PRNG");
3 secureRadmon.setSeed(aesKey.getBytes());
4 aesGen.init(128,secureRadmon);
5 SecretKey secretKey =aesGen.generateKey();

.Net(C#)代码:

1 byte[] keyArray = null;
2 using (var sha1 = new SHA1CryptoServiceProvider())
3 {
4     byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(aesKey));
5     var rd = sha1.ComputeHash(hash);
6     keyArray = rd.Take(16).ToArray();
7 }

------------------附对应的AES/ECB/PKCS5Padding加密-----------------------

 1 private static string encryptAES(string keyStr)
 2 {
 3 byte[] plainText = System.Text.Encoding.UTF8.GetBytes(keyStr);
 4 Base64Encoder encoder = new Base64Encoder();
 5 RijndaelManaged AesCipher = new RijndaelManaged();
 6 AesCipher.KeySize = 128; // 192, 256
 7 AesCipher.BlockSize = 128;
 8 AesCipher.Mode = CipherMode.ECB;
 9 AesCipher.Padding = PaddingMode.PKCS7; 
10 //AesCipher.IV = keyArray;
11 //AesCipher.Key = keyArray;
12 ICryptoTransform crypto = AesCipher.CreateEncryptor();
13 byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length);
14 string aesBase64 = Regex.Replace(encoder.GetEncoded(cipherText), @"[\r\n]+", "");
15 return aesBase64;
16 }
View Code

>>>>>>>>>>

  1     /// <summary>
  2     /// Base64编码类。
  3     /// 将byte[]类型转换成Base64编码的string类型。
  4     /// </summary>
  5     public class Base64Encoder
  6     {
  7         byte[] source;
  8         int length, length2;
  9         int blockCount;
 10         int paddingCount;
 11         public static Base64Encoder Encoder = new Base64Encoder();
 12 
 13         public Base64Encoder()
 14         {
 15         }
 16 
 17         private void init(byte[] input)
 18         {
 19             source = input;
 20             length = input.Length;
 21             if ((length % 3) == 0)
 22             {
 23                 paddingCount = 0;
 24                 blockCount = length / 3;
 25             }
 26             else
 27             {
 28                 paddingCount = 3 - (length % 3);
 29                 blockCount = (length + paddingCount) / 3;
 30             }
 31             length2 = length + paddingCount;
 32         }
 33 
 34         public string GetEncoded(byte[] input)
 35         {
 36             //初始化
 37             init(input);
 38             byte[] source2;
 39             source2 = new byte[length2];
 40 
 41             for (int x = 0; x < length2; x++)
 42             {
 43                 if (x < length)
 44                 {
 45                     source2[x] = source[x];
 46                 }
 47                 else
 48                 {
 49                     source2[x] = 0;
 50                 }
 51             }
 52 
 53             byte b1, b2, b3;
 54             byte temp, temp1, temp2, temp3, temp4;
 55             byte[] buffer = new byte[blockCount * 4];
 56             char[] result = new char[blockCount * 4];
 57             for (int x = 0; x < blockCount; x++)
 58             {
 59                 b1 = source2[x * 3];
 60                 b2 = source2[x * 3 + 1];
 61                 b3 = source2[x * 3 + 2];
 62                 temp1 = (byte)((b1 & 252) >> 2);
 63                 temp = (byte)((b1 & 3) << 4);
 64                 temp2 = (byte)((b2 & 240) >> 4);
 65                 temp2 += temp;
 66                 temp = (byte)((b2 & 15) << 2);
 67                 temp3 = (byte)((b3 & 192) >> 6);
 68                 temp3 += temp;
 69                 temp4 = (byte)(b3 & 63);
 70                 buffer[x * 4] = temp1;
 71                 buffer[x * 4 + 1] = temp2;
 72                 buffer[x * 4 + 2] = temp3;
 73                 buffer[x * 4 + 3] = temp4;
 74             }
 75 
 76             for (int x = 0; x < blockCount * 4; x++)
 77             {
 78                 result[x] = sixbit2char(buffer[x]);
 79             }
 80 
 81             switch (paddingCount)
 82             {
 83                 case 0: break;
 84                 case 1: result[blockCount * 4 - 1] = '='; break;
 85                 case 2:
 86                     result[blockCount * 4 - 1] = '=';
 87                     result[blockCount * 4 - 2] = '=';
 88                     break;
 89                 default: break;
 90             }
 91             return new string(result);
 92         }
 93 
 94         private char sixbit2char(byte b)
 95         {
 96             char[] lookupTable = new char[64]{
 97                   'A','B','C','D','E','F','G','H','I','J','K','L','M',
 98                  'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
 99                  'a','b','c','d','e','f','g','h','i','j','k','l','m',
100                  'n','o','p','q','r','s','t','u','v','w','x','y','z',
101                  '0','1','2','3','4','5','6','7','8','9','+','/'};
102 
103             if ((b >= 0) && (b <= 63))
104             {
105                 return lookupTable[(int)b];
106             }
107             else
108             {
109                 return ' ';
110             }
111         }
112     }

 

posted @ 2019-07-03 12:25  妙衍软设  阅读(5332)  评论(1编辑  收藏  举报