1 // 2 // Copyright (C) 2011 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "shill/crypto_rot47.h" 18 19 using std::string; 20 21 namespace shill { 22 23 const char CryptoROT47::kID[] = "rot47"; 24 CryptoROT47()25CryptoROT47::CryptoROT47() {} 26 GetID()27string CryptoROT47::GetID() { 28 return kID; 29 } 30 Encrypt(const string & plaintext,string * ciphertext)31bool CryptoROT47::Encrypt(const string& plaintext, string* ciphertext) { 32 const int kRotSize = 94; 33 const int kRotHalf = kRotSize / 2; 34 const char kRotMin = '!'; 35 const char kRotMax = kRotMin + kRotSize - 1; 36 37 *ciphertext = plaintext; 38 for (auto& ch : *ciphertext) { 39 if (ch < kRotMin || ch > kRotMax) { 40 continue; 41 } 42 int rot = ch + kRotHalf; 43 ch = (rot > kRotMax) ? rot - kRotSize : rot; 44 } 45 return true; 46 } 47 Decrypt(const string & ciphertext,string * plaintext)48bool CryptoROT47::Decrypt(const string& ciphertext, string* plaintext) { 49 // ROT47 is self-reciprocal. 50 return Encrypt(ciphertext, plaintext); 51 } 52 53 } // namespace shill 54