· 7 years ago · Feb 26, 2018, 06:40 AM
1public string EncryptString(string ClearText)
2{
3
4 byte[] clearTextBytes = Encoding.UTF8.GetBytes(ClearText);
5
6 System.Security.Cryptography.SymmetricAlgorithm rijn =
7 SymmetricAlgorithm.Create();
8
9 MemoryStream ms = new MemoryStream();
10 byte[] rgbIV = Encoding.ASCII.GetBytes("ryojvlzmdalyglrj");
11 byte[] key =
12 Encoding.ASCII.GetBytes("hcxilkqbbhczfeultgbskdmaunivmfuo");
13 CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV),
14 CryptoStreamMode.Write);
15
16 cs.Write(clearTextBytes, 0, clearTextBytes.Length);
17
18 cs.Close();
19
20 return Convert.ToBase64String(ms.ToArray());
21}
22
23public String encryptText(String cipherText) throws Exception {
24
25 String plainKey = "hcxilkqbbhczfeultgbskdmaunivmfuo";
26 String plainIV = "ryojvlzmdalyglrj";
27
28 KeySpec ks = new DESKeySpec(plainKey.getBytes("UTF-8"));
29 SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(ks);
30
31 IvParameterSpec ivspec = new IvParameterSpec(plainIV.getBytes());
32
33 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
34 cipher.init(Cipher.ENCRYPT_MODE, key, ivspec);
35 byte[] decoded = cipher.doFinal(cipherText.getBytes("UTF-8"));
36 return new Base64().encodeToString(decoded);
37}