Android编程加密算法小结(AES、Base64、RAS加密算法)
程序员文章站
2023-12-15 19:34:16
本文实例总结了android编程加密算法。分享给大家供大家参考,具体如下:
android常用加密算法之base64加密算法:
package com.long...
本文实例总结了android编程加密算法。分享给大家供大家参考,具体如下:
android常用加密算法之base64加密算法:
package com.long; /** * copyright (c) 2010 the android open source project * * licensed under the apache license, version 2.0 (the "license"); * you may not use this file except in compliance with the license. * you may obtain a copy of the license at * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in writing, software * distributed under the license is distributed on an "as is" basis, * without warranties or conditions of any kind, either express or implied. * see the license for the specific language governing permissions and * limitations under the license. */ import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.outputstream; /* * @author long * */ public class base64 { private static final char[] legalchars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/" .tochararray(); public static string encode(byte[] data) { int start = 0; int len = data.length; stringbuffer buf = new stringbuffer(data.length * 3 / 2); int end = len - 3; int i = start; int n = 0; while (i <= end) { int d = ((((int) data[i]) & 0x0ff) << 16) | ((((int) data[i + 1]) & 0x0ff) << 8) | (((int) data[i + 2]) & 0x0ff); buf.append(legalchars[(d >> 18) & 63]); buf.append(legalchars[(d >> 12) & 63]); buf.append(legalchars[(d >> 6) & 63]); buf.append(legalchars[d & 63]); i += 3; if (n++ >= 14) { n = 0; buf.append(" "); } } if (i == start + len - 2) { int d = ((((int) data[i]) & 0x0ff) << 16) | ((((int) data[i + 1]) & 255) << 8); buf.append(legalchars[(d >> 18) & 63]); buf.append(legalchars[(d >> 12) & 63]); buf.append(legalchars[(d >> 6) & 63]); buf.append("="); } else if (i == start + len - 1) { int d = (((int) data[i]) & 0x0ff) << 16; buf.append(legalchars[(d >> 18) & 63]); buf.append(legalchars[(d >> 12) & 63]); buf.append("=="); } return buf.tostring(); } private static int decode(char c) { if (c >= 'a' && c <= 'z') return ((int) c) - 65; else if (c >= 'a' && c <= 'z') return ((int) c) - 97 + 26; else if (c >= '0' && c <= '9') return ((int) c) - 48 + 26 + 26; else switch (c) { case '+': return 62; case '/': return 63; case '=': return 0; default: throw new runtimeexception("unexpected code: " + c); } } public static byte[] decode(string s) { bytearrayoutputstream bos = new bytearrayoutputstream(); try { decode(s, bos); } catch (ioexception e) { throw new runtimeexception(); } byte[] decodedbytes = bos.tobytearray(); try { bos.close(); bos = null; } catch (ioexception ex) { system.err.println("error while decoding base64: " + ex.tostring()); } return decodedbytes; } private static void decode(string s, outputstream os) throws ioexception { int i = 0; int len = s.length(); while (true) { while (i < len && s.charat(i) <= ' ') i++; if (i == len) break; int tri = (decode(s.charat(i)) << 18) + (decode(s.charat(i + 1)) << 12) + (decode(s.charat(i + 2)) << 6) + (decode(s.charat(i + 3))); os.write((tri >> 16) & 255); if (s.charat(i + 2) == '=') break; os.write((tri >> 8) & 255); if (s.charat(i + 3) == '=') break; os.write(tri & 255); i += 4; } } }
android常用加密算法之aes加密算法:
package com.long; import java.security.securerandom; import javax.crypto.cipher; import javax.crypto.keygenerator; import javax.crypto.secretkey; import javax.crypto.spec.secretkeyspec; /** * aes加密解密算法 * * @author long * */ public class encryption { private final static string hex = "0123456789abcdef"; public static string encrypt(string seed, string cleartext) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] result = encrypt(rawkey, cleartext.getbytes()); return tohex(result); } public static string decrypt(string seed, string encrypted) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] enc = tobyte(encrypted); byte[] result = decrypt(rawkey, enc); return new string(result); } private static byte[] getrawkey(byte[] seed) throws exception { keygenerator kgen = keygenerator.getinstance("aes"); securerandom sr = securerandom.getinstance("sha1prng"); sr.setseed(seed); kgen.init(128, sr); // 192 and 256 bits may not be available secretkey skey = kgen.generatekey(); byte[] raw = skey.getencoded(); return raw; } private static byte[] encrypt(byte[] raw, byte[] clear) throws exception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.encrypt_mode, skeyspec); byte[] encrypted = cipher.dofinal(clear); return encrypted; } private static byte[] decrypt(byte[] raw, byte[] encrypted) throws exception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, skeyspec); byte[] decrypted = cipher.dofinal(encrypted); return decrypted; } public static string tohex(string txt) { return tohex(txt.getbytes()); } public static string fromhex(string hex) { return new string(tobyte(hex)); } public static byte[] tobyte(string hexstring) { int len = hexstring.length() / 2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) result[i] = integer.valueof(hexstring.substring(2 * i, 2 * i + 2), 16).bytevalue(); return result; } public static string tohex(byte[] buf) { if (buf == null) return ""; stringbuffer result = new stringbuffer(2 * buf.length); for (int i = 0; i < buf.length; i++) { appendhex(result, buf[i]); } return result.tostring(); } private static void appendhex(stringbuffer sb, byte b) { sb.append(hex.charat((b >> 4) & 0x0f)).append(hex.charat(b & 0x0f)); } }
android常用加密算法之ras加密算法:
import java.security.key; import java.security.keyfactory; import java.security.keypair; import java.security.keypairgenerator; import java.security.privatekey; import java.security.publickey; import java.security.interfaces.rsaprivatekey; import java.security.interfaces.rsapublickey; import java.security.spec.pkcs8encodedkeyspec; import java.security.spec.x509encodedkeyspec; import javax.crypto.cipher; import sun.misc.base64decoder; import sun.misc.base64encoder; public class rsahelper { public static publickey getpublickey(string key) throws exception { byte[] keybytes; keybytes = (new base64decoder()).decodebuffer(key); x509encodedkeyspec keyspec = new x509encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance("rsa"); publickey publickey = keyfactory.generatepublic(keyspec); return publickey; } public static privatekey getprivatekey(string key) throws exception { byte[] keybytes; keybytes = (new base64decoder()).decodebuffer(key); pkcs8encodedkeyspec keyspec = new pkcs8encodedkeyspec(keybytes); keyfactory keyfactory = keyfactory.getinstance("rsa"); privatekey privatekey = keyfactory.generateprivate(keyspec); return privatekey; } public static string getkeystring(key key) throws exception { byte[] keybytes = key.getencoded(); string s = (new base64encoder()).encode(keybytes); return s; } public static void main(string[] args) throws exception { keypairgenerator keypairgen = keypairgenerator.getinstance("rsa"); //密钥位数 keypairgen.initialize(1024); //密钥对 keypair keypair = keypairgen.generatekeypair(); // 公钥 publickey publickey = (rsapublickey) keypair.getpublic(); // 私钥 privatekey privatekey = (rsaprivatekey) keypair.getprivate(); string publickeystring = getkeystring(publickey); system.out.println("public:\n" + publickeystring); string privatekeystring = getkeystring(privatekey); system.out.println("private:\n" + privatekeystring); //加解密类 cipher cipher = cipher.getinstance("rsa");//cipher.getinstance("rsa/ecb/pkcs1padding"); //明文 byte[] plaintext = "我们都很好!邮件:@sina.com".getbytes(); //加密 cipher.init(cipher.encrypt_mode, publickey); byte[] enbytes = cipher.dofinal(plaintext); //通过密钥字符串得到密钥 publickey = getpublickey(publickeystring); privatekey = getprivatekey(privatekeystring); //解密 cipher.init(cipher.decrypt_mode, privatekey); byte[]debytes = cipher.dofinal(enbytes); publickeystring = getkeystring(publickey); system.out.println("public:\n" +publickeystring); privatekeystring = getkeystring(privatekey); system.out.println("private:\n" + privatekeystring); string s = new string(debytes); system.out.println(s); } }
希望本文所述对大家android程序设计有所帮助。